check.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
  4. */
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <inttypes.h>
  8. #include <sys/mman.h>
  9. #include <objtool/builtin.h>
  10. #include <objtool/cfi.h>
  11. #include <objtool/arch.h>
  12. #include <objtool/check.h>
  13. #include <objtool/special.h>
  14. #include <objtool/warn.h>
  15. #include <objtool/endianness.h>
  16. #include <linux/objtool_types.h>
  17. #include <linux/hashtable.h>
  18. #include <linux/kernel.h>
  19. #include <linux/static_call_types.h>
  20. #include <linux/string.h>
  21. struct alternative {
  22. struct alternative *next;
  23. struct instruction *insn;
  24. bool skip_orig;
  25. };
  26. static unsigned long nr_cfi, nr_cfi_reused, nr_cfi_cache;
  27. static struct cfi_init_state initial_func_cfi;
  28. static struct cfi_state init_cfi;
  29. static struct cfi_state func_cfi;
  30. static struct cfi_state force_undefined_cfi;
  31. struct instruction *find_insn(struct objtool_file *file,
  32. struct section *sec, unsigned long offset)
  33. {
  34. struct instruction *insn;
  35. hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) {
  36. if (insn->sec == sec && insn->offset == offset)
  37. return insn;
  38. }
  39. return NULL;
  40. }
  41. struct instruction *next_insn_same_sec(struct objtool_file *file,
  42. struct instruction *insn)
  43. {
  44. if (insn->idx == INSN_CHUNK_MAX)
  45. return find_insn(file, insn->sec, insn->offset + insn->len);
  46. insn++;
  47. if (!insn->len)
  48. return NULL;
  49. return insn;
  50. }
  51. static struct instruction *next_insn_same_func(struct objtool_file *file,
  52. struct instruction *insn)
  53. {
  54. struct instruction *next = next_insn_same_sec(file, insn);
  55. struct symbol *func = insn_func(insn);
  56. if (!func)
  57. return NULL;
  58. if (next && insn_func(next) == 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. static struct instruction *prev_insn_same_sec(struct objtool_file *file,
  67. struct instruction *insn)
  68. {
  69. if (insn->idx == 0) {
  70. if (insn->prev_len)
  71. return find_insn(file, insn->sec, insn->offset - insn->prev_len);
  72. return NULL;
  73. }
  74. return insn - 1;
  75. }
  76. static struct instruction *prev_insn_same_sym(struct objtool_file *file,
  77. struct instruction *insn)
  78. {
  79. struct instruction *prev = prev_insn_same_sec(file, insn);
  80. if (prev && insn_func(prev) == insn_func(insn))
  81. return prev;
  82. return NULL;
  83. }
  84. #define for_each_insn(file, insn) \
  85. for (struct section *__sec, *__fake = (struct section *)1; \
  86. __fake; __fake = NULL) \
  87. for_each_sec(file, __sec) \
  88. sec_for_each_insn(file, __sec, insn)
  89. #define func_for_each_insn(file, func, insn) \
  90. for (insn = find_insn(file, func->sec, func->offset); \
  91. insn; \
  92. insn = next_insn_same_func(file, insn))
  93. #define sym_for_each_insn(file, sym, insn) \
  94. for (insn = find_insn(file, sym->sec, sym->offset); \
  95. insn && insn->offset < sym->offset + sym->len; \
  96. insn = next_insn_same_sec(file, insn))
  97. #define sym_for_each_insn_continue_reverse(file, sym, insn) \
  98. for (insn = prev_insn_same_sec(file, insn); \
  99. insn && insn->offset >= sym->offset; \
  100. insn = prev_insn_same_sec(file, insn))
  101. #define sec_for_each_insn_from(file, insn) \
  102. for (; insn; insn = next_insn_same_sec(file, insn))
  103. #define sec_for_each_insn_continue(file, insn) \
  104. for (insn = next_insn_same_sec(file, insn); insn; \
  105. insn = next_insn_same_sec(file, insn))
  106. static inline struct symbol *insn_call_dest(struct instruction *insn)
  107. {
  108. if (insn->type == INSN_JUMP_DYNAMIC ||
  109. insn->type == INSN_CALL_DYNAMIC)
  110. return NULL;
  111. return insn->_call_dest;
  112. }
  113. static inline struct reloc *insn_jump_table(struct instruction *insn)
  114. {
  115. if (insn->type == INSN_JUMP_DYNAMIC ||
  116. insn->type == INSN_CALL_DYNAMIC)
  117. return insn->_jump_table;
  118. return NULL;
  119. }
  120. static bool is_jump_table_jump(struct instruction *insn)
  121. {
  122. struct alt_group *alt_group = insn->alt_group;
  123. if (insn_jump_table(insn))
  124. return true;
  125. /* Retpoline alternative for a jump table? */
  126. return alt_group && alt_group->orig_group &&
  127. insn_jump_table(alt_group->orig_group->first_insn);
  128. }
  129. static bool is_sibling_call(struct instruction *insn)
  130. {
  131. /*
  132. * Assume only STT_FUNC calls have jump-tables.
  133. */
  134. if (insn_func(insn)) {
  135. /* An indirect jump is either a sibling call or a jump to a table. */
  136. if (insn->type == INSN_JUMP_DYNAMIC)
  137. return !is_jump_table_jump(insn);
  138. }
  139. /* add_jump_destinations() sets insn_call_dest(insn) for sibling calls. */
  140. return (is_static_jump(insn) && insn_call_dest(insn));
  141. }
  142. /*
  143. * Checks if a string ends with another.
  144. */
  145. static bool str_ends_with(const char *s, const char *sub)
  146. {
  147. const int slen = strlen(s);
  148. const int sublen = strlen(sub);
  149. if (sublen > slen)
  150. return 0;
  151. return !memcmp(s + slen - sublen, sub, sublen);
  152. }
  153. /*
  154. * Checks if a function is a Rust "noreturn" one.
  155. */
  156. static bool is_rust_noreturn(const struct symbol *func)
  157. {
  158. /*
  159. * If it does not start with "_R", then it is not a Rust symbol.
  160. */
  161. if (strncmp(func->name, "_R", 2))
  162. return false;
  163. /*
  164. * These are just heuristics -- we do not control the precise symbol
  165. * name, due to the crate disambiguators (which depend on the compiler)
  166. * as well as changes to the source code itself between versions (since
  167. * these come from the Rust standard library).
  168. */
  169. return str_ends_with(func->name, "_4core5sliceSp15copy_from_slice17len_mismatch_fail") ||
  170. str_ends_with(func->name, "_4core6option13expect_failed") ||
  171. str_ends_with(func->name, "_4core6option13unwrap_failed") ||
  172. str_ends_with(func->name, "_4core6result13unwrap_failed") ||
  173. str_ends_with(func->name, "_4core9panicking5panic") ||
  174. str_ends_with(func->name, "_4core9panicking9panic_fmt") ||
  175. str_ends_with(func->name, "_4core9panicking14panic_explicit") ||
  176. str_ends_with(func->name, "_4core9panicking14panic_nounwind") ||
  177. str_ends_with(func->name, "_4core9panicking18panic_bounds_check") ||
  178. str_ends_with(func->name, "_4core9panicking18panic_nounwind_fmt") ||
  179. str_ends_with(func->name, "_4core9panicking19assert_failed_inner") ||
  180. str_ends_with(func->name, "_4core9panicking30panic_null_pointer_dereference") ||
  181. str_ends_with(func->name, "_4core9panicking36panic_misaligned_pointer_dereference") ||
  182. str_ends_with(func->name, "_7___rustc17rust_begin_unwind") ||
  183. strstr(func->name, "_4core9panicking13assert_failed") ||
  184. strstr(func->name, "_4core9panicking11panic_const24panic_const_") ||
  185. (strstr(func->name, "_4core5slice5index") &&
  186. strstr(func->name, "slice_") &&
  187. str_ends_with(func->name, "_fail"));
  188. }
  189. /*
  190. * This checks to see if the given function is a "noreturn" function.
  191. *
  192. * For global functions which are outside the scope of this object file, we
  193. * have to keep a manual list of them.
  194. *
  195. * For local functions, we have to detect them manually by simply looking for
  196. * the lack of a return instruction.
  197. */
  198. static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
  199. int recursion)
  200. {
  201. int i;
  202. struct instruction *insn;
  203. bool empty = true;
  204. #define NORETURN(func) __stringify(func),
  205. static const char * const global_noreturns[] = {
  206. #include "noreturns.h"
  207. };
  208. #undef NORETURN
  209. if (!func)
  210. return false;
  211. if (func->bind == STB_GLOBAL || func->bind == STB_WEAK) {
  212. if (is_rust_noreturn(func))
  213. return true;
  214. for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
  215. if (!strcmp(func->name, global_noreturns[i]))
  216. return true;
  217. }
  218. if (func->bind == STB_WEAK)
  219. return false;
  220. if (!func->len)
  221. return false;
  222. insn = find_insn(file, func->sec, func->offset);
  223. if (!insn || !insn_func(insn))
  224. return false;
  225. func_for_each_insn(file, func, insn) {
  226. empty = false;
  227. if (insn->type == INSN_RETURN)
  228. return false;
  229. }
  230. if (empty)
  231. return false;
  232. /*
  233. * A function can have a sibling call instead of a return. In that
  234. * case, the function's dead-end status depends on whether the target
  235. * of the sibling call returns.
  236. */
  237. func_for_each_insn(file, func, insn) {
  238. if (is_sibling_call(insn)) {
  239. struct instruction *dest = insn->jump_dest;
  240. if (!dest)
  241. /* sibling call to another file */
  242. return false;
  243. /* local sibling call */
  244. if (recursion == 5) {
  245. /*
  246. * Infinite recursion: two functions have
  247. * sibling calls to each other. This is a very
  248. * rare case. It means they aren't dead ends.
  249. */
  250. return false;
  251. }
  252. return __dead_end_function(file, insn_func(dest), recursion+1);
  253. }
  254. }
  255. return true;
  256. }
  257. static bool dead_end_function(struct objtool_file *file, struct symbol *func)
  258. {
  259. return __dead_end_function(file, func, 0);
  260. }
  261. static void init_cfi_state(struct cfi_state *cfi)
  262. {
  263. int i;
  264. for (i = 0; i < CFI_NUM_REGS; i++) {
  265. cfi->regs[i].base = CFI_UNDEFINED;
  266. cfi->vals[i].base = CFI_UNDEFINED;
  267. }
  268. cfi->cfa.base = CFI_UNDEFINED;
  269. cfi->drap_reg = CFI_UNDEFINED;
  270. cfi->drap_offset = -1;
  271. }
  272. static void init_insn_state(struct objtool_file *file, struct insn_state *state,
  273. struct section *sec)
  274. {
  275. memset(state, 0, sizeof(*state));
  276. init_cfi_state(&state->cfi);
  277. /*
  278. * We need the full vmlinux for noinstr validation, otherwise we can
  279. * not correctly determine insn_call_dest(insn)->sec (external symbols
  280. * do not have a section).
  281. */
  282. if (opts.link && opts.noinstr && sec)
  283. state->noinstr = sec->noinstr;
  284. }
  285. static struct cfi_state *cfi_alloc(void)
  286. {
  287. struct cfi_state *cfi = calloc(1, sizeof(struct cfi_state));
  288. if (!cfi) {
  289. WARN("calloc failed");
  290. exit(1);
  291. }
  292. nr_cfi++;
  293. return cfi;
  294. }
  295. static int cfi_bits;
  296. static struct hlist_head *cfi_hash;
  297. static inline bool cficmp(struct cfi_state *cfi1, struct cfi_state *cfi2)
  298. {
  299. return memcmp((void *)cfi1 + sizeof(cfi1->hash),
  300. (void *)cfi2 + sizeof(cfi2->hash),
  301. sizeof(struct cfi_state) - sizeof(struct hlist_node));
  302. }
  303. static inline u32 cfi_key(struct cfi_state *cfi)
  304. {
  305. return jhash((void *)cfi + sizeof(cfi->hash),
  306. sizeof(*cfi) - sizeof(cfi->hash), 0);
  307. }
  308. static struct cfi_state *cfi_hash_find_or_add(struct cfi_state *cfi)
  309. {
  310. struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
  311. struct cfi_state *obj;
  312. hlist_for_each_entry(obj, head, hash) {
  313. if (!cficmp(cfi, obj)) {
  314. nr_cfi_cache++;
  315. return obj;
  316. }
  317. }
  318. obj = cfi_alloc();
  319. *obj = *cfi;
  320. hlist_add_head(&obj->hash, head);
  321. return obj;
  322. }
  323. static void cfi_hash_add(struct cfi_state *cfi)
  324. {
  325. struct hlist_head *head = &cfi_hash[hash_min(cfi_key(cfi), cfi_bits)];
  326. hlist_add_head(&cfi->hash, head);
  327. }
  328. static void *cfi_hash_alloc(unsigned long size)
  329. {
  330. cfi_bits = max(10, ilog2(size));
  331. cfi_hash = mmap(NULL, sizeof(struct hlist_head) << cfi_bits,
  332. PROT_READ|PROT_WRITE,
  333. MAP_PRIVATE|MAP_ANON, -1, 0);
  334. if (cfi_hash == (void *)-1L) {
  335. WARN("mmap fail cfi_hash");
  336. cfi_hash = NULL;
  337. } else if (opts.stats) {
  338. printf("cfi_bits: %d\n", cfi_bits);
  339. }
  340. return cfi_hash;
  341. }
  342. static unsigned long nr_insns;
  343. static unsigned long nr_insns_visited;
  344. /*
  345. * Call the arch-specific instruction decoder for all the instructions and add
  346. * them to the global instruction list.
  347. */
  348. static int decode_instructions(struct objtool_file *file)
  349. {
  350. struct section *sec;
  351. struct symbol *func;
  352. unsigned long offset;
  353. struct instruction *insn;
  354. int ret;
  355. for_each_sec(file, sec) {
  356. struct instruction *insns = NULL;
  357. u8 prev_len = 0;
  358. u8 idx = 0;
  359. if (!(sec->sh.sh_flags & SHF_EXECINSTR))
  360. continue;
  361. if (strcmp(sec->name, ".altinstr_replacement") &&
  362. strcmp(sec->name, ".altinstr_aux") &&
  363. strncmp(sec->name, ".discard.", 9))
  364. sec->text = true;
  365. if (!strcmp(sec->name, ".noinstr.text") ||
  366. !strcmp(sec->name, ".entry.text") ||
  367. !strcmp(sec->name, ".cpuidle.text") ||
  368. !strncmp(sec->name, ".text..__x86.", 13))
  369. sec->noinstr = true;
  370. /*
  371. * .init.text code is ran before userspace and thus doesn't
  372. * strictly need retpolines, except for modules which are
  373. * loaded late, they very much do need retpoline in their
  374. * .init.text
  375. */
  376. if (!strcmp(sec->name, ".init.text") && !opts.module)
  377. sec->init = true;
  378. for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) {
  379. if (!insns || idx == INSN_CHUNK_MAX) {
  380. insns = calloc(sizeof(*insn), INSN_CHUNK_SIZE);
  381. if (!insns) {
  382. WARN("malloc failed");
  383. return -1;
  384. }
  385. idx = 0;
  386. } else {
  387. idx++;
  388. }
  389. insn = &insns[idx];
  390. insn->idx = idx;
  391. INIT_LIST_HEAD(&insn->call_node);
  392. insn->sec = sec;
  393. insn->offset = offset;
  394. insn->prev_len = prev_len;
  395. ret = arch_decode_instruction(file, sec, offset,
  396. sec->sh.sh_size - offset,
  397. insn);
  398. if (ret)
  399. return ret;
  400. prev_len = insn->len;
  401. /*
  402. * By default, "ud2" is a dead end unless otherwise
  403. * annotated, because GCC 7 inserts it for certain
  404. * divide-by-zero cases.
  405. */
  406. if (insn->type == INSN_BUG)
  407. insn->dead_end = true;
  408. hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));
  409. nr_insns++;
  410. }
  411. // printf("%s: last chunk used: %d\n", sec->name, (int)idx);
  412. sec_for_each_sym(sec, func) {
  413. if (func->type != STT_NOTYPE && func->type != STT_FUNC)
  414. continue;
  415. if (func->offset == sec->sh.sh_size) {
  416. /* Heuristic: likely an "end" symbol */
  417. if (func->type == STT_NOTYPE)
  418. continue;
  419. WARN("%s(): STT_FUNC at end of section",
  420. func->name);
  421. return -1;
  422. }
  423. if (func->embedded_insn || func->alias != func)
  424. continue;
  425. if (!find_insn(file, sec, func->offset)) {
  426. WARN("%s(): can't find starting instruction",
  427. func->name);
  428. return -1;
  429. }
  430. sym_for_each_insn(file, func, insn) {
  431. insn->sym = func;
  432. if (func->type == STT_FUNC &&
  433. insn->type == INSN_ENDBR &&
  434. list_empty(&insn->call_node)) {
  435. if (insn->offset == func->offset) {
  436. list_add_tail(&insn->call_node, &file->endbr_list);
  437. file->nr_endbr++;
  438. } else {
  439. file->nr_endbr_int++;
  440. }
  441. }
  442. }
  443. }
  444. }
  445. if (opts.stats)
  446. printf("nr_insns: %lu\n", nr_insns);
  447. return 0;
  448. }
  449. /*
  450. * Read the pv_ops[] .data table to find the static initialized values.
  451. */
  452. static int add_pv_ops(struct objtool_file *file, const char *symname)
  453. {
  454. struct symbol *sym, *func;
  455. unsigned long off, end;
  456. struct reloc *reloc;
  457. int idx;
  458. sym = find_symbol_by_name(file->elf, symname);
  459. if (!sym)
  460. return 0;
  461. off = sym->offset;
  462. end = off + sym->len;
  463. for (;;) {
  464. reloc = find_reloc_by_dest_range(file->elf, sym->sec, off, end - off);
  465. if (!reloc)
  466. break;
  467. func = reloc->sym;
  468. if (func->type == STT_SECTION)
  469. func = find_symbol_by_offset(reloc->sym->sec,
  470. reloc_addend(reloc));
  471. idx = (reloc_offset(reloc) - sym->offset) / sizeof(unsigned long);
  472. objtool_pv_add(file, idx, func);
  473. off = reloc_offset(reloc) + 1;
  474. if (off > end)
  475. break;
  476. }
  477. return 0;
  478. }
  479. /*
  480. * Allocate and initialize file->pv_ops[].
  481. */
  482. static int init_pv_ops(struct objtool_file *file)
  483. {
  484. static const char *pv_ops_tables[] = {
  485. "pv_ops",
  486. "xen_cpu_ops",
  487. "xen_irq_ops",
  488. "xen_mmu_ops",
  489. NULL,
  490. };
  491. const char *pv_ops;
  492. struct symbol *sym;
  493. int idx, nr;
  494. if (!opts.noinstr)
  495. return 0;
  496. file->pv_ops = NULL;
  497. sym = find_symbol_by_name(file->elf, "pv_ops");
  498. if (!sym)
  499. return 0;
  500. nr = sym->len / sizeof(unsigned long);
  501. file->pv_ops = calloc(sizeof(struct pv_state), nr);
  502. if (!file->pv_ops)
  503. return -1;
  504. for (idx = 0; idx < nr; idx++)
  505. INIT_LIST_HEAD(&file->pv_ops[idx].targets);
  506. for (idx = 0; (pv_ops = pv_ops_tables[idx]); idx++)
  507. add_pv_ops(file, pv_ops);
  508. return 0;
  509. }
  510. static struct instruction *find_last_insn(struct objtool_file *file,
  511. struct section *sec)
  512. {
  513. struct instruction *insn = NULL;
  514. unsigned int offset;
  515. unsigned int end = (sec->sh.sh_size > 10) ? sec->sh.sh_size - 10 : 0;
  516. for (offset = sec->sh.sh_size - 1; offset >= end && !insn; offset--)
  517. insn = find_insn(file, sec, offset);
  518. return insn;
  519. }
  520. /*
  521. * Mark "ud2" instructions and manually annotated dead ends.
  522. */
  523. static int add_dead_ends(struct objtool_file *file)
  524. {
  525. struct section *rsec;
  526. struct reloc *reloc;
  527. struct instruction *insn;
  528. uint64_t offset;
  529. /*
  530. * UD2 defaults to being a dead-end, allow them to be annotated for
  531. * non-fatal, eg WARN.
  532. */
  533. rsec = find_section_by_name(file->elf, ".rela.discard.reachable");
  534. if (!rsec)
  535. return 0;
  536. for_each_reloc(rsec, reloc) {
  537. if (reloc->sym->type == STT_SECTION) {
  538. offset = reloc_addend(reloc);
  539. } else if (reloc->sym->local_label) {
  540. offset = reloc->sym->offset;
  541. } else {
  542. WARN("unexpected relocation symbol type in %s", rsec->name);
  543. return -1;
  544. }
  545. insn = find_insn(file, reloc->sym->sec, offset);
  546. if (insn)
  547. insn = prev_insn_same_sec(file, insn);
  548. else if (offset == reloc->sym->sec->sh.sh_size) {
  549. insn = find_last_insn(file, reloc->sym->sec);
  550. if (!insn) {
  551. WARN("can't find reachable insn at %s+0x%" PRIx64,
  552. reloc->sym->sec->name, offset);
  553. return -1;
  554. }
  555. } else {
  556. WARN("can't find reachable insn at %s+0x%" PRIx64,
  557. reloc->sym->sec->name, offset);
  558. return -1;
  559. }
  560. insn->dead_end = false;
  561. }
  562. return 0;
  563. }
  564. static int create_static_call_sections(struct objtool_file *file)
  565. {
  566. struct static_call_site *site;
  567. struct section *sec;
  568. struct instruction *insn;
  569. struct symbol *key_sym;
  570. char *key_name, *tmp;
  571. int idx;
  572. sec = find_section_by_name(file->elf, ".static_call_sites");
  573. if (sec) {
  574. INIT_LIST_HEAD(&file->static_call_list);
  575. WARN("file already has .static_call_sites section, skipping");
  576. return 0;
  577. }
  578. if (list_empty(&file->static_call_list))
  579. return 0;
  580. idx = 0;
  581. list_for_each_entry(insn, &file->static_call_list, call_node)
  582. idx++;
  583. sec = elf_create_section_pair(file->elf, ".static_call_sites",
  584. sizeof(*site), idx, idx * 2);
  585. if (!sec)
  586. return -1;
  587. /* Allow modules to modify the low bits of static_call_site::key */
  588. sec->sh.sh_flags |= SHF_WRITE;
  589. idx = 0;
  590. list_for_each_entry(insn, &file->static_call_list, call_node) {
  591. /* populate reloc for 'addr' */
  592. if (!elf_init_reloc_text_sym(file->elf, sec,
  593. idx * sizeof(*site), idx * 2,
  594. insn->sec, insn->offset))
  595. return -1;
  596. /* find key symbol */
  597. key_name = strdup(insn_call_dest(insn)->name);
  598. if (!key_name) {
  599. perror("strdup");
  600. return -1;
  601. }
  602. if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
  603. STATIC_CALL_TRAMP_PREFIX_LEN)) {
  604. WARN("static_call: trampoline name malformed: %s", key_name);
  605. free(key_name);
  606. return -1;
  607. }
  608. tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
  609. memcpy(tmp, STATIC_CALL_KEY_PREFIX_STR, STATIC_CALL_KEY_PREFIX_LEN);
  610. key_sym = find_symbol_by_name(file->elf, tmp);
  611. if (!key_sym) {
  612. if (!opts.module) {
  613. WARN("static_call: can't find static_call_key symbol: %s", tmp);
  614. free(key_name);
  615. return -1;
  616. }
  617. /*
  618. * For modules(), the key might not be exported, which
  619. * means the module can make static calls but isn't
  620. * allowed to change them.
  621. *
  622. * In that case we temporarily set the key to be the
  623. * trampoline address. This is fixed up in
  624. * static_call_add_module().
  625. */
  626. key_sym = insn_call_dest(insn);
  627. }
  628. free(key_name);
  629. /* populate reloc for 'key' */
  630. if (!elf_init_reloc_data_sym(file->elf, sec,
  631. idx * sizeof(*site) + 4,
  632. (idx * 2) + 1, key_sym,
  633. is_sibling_call(insn) * STATIC_CALL_SITE_TAIL))
  634. return -1;
  635. idx++;
  636. }
  637. return 0;
  638. }
  639. static int create_retpoline_sites_sections(struct objtool_file *file)
  640. {
  641. struct instruction *insn;
  642. struct section *sec;
  643. int idx;
  644. sec = find_section_by_name(file->elf, ".retpoline_sites");
  645. if (sec) {
  646. WARN("file already has .retpoline_sites, skipping");
  647. return 0;
  648. }
  649. idx = 0;
  650. list_for_each_entry(insn, &file->retpoline_call_list, call_node)
  651. idx++;
  652. if (!idx)
  653. return 0;
  654. sec = elf_create_section_pair(file->elf, ".retpoline_sites",
  655. sizeof(int), idx, idx);
  656. if (!sec)
  657. return -1;
  658. idx = 0;
  659. list_for_each_entry(insn, &file->retpoline_call_list, call_node) {
  660. if (!elf_init_reloc_text_sym(file->elf, sec,
  661. idx * sizeof(int), idx,
  662. insn->sec, insn->offset))
  663. return -1;
  664. idx++;
  665. }
  666. return 0;
  667. }
  668. static int create_return_sites_sections(struct objtool_file *file)
  669. {
  670. struct instruction *insn;
  671. struct section *sec;
  672. int idx;
  673. sec = find_section_by_name(file->elf, ".return_sites");
  674. if (sec) {
  675. WARN("file already has .return_sites, skipping");
  676. return 0;
  677. }
  678. idx = 0;
  679. list_for_each_entry(insn, &file->return_thunk_list, call_node)
  680. idx++;
  681. if (!idx)
  682. return 0;
  683. sec = elf_create_section_pair(file->elf, ".return_sites",
  684. sizeof(int), idx, idx);
  685. if (!sec)
  686. return -1;
  687. idx = 0;
  688. list_for_each_entry(insn, &file->return_thunk_list, call_node) {
  689. if (!elf_init_reloc_text_sym(file->elf, sec,
  690. idx * sizeof(int), idx,
  691. insn->sec, insn->offset))
  692. return -1;
  693. idx++;
  694. }
  695. return 0;
  696. }
  697. static int create_ibt_endbr_seal_sections(struct objtool_file *file)
  698. {
  699. struct instruction *insn;
  700. struct section *sec;
  701. int idx;
  702. sec = find_section_by_name(file->elf, ".ibt_endbr_seal");
  703. if (sec) {
  704. WARN("file already has .ibt_endbr_seal, skipping");
  705. return 0;
  706. }
  707. idx = 0;
  708. list_for_each_entry(insn, &file->endbr_list, call_node)
  709. idx++;
  710. if (opts.stats) {
  711. printf("ibt: ENDBR at function start: %d\n", file->nr_endbr);
  712. printf("ibt: ENDBR inside functions: %d\n", file->nr_endbr_int);
  713. printf("ibt: superfluous ENDBR: %d\n", idx);
  714. }
  715. if (!idx)
  716. return 0;
  717. sec = elf_create_section_pair(file->elf, ".ibt_endbr_seal",
  718. sizeof(int), idx, idx);
  719. if (!sec)
  720. return -1;
  721. idx = 0;
  722. list_for_each_entry(insn, &file->endbr_list, call_node) {
  723. int *site = (int *)sec->data->d_buf + idx;
  724. struct symbol *sym = insn->sym;
  725. *site = 0;
  726. if (opts.module && sym && sym->type == STT_FUNC &&
  727. insn->offset == sym->offset &&
  728. (!strcmp(sym->name, "init_module") ||
  729. !strcmp(sym->name, "cleanup_module")))
  730. WARN("%s(): not an indirect call target", sym->name);
  731. if (!elf_init_reloc_text_sym(file->elf, sec,
  732. idx * sizeof(int), idx,
  733. insn->sec, insn->offset))
  734. return -1;
  735. idx++;
  736. }
  737. return 0;
  738. }
  739. static int create_cfi_sections(struct objtool_file *file)
  740. {
  741. struct section *sec;
  742. struct symbol *sym;
  743. int idx;
  744. sec = find_section_by_name(file->elf, ".cfi_sites");
  745. if (sec) {
  746. INIT_LIST_HEAD(&file->call_list);
  747. WARN("file already has .cfi_sites section, skipping");
  748. return 0;
  749. }
  750. idx = 0;
  751. for_each_sym(file, sym) {
  752. if (sym->type != STT_FUNC)
  753. continue;
  754. if (strncmp(sym->name, "__cfi_", 6))
  755. continue;
  756. idx++;
  757. }
  758. sec = elf_create_section_pair(file->elf, ".cfi_sites",
  759. sizeof(unsigned int), idx, idx);
  760. if (!sec)
  761. return -1;
  762. idx = 0;
  763. for_each_sym(file, sym) {
  764. if (sym->type != STT_FUNC)
  765. continue;
  766. if (strncmp(sym->name, "__cfi_", 6))
  767. continue;
  768. if (!elf_init_reloc_text_sym(file->elf, sec,
  769. idx * sizeof(unsigned int), idx,
  770. sym->sec, sym->offset))
  771. return -1;
  772. idx++;
  773. }
  774. return 0;
  775. }
  776. static int create_mcount_loc_sections(struct objtool_file *file)
  777. {
  778. size_t addr_size = elf_addr_size(file->elf);
  779. struct instruction *insn;
  780. struct section *sec;
  781. int idx;
  782. sec = find_section_by_name(file->elf, "__mcount_loc");
  783. if (sec) {
  784. INIT_LIST_HEAD(&file->mcount_loc_list);
  785. WARN("file already has __mcount_loc section, skipping");
  786. return 0;
  787. }
  788. if (list_empty(&file->mcount_loc_list))
  789. return 0;
  790. idx = 0;
  791. list_for_each_entry(insn, &file->mcount_loc_list, call_node)
  792. idx++;
  793. sec = elf_create_section_pair(file->elf, "__mcount_loc", addr_size,
  794. idx, idx);
  795. if (!sec)
  796. return -1;
  797. sec->sh.sh_addralign = addr_size;
  798. idx = 0;
  799. list_for_each_entry(insn, &file->mcount_loc_list, call_node) {
  800. struct reloc *reloc;
  801. reloc = elf_init_reloc_text_sym(file->elf, sec, idx * addr_size, idx,
  802. insn->sec, insn->offset);
  803. if (!reloc)
  804. return -1;
  805. set_reloc_type(file->elf, reloc, addr_size == 8 ? R_ABS64 : R_ABS32);
  806. idx++;
  807. }
  808. return 0;
  809. }
  810. static int create_direct_call_sections(struct objtool_file *file)
  811. {
  812. struct instruction *insn;
  813. struct section *sec;
  814. int idx;
  815. sec = find_section_by_name(file->elf, ".call_sites");
  816. if (sec) {
  817. INIT_LIST_HEAD(&file->call_list);
  818. WARN("file already has .call_sites section, skipping");
  819. return 0;
  820. }
  821. if (list_empty(&file->call_list))
  822. return 0;
  823. idx = 0;
  824. list_for_each_entry(insn, &file->call_list, call_node)
  825. idx++;
  826. sec = elf_create_section_pair(file->elf, ".call_sites",
  827. sizeof(unsigned int), idx, idx);
  828. if (!sec)
  829. return -1;
  830. idx = 0;
  831. list_for_each_entry(insn, &file->call_list, call_node) {
  832. if (!elf_init_reloc_text_sym(file->elf, sec,
  833. idx * sizeof(unsigned int), idx,
  834. insn->sec, insn->offset))
  835. return -1;
  836. idx++;
  837. }
  838. return 0;
  839. }
  840. /*
  841. * Warnings shouldn't be reported for ignored functions.
  842. */
  843. static void add_ignores(struct objtool_file *file)
  844. {
  845. struct instruction *insn;
  846. struct section *rsec;
  847. struct symbol *func;
  848. struct reloc *reloc;
  849. rsec = find_section_by_name(file->elf, ".rela.discard.func_stack_frame_non_standard");
  850. if (!rsec)
  851. return;
  852. for_each_reloc(rsec, reloc) {
  853. switch (reloc->sym->type) {
  854. case STT_FUNC:
  855. func = reloc->sym;
  856. break;
  857. case STT_SECTION:
  858. func = find_func_by_offset(reloc->sym->sec, reloc_addend(reloc));
  859. if (!func)
  860. continue;
  861. break;
  862. default:
  863. WARN("unexpected relocation symbol type in %s: %d",
  864. rsec->name, reloc->sym->type);
  865. continue;
  866. }
  867. func_for_each_insn(file, func, insn)
  868. insn->ignore = true;
  869. }
  870. }
  871. /*
  872. * This is a whitelist of functions that is allowed to be called with AC set.
  873. * The list is meant to be minimal and only contains compiler instrumentation
  874. * ABI and a few functions used to implement *_{to,from}_user() functions.
  875. *
  876. * These functions must not directly change AC, but may PUSHF/POPF.
  877. */
  878. static const char *uaccess_safe_builtin[] = {
  879. /* KASAN */
  880. "kasan_report",
  881. "kasan_check_range",
  882. /* KASAN out-of-line */
  883. "__asan_loadN_noabort",
  884. "__asan_load1_noabort",
  885. "__asan_load2_noabort",
  886. "__asan_load4_noabort",
  887. "__asan_load8_noabort",
  888. "__asan_load16_noabort",
  889. "__asan_storeN_noabort",
  890. "__asan_store1_noabort",
  891. "__asan_store2_noabort",
  892. "__asan_store4_noabort",
  893. "__asan_store8_noabort",
  894. "__asan_store16_noabort",
  895. "__kasan_check_read",
  896. "__kasan_check_write",
  897. /* KASAN in-line */
  898. "__asan_report_load_n_noabort",
  899. "__asan_report_load1_noabort",
  900. "__asan_report_load2_noabort",
  901. "__asan_report_load4_noabort",
  902. "__asan_report_load8_noabort",
  903. "__asan_report_load16_noabort",
  904. "__asan_report_store_n_noabort",
  905. "__asan_report_store1_noabort",
  906. "__asan_report_store2_noabort",
  907. "__asan_report_store4_noabort",
  908. "__asan_report_store8_noabort",
  909. "__asan_report_store16_noabort",
  910. /* KCSAN */
  911. "__kcsan_check_access",
  912. "__kcsan_mb",
  913. "__kcsan_wmb",
  914. "__kcsan_rmb",
  915. "__kcsan_release",
  916. "kcsan_found_watchpoint",
  917. "kcsan_setup_watchpoint",
  918. "kcsan_check_scoped_accesses",
  919. "kcsan_disable_current",
  920. "kcsan_enable_current_nowarn",
  921. /* KCSAN/TSAN */
  922. "__tsan_func_entry",
  923. "__tsan_func_exit",
  924. "__tsan_read_range",
  925. "__tsan_write_range",
  926. "__tsan_read1",
  927. "__tsan_read2",
  928. "__tsan_read4",
  929. "__tsan_read8",
  930. "__tsan_read16",
  931. "__tsan_write1",
  932. "__tsan_write2",
  933. "__tsan_write4",
  934. "__tsan_write8",
  935. "__tsan_write16",
  936. "__tsan_read_write1",
  937. "__tsan_read_write2",
  938. "__tsan_read_write4",
  939. "__tsan_read_write8",
  940. "__tsan_read_write16",
  941. "__tsan_volatile_read1",
  942. "__tsan_volatile_read2",
  943. "__tsan_volatile_read4",
  944. "__tsan_volatile_read8",
  945. "__tsan_volatile_read16",
  946. "__tsan_volatile_write1",
  947. "__tsan_volatile_write2",
  948. "__tsan_volatile_write4",
  949. "__tsan_volatile_write8",
  950. "__tsan_volatile_write16",
  951. "__tsan_atomic8_load",
  952. "__tsan_atomic16_load",
  953. "__tsan_atomic32_load",
  954. "__tsan_atomic64_load",
  955. "__tsan_atomic8_store",
  956. "__tsan_atomic16_store",
  957. "__tsan_atomic32_store",
  958. "__tsan_atomic64_store",
  959. "__tsan_atomic8_exchange",
  960. "__tsan_atomic16_exchange",
  961. "__tsan_atomic32_exchange",
  962. "__tsan_atomic64_exchange",
  963. "__tsan_atomic8_fetch_add",
  964. "__tsan_atomic16_fetch_add",
  965. "__tsan_atomic32_fetch_add",
  966. "__tsan_atomic64_fetch_add",
  967. "__tsan_atomic8_fetch_sub",
  968. "__tsan_atomic16_fetch_sub",
  969. "__tsan_atomic32_fetch_sub",
  970. "__tsan_atomic64_fetch_sub",
  971. "__tsan_atomic8_fetch_and",
  972. "__tsan_atomic16_fetch_and",
  973. "__tsan_atomic32_fetch_and",
  974. "__tsan_atomic64_fetch_and",
  975. "__tsan_atomic8_fetch_or",
  976. "__tsan_atomic16_fetch_or",
  977. "__tsan_atomic32_fetch_or",
  978. "__tsan_atomic64_fetch_or",
  979. "__tsan_atomic8_fetch_xor",
  980. "__tsan_atomic16_fetch_xor",
  981. "__tsan_atomic32_fetch_xor",
  982. "__tsan_atomic64_fetch_xor",
  983. "__tsan_atomic8_fetch_nand",
  984. "__tsan_atomic16_fetch_nand",
  985. "__tsan_atomic32_fetch_nand",
  986. "__tsan_atomic64_fetch_nand",
  987. "__tsan_atomic8_compare_exchange_strong",
  988. "__tsan_atomic16_compare_exchange_strong",
  989. "__tsan_atomic32_compare_exchange_strong",
  990. "__tsan_atomic64_compare_exchange_strong",
  991. "__tsan_atomic8_compare_exchange_weak",
  992. "__tsan_atomic16_compare_exchange_weak",
  993. "__tsan_atomic32_compare_exchange_weak",
  994. "__tsan_atomic64_compare_exchange_weak",
  995. "__tsan_atomic8_compare_exchange_val",
  996. "__tsan_atomic16_compare_exchange_val",
  997. "__tsan_atomic32_compare_exchange_val",
  998. "__tsan_atomic64_compare_exchange_val",
  999. "__tsan_atomic_thread_fence",
  1000. "__tsan_atomic_signal_fence",
  1001. "__tsan_unaligned_read16",
  1002. "__tsan_unaligned_write16",
  1003. /* KCOV */
  1004. "write_comp_data",
  1005. "check_kcov_mode",
  1006. "__sanitizer_cov_trace_pc",
  1007. "__sanitizer_cov_trace_const_cmp1",
  1008. "__sanitizer_cov_trace_const_cmp2",
  1009. "__sanitizer_cov_trace_const_cmp4",
  1010. "__sanitizer_cov_trace_const_cmp8",
  1011. "__sanitizer_cov_trace_cmp1",
  1012. "__sanitizer_cov_trace_cmp2",
  1013. "__sanitizer_cov_trace_cmp4",
  1014. "__sanitizer_cov_trace_cmp8",
  1015. "__sanitizer_cov_trace_switch",
  1016. /* KMSAN */
  1017. "kmsan_copy_to_user",
  1018. "kmsan_disable_current",
  1019. "kmsan_enable_current",
  1020. "kmsan_report",
  1021. "kmsan_unpoison_entry_regs",
  1022. "kmsan_unpoison_memory",
  1023. "__msan_chain_origin",
  1024. "__msan_get_context_state",
  1025. "__msan_instrument_asm_store",
  1026. "__msan_metadata_ptr_for_load_1",
  1027. "__msan_metadata_ptr_for_load_2",
  1028. "__msan_metadata_ptr_for_load_4",
  1029. "__msan_metadata_ptr_for_load_8",
  1030. "__msan_metadata_ptr_for_load_n",
  1031. "__msan_metadata_ptr_for_store_1",
  1032. "__msan_metadata_ptr_for_store_2",
  1033. "__msan_metadata_ptr_for_store_4",
  1034. "__msan_metadata_ptr_for_store_8",
  1035. "__msan_metadata_ptr_for_store_n",
  1036. "__msan_poison_alloca",
  1037. "__msan_warning",
  1038. /* UBSAN */
  1039. "ubsan_type_mismatch_common",
  1040. "__ubsan_handle_type_mismatch",
  1041. "__ubsan_handle_type_mismatch_v1",
  1042. "__ubsan_handle_shift_out_of_bounds",
  1043. "__ubsan_handle_load_invalid_value",
  1044. /* STACKLEAK */
  1045. "stackleak_track_stack",
  1046. /* TRACE_BRANCH_PROFILING */
  1047. "ftrace_likely_update",
  1048. /* STACKPROTECTOR */
  1049. "__stack_chk_fail",
  1050. /* misc */
  1051. "csum_partial_copy_generic",
  1052. "copy_mc_fragile",
  1053. "copy_mc_fragile_handle_tail",
  1054. "copy_mc_enhanced_fast_string",
  1055. "rep_stos_alternative",
  1056. "rep_movs_alternative",
  1057. "__copy_user_nocache",
  1058. NULL
  1059. };
  1060. static void add_uaccess_safe(struct objtool_file *file)
  1061. {
  1062. struct symbol *func;
  1063. const char **name;
  1064. if (!opts.uaccess)
  1065. return;
  1066. for (name = uaccess_safe_builtin; *name; name++) {
  1067. func = find_symbol_by_name(file->elf, *name);
  1068. if (!func)
  1069. continue;
  1070. func->uaccess_safe = true;
  1071. }
  1072. }
  1073. /*
  1074. * FIXME: For now, just ignore any alternatives which add retpolines. This is
  1075. * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
  1076. * But it at least allows objtool to understand the control flow *around* the
  1077. * retpoline.
  1078. */
  1079. static int add_ignore_alternatives(struct objtool_file *file)
  1080. {
  1081. struct section *rsec;
  1082. struct reloc *reloc;
  1083. struct instruction *insn;
  1084. rsec = find_section_by_name(file->elf, ".rela.discard.ignore_alts");
  1085. if (!rsec)
  1086. return 0;
  1087. for_each_reloc(rsec, reloc) {
  1088. if (reloc->sym->type != STT_SECTION) {
  1089. WARN("unexpected relocation symbol type in %s", rsec->name);
  1090. return -1;
  1091. }
  1092. insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
  1093. if (!insn) {
  1094. WARN("bad .discard.ignore_alts entry");
  1095. return -1;
  1096. }
  1097. insn->ignore_alts = true;
  1098. }
  1099. return 0;
  1100. }
  1101. /*
  1102. * Symbols that replace INSN_CALL_DYNAMIC, every (tail) call to such a symbol
  1103. * will be added to the .retpoline_sites section.
  1104. */
  1105. __weak bool arch_is_retpoline(struct symbol *sym)
  1106. {
  1107. return false;
  1108. }
  1109. /*
  1110. * Symbols that replace INSN_RETURN, every (tail) call to such a symbol
  1111. * will be added to the .return_sites section.
  1112. */
  1113. __weak bool arch_is_rethunk(struct symbol *sym)
  1114. {
  1115. return false;
  1116. }
  1117. /*
  1118. * Symbols that are embedded inside other instructions, because sometimes crazy
  1119. * code exists. These are mostly ignored for validation purposes.
  1120. */
  1121. __weak bool arch_is_embedded_insn(struct symbol *sym)
  1122. {
  1123. return false;
  1124. }
  1125. static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn)
  1126. {
  1127. struct reloc *reloc;
  1128. if (insn->no_reloc)
  1129. return NULL;
  1130. if (!file)
  1131. return NULL;
  1132. reloc = find_reloc_by_dest_range(file->elf, insn->sec,
  1133. insn->offset, insn->len);
  1134. if (!reloc) {
  1135. insn->no_reloc = 1;
  1136. return NULL;
  1137. }
  1138. return reloc;
  1139. }
  1140. static void remove_insn_ops(struct instruction *insn)
  1141. {
  1142. struct stack_op *op, *next;
  1143. for (op = insn->stack_ops; op; op = next) {
  1144. next = op->next;
  1145. free(op);
  1146. }
  1147. insn->stack_ops = NULL;
  1148. }
  1149. static void annotate_call_site(struct objtool_file *file,
  1150. struct instruction *insn, bool sibling)
  1151. {
  1152. struct reloc *reloc = insn_reloc(file, insn);
  1153. struct symbol *sym = insn_call_dest(insn);
  1154. if (!sym)
  1155. sym = reloc->sym;
  1156. /*
  1157. * Alternative replacement code is just template code which is
  1158. * sometimes copied to the original instruction. For now, don't
  1159. * annotate it. (In the future we might consider annotating the
  1160. * original instruction if/when it ever makes sense to do so.)
  1161. */
  1162. if (!strcmp(insn->sec->name, ".altinstr_replacement"))
  1163. return;
  1164. if (sym->static_call_tramp) {
  1165. list_add_tail(&insn->call_node, &file->static_call_list);
  1166. return;
  1167. }
  1168. if (sym->retpoline_thunk) {
  1169. list_add_tail(&insn->call_node, &file->retpoline_call_list);
  1170. return;
  1171. }
  1172. /*
  1173. * Many compilers cannot disable KCOV or sanitizer calls with a function
  1174. * attribute so they need a little help, NOP out any such calls from
  1175. * noinstr text.
  1176. */
  1177. if (opts.hack_noinstr && insn->sec->noinstr && sym->profiling_func) {
  1178. if (reloc)
  1179. set_reloc_type(file->elf, reloc, R_NONE);
  1180. elf_write_insn(file->elf, insn->sec,
  1181. insn->offset, insn->len,
  1182. sibling ? arch_ret_insn(insn->len)
  1183. : arch_nop_insn(insn->len));
  1184. insn->type = sibling ? INSN_RETURN : INSN_NOP;
  1185. if (sibling) {
  1186. /*
  1187. * We've replaced the tail-call JMP insn by two new
  1188. * insn: RET; INT3, except we only have a single struct
  1189. * insn here. Mark it retpoline_safe to avoid the SLS
  1190. * warning, instead of adding another insn.
  1191. */
  1192. insn->retpoline_safe = true;
  1193. }
  1194. return;
  1195. }
  1196. if (opts.mcount && sym->fentry) {
  1197. if (sibling)
  1198. WARN_INSN(insn, "tail call to __fentry__ !?!?");
  1199. if (opts.mnop) {
  1200. if (reloc)
  1201. set_reloc_type(file->elf, reloc, R_NONE);
  1202. elf_write_insn(file->elf, insn->sec,
  1203. insn->offset, insn->len,
  1204. arch_nop_insn(insn->len));
  1205. insn->type = INSN_NOP;
  1206. }
  1207. list_add_tail(&insn->call_node, &file->mcount_loc_list);
  1208. return;
  1209. }
  1210. if (insn->type == INSN_CALL && !insn->sec->init)
  1211. list_add_tail(&insn->call_node, &file->call_list);
  1212. if (!sibling && dead_end_function(file, sym))
  1213. insn->dead_end = true;
  1214. }
  1215. static void add_call_dest(struct objtool_file *file, struct instruction *insn,
  1216. struct symbol *dest, bool sibling)
  1217. {
  1218. insn->_call_dest = dest;
  1219. if (!dest)
  1220. return;
  1221. /*
  1222. * Whatever stack impact regular CALLs have, should be undone
  1223. * by the RETURN of the called function.
  1224. *
  1225. * Annotated intra-function calls retain the stack_ops but
  1226. * are converted to JUMP, see read_intra_function_calls().
  1227. */
  1228. remove_insn_ops(insn);
  1229. annotate_call_site(file, insn, sibling);
  1230. }
  1231. static void add_retpoline_call(struct objtool_file *file, struct instruction *insn)
  1232. {
  1233. /*
  1234. * Retpoline calls/jumps are really dynamic calls/jumps in disguise,
  1235. * so convert them accordingly.
  1236. */
  1237. switch (insn->type) {
  1238. case INSN_CALL:
  1239. insn->type = INSN_CALL_DYNAMIC;
  1240. break;
  1241. case INSN_JUMP_UNCONDITIONAL:
  1242. insn->type = INSN_JUMP_DYNAMIC;
  1243. break;
  1244. case INSN_JUMP_CONDITIONAL:
  1245. insn->type = INSN_JUMP_DYNAMIC_CONDITIONAL;
  1246. break;
  1247. default:
  1248. return;
  1249. }
  1250. insn->retpoline_safe = true;
  1251. /*
  1252. * Whatever stack impact regular CALLs have, should be undone
  1253. * by the RETURN of the called function.
  1254. *
  1255. * Annotated intra-function calls retain the stack_ops but
  1256. * are converted to JUMP, see read_intra_function_calls().
  1257. */
  1258. remove_insn_ops(insn);
  1259. annotate_call_site(file, insn, false);
  1260. }
  1261. static void add_return_call(struct objtool_file *file, struct instruction *insn, bool add)
  1262. {
  1263. /*
  1264. * Return thunk tail calls are really just returns in disguise,
  1265. * so convert them accordingly.
  1266. */
  1267. insn->type = INSN_RETURN;
  1268. insn->retpoline_safe = true;
  1269. if (add)
  1270. list_add_tail(&insn->call_node, &file->return_thunk_list);
  1271. }
  1272. static bool is_first_func_insn(struct objtool_file *file,
  1273. struct instruction *insn, struct symbol *sym)
  1274. {
  1275. if (insn->offset == sym->offset)
  1276. return true;
  1277. /* Allow direct CALL/JMP past ENDBR */
  1278. if (opts.ibt) {
  1279. struct instruction *prev = prev_insn_same_sym(file, insn);
  1280. if (prev && prev->type == INSN_ENDBR &&
  1281. insn->offset == sym->offset + prev->len)
  1282. return true;
  1283. }
  1284. return false;
  1285. }
  1286. /*
  1287. * A sibling call is a tail-call to another symbol -- to differentiate from a
  1288. * recursive tail-call which is to the same symbol.
  1289. */
  1290. static bool jump_is_sibling_call(struct objtool_file *file,
  1291. struct instruction *from, struct instruction *to)
  1292. {
  1293. struct symbol *fs = from->sym;
  1294. struct symbol *ts = to->sym;
  1295. /* Not a sibling call if from/to a symbol hole */
  1296. if (!fs || !ts)
  1297. return false;
  1298. /* Not a sibling call if not targeting the start of a symbol. */
  1299. if (!is_first_func_insn(file, to, ts))
  1300. return false;
  1301. /* Disallow sibling calls into STT_NOTYPE */
  1302. if (ts->type == STT_NOTYPE)
  1303. return false;
  1304. /* Must not be self to be a sibling */
  1305. return fs->pfunc != ts->pfunc;
  1306. }
  1307. /*
  1308. * Find the destination instructions for all jumps.
  1309. */
  1310. static int add_jump_destinations(struct objtool_file *file)
  1311. {
  1312. struct instruction *insn, *jump_dest;
  1313. struct reloc *reloc;
  1314. struct section *dest_sec;
  1315. unsigned long dest_off;
  1316. for_each_insn(file, insn) {
  1317. struct symbol *func = insn_func(insn);
  1318. if (insn->jump_dest) {
  1319. /*
  1320. * handle_group_alt() may have previously set
  1321. * 'jump_dest' for some alternatives.
  1322. */
  1323. continue;
  1324. }
  1325. if (!is_static_jump(insn))
  1326. continue;
  1327. reloc = insn_reloc(file, insn);
  1328. if (!reloc) {
  1329. dest_sec = insn->sec;
  1330. dest_off = arch_jump_destination(insn);
  1331. } else if (reloc->sym->type == STT_SECTION) {
  1332. dest_sec = reloc->sym->sec;
  1333. dest_off = arch_dest_reloc_offset(reloc_addend(reloc));
  1334. } else if (reloc->sym->retpoline_thunk) {
  1335. add_retpoline_call(file, insn);
  1336. continue;
  1337. } else if (reloc->sym->return_thunk) {
  1338. add_return_call(file, insn, true);
  1339. continue;
  1340. } else if (func) {
  1341. /*
  1342. * External sibling call or internal sibling call with
  1343. * STT_FUNC reloc.
  1344. */
  1345. add_call_dest(file, insn, reloc->sym, true);
  1346. continue;
  1347. } else if (reloc->sym->sec->idx) {
  1348. dest_sec = reloc->sym->sec;
  1349. dest_off = reloc->sym->sym.st_value +
  1350. arch_dest_reloc_offset(reloc_addend(reloc));
  1351. } else {
  1352. /* non-func asm code jumping to another file */
  1353. continue;
  1354. }
  1355. jump_dest = find_insn(file, dest_sec, dest_off);
  1356. if (!jump_dest) {
  1357. struct symbol *sym = find_symbol_by_offset(dest_sec, dest_off);
  1358. /*
  1359. * This is a special case for retbleed_untrain_ret().
  1360. * It jumps to __x86_return_thunk(), but objtool
  1361. * can't find the thunk's starting RET
  1362. * instruction, because the RET is also in the
  1363. * middle of another instruction. Objtool only
  1364. * knows about the outer instruction.
  1365. */
  1366. if (sym && sym->embedded_insn) {
  1367. add_return_call(file, insn, false);
  1368. continue;
  1369. }
  1370. /*
  1371. * GCOV/KCOV dead code can jump to the end of the
  1372. * function/section.
  1373. */
  1374. if (file->ignore_unreachables && func &&
  1375. dest_sec == insn->sec &&
  1376. dest_off == func->offset + func->len)
  1377. continue;
  1378. WARN_INSN(insn, "can't find jump dest instruction at %s+0x%lx",
  1379. dest_sec->name, dest_off);
  1380. return -1;
  1381. }
  1382. /*
  1383. * An intra-TU jump in retpoline.o might not have a relocation
  1384. * for its jump dest, in which case the above
  1385. * add_{retpoline,return}_call() didn't happen.
  1386. */
  1387. if (jump_dest->sym && jump_dest->offset == jump_dest->sym->offset) {
  1388. if (jump_dest->sym->retpoline_thunk) {
  1389. add_retpoline_call(file, insn);
  1390. continue;
  1391. }
  1392. if (jump_dest->sym->return_thunk) {
  1393. add_return_call(file, insn, true);
  1394. continue;
  1395. }
  1396. }
  1397. /*
  1398. * Cross-function jump.
  1399. */
  1400. if (func && insn_func(jump_dest) && func != insn_func(jump_dest)) {
  1401. /*
  1402. * For GCC 8+, create parent/child links for any cold
  1403. * subfunctions. This is _mostly_ redundant with a
  1404. * similar initialization in read_symbols().
  1405. *
  1406. * If a function has aliases, we want the *first* such
  1407. * function in the symbol table to be the subfunction's
  1408. * parent. In that case we overwrite the
  1409. * initialization done in read_symbols().
  1410. *
  1411. * However this code can't completely replace the
  1412. * read_symbols() code because this doesn't detect the
  1413. * case where the parent function's only reference to a
  1414. * subfunction is through a jump table.
  1415. */
  1416. if (!strstr(func->name, ".cold") &&
  1417. strstr(insn_func(jump_dest)->name, ".cold")) {
  1418. func->cfunc = insn_func(jump_dest);
  1419. insn_func(jump_dest)->pfunc = func;
  1420. }
  1421. }
  1422. if (jump_is_sibling_call(file, insn, jump_dest)) {
  1423. /*
  1424. * Internal sibling call without reloc or with
  1425. * STT_SECTION reloc.
  1426. */
  1427. add_call_dest(file, insn, insn_func(jump_dest), true);
  1428. continue;
  1429. }
  1430. insn->jump_dest = jump_dest;
  1431. }
  1432. return 0;
  1433. }
  1434. static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
  1435. {
  1436. struct symbol *call_dest;
  1437. call_dest = find_func_by_offset(sec, offset);
  1438. if (!call_dest)
  1439. call_dest = find_symbol_by_offset(sec, offset);
  1440. return call_dest;
  1441. }
  1442. /*
  1443. * Find the destination instructions for all calls.
  1444. */
  1445. static int add_call_destinations(struct objtool_file *file)
  1446. {
  1447. struct instruction *insn;
  1448. unsigned long dest_off;
  1449. struct symbol *dest;
  1450. struct reloc *reloc;
  1451. for_each_insn(file, insn) {
  1452. if (insn->type != INSN_CALL)
  1453. continue;
  1454. reloc = insn_reloc(file, insn);
  1455. if (!reloc) {
  1456. dest_off = arch_jump_destination(insn);
  1457. dest = find_call_destination(insn->sec, dest_off);
  1458. add_call_dest(file, insn, dest, false);
  1459. if (insn->ignore)
  1460. continue;
  1461. if (!insn_call_dest(insn)) {
  1462. WARN_INSN(insn, "unannotated intra-function call");
  1463. return -1;
  1464. }
  1465. if (insn_func(insn) && insn_call_dest(insn)->type != STT_FUNC) {
  1466. WARN_INSN(insn, "unsupported call to non-function");
  1467. return -1;
  1468. }
  1469. } else if (reloc->sym->type == STT_SECTION) {
  1470. dest_off = arch_dest_reloc_offset(reloc_addend(reloc));
  1471. dest = find_call_destination(reloc->sym->sec, dest_off);
  1472. if (!dest) {
  1473. WARN_INSN(insn, "can't find call dest symbol at %s+0x%lx",
  1474. reloc->sym->sec->name, dest_off);
  1475. return -1;
  1476. }
  1477. add_call_dest(file, insn, dest, false);
  1478. } else if (reloc->sym->retpoline_thunk) {
  1479. add_retpoline_call(file, insn);
  1480. } else
  1481. add_call_dest(file, insn, reloc->sym, false);
  1482. }
  1483. return 0;
  1484. }
  1485. /*
  1486. * The .alternatives section requires some extra special care over and above
  1487. * other special sections because alternatives are patched in place.
  1488. */
  1489. static int handle_group_alt(struct objtool_file *file,
  1490. struct special_alt *special_alt,
  1491. struct instruction *orig_insn,
  1492. struct instruction **new_insn)
  1493. {
  1494. struct instruction *last_new_insn = NULL, *insn, *nop = NULL;
  1495. struct alt_group *orig_alt_group, *new_alt_group;
  1496. unsigned long dest_off;
  1497. orig_alt_group = orig_insn->alt_group;
  1498. if (!orig_alt_group) {
  1499. struct instruction *last_orig_insn = NULL;
  1500. orig_alt_group = malloc(sizeof(*orig_alt_group));
  1501. if (!orig_alt_group) {
  1502. WARN("malloc failed");
  1503. return -1;
  1504. }
  1505. orig_alt_group->cfi = calloc(special_alt->orig_len,
  1506. sizeof(struct cfi_state *));
  1507. if (!orig_alt_group->cfi) {
  1508. WARN("calloc failed");
  1509. return -1;
  1510. }
  1511. insn = orig_insn;
  1512. sec_for_each_insn_from(file, insn) {
  1513. if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
  1514. break;
  1515. insn->alt_group = orig_alt_group;
  1516. last_orig_insn = insn;
  1517. }
  1518. orig_alt_group->orig_group = NULL;
  1519. orig_alt_group->first_insn = orig_insn;
  1520. orig_alt_group->last_insn = last_orig_insn;
  1521. orig_alt_group->nop = NULL;
  1522. } else {
  1523. if (orig_alt_group->last_insn->offset + orig_alt_group->last_insn->len -
  1524. orig_alt_group->first_insn->offset != special_alt->orig_len) {
  1525. WARN_INSN(orig_insn, "weirdly overlapping alternative! %ld != %d",
  1526. orig_alt_group->last_insn->offset +
  1527. orig_alt_group->last_insn->len -
  1528. orig_alt_group->first_insn->offset,
  1529. special_alt->orig_len);
  1530. return -1;
  1531. }
  1532. }
  1533. new_alt_group = malloc(sizeof(*new_alt_group));
  1534. if (!new_alt_group) {
  1535. WARN("malloc failed");
  1536. return -1;
  1537. }
  1538. if (special_alt->new_len < special_alt->orig_len) {
  1539. /*
  1540. * Insert a fake nop at the end to make the replacement
  1541. * alt_group the same size as the original. This is needed to
  1542. * allow propagate_alt_cfi() to do its magic. When the last
  1543. * instruction affects the stack, the instruction after it (the
  1544. * nop) will propagate the new state to the shared CFI array.
  1545. */
  1546. nop = malloc(sizeof(*nop));
  1547. if (!nop) {
  1548. WARN("malloc failed");
  1549. return -1;
  1550. }
  1551. memset(nop, 0, sizeof(*nop));
  1552. nop->sec = special_alt->new_sec;
  1553. nop->offset = special_alt->new_off + special_alt->new_len;
  1554. nop->len = special_alt->orig_len - special_alt->new_len;
  1555. nop->type = INSN_NOP;
  1556. nop->sym = orig_insn->sym;
  1557. nop->alt_group = new_alt_group;
  1558. nop->ignore = orig_insn->ignore_alts;
  1559. }
  1560. if (!special_alt->new_len) {
  1561. *new_insn = nop;
  1562. goto end;
  1563. }
  1564. insn = *new_insn;
  1565. sec_for_each_insn_from(file, insn) {
  1566. struct reloc *alt_reloc;
  1567. if (insn->offset >= special_alt->new_off + special_alt->new_len)
  1568. break;
  1569. last_new_insn = insn;
  1570. insn->ignore = orig_insn->ignore_alts;
  1571. insn->sym = orig_insn->sym;
  1572. insn->alt_group = new_alt_group;
  1573. /*
  1574. * Since alternative replacement code is copy/pasted by the
  1575. * kernel after applying relocations, generally such code can't
  1576. * have relative-address relocation references to outside the
  1577. * .altinstr_replacement section, unless the arch's
  1578. * alternatives code can adjust the relative offsets
  1579. * accordingly.
  1580. */
  1581. alt_reloc = insn_reloc(file, insn);
  1582. if (alt_reloc && arch_pc_relative_reloc(alt_reloc) &&
  1583. !arch_support_alt_relocation(special_alt, insn, alt_reloc)) {
  1584. WARN_INSN(insn, "unsupported relocation in alternatives section");
  1585. return -1;
  1586. }
  1587. if (!is_static_jump(insn))
  1588. continue;
  1589. if (!insn->immediate)
  1590. continue;
  1591. dest_off = arch_jump_destination(insn);
  1592. if (dest_off == special_alt->new_off + special_alt->new_len) {
  1593. insn->jump_dest = next_insn_same_sec(file, orig_alt_group->last_insn);
  1594. if (!insn->jump_dest) {
  1595. WARN_INSN(insn, "can't find alternative jump destination");
  1596. return -1;
  1597. }
  1598. }
  1599. }
  1600. if (!last_new_insn) {
  1601. WARN_FUNC("can't find last new alternative instruction",
  1602. special_alt->new_sec, special_alt->new_off);
  1603. return -1;
  1604. }
  1605. end:
  1606. new_alt_group->orig_group = orig_alt_group;
  1607. new_alt_group->first_insn = *new_insn;
  1608. new_alt_group->last_insn = last_new_insn;
  1609. new_alt_group->nop = nop;
  1610. new_alt_group->cfi = orig_alt_group->cfi;
  1611. return 0;
  1612. }
  1613. /*
  1614. * A jump table entry can either convert a nop to a jump or a jump to a nop.
  1615. * If the original instruction is a jump, make the alt entry an effective nop
  1616. * by just skipping the original instruction.
  1617. */
  1618. static int handle_jump_alt(struct objtool_file *file,
  1619. struct special_alt *special_alt,
  1620. struct instruction *orig_insn,
  1621. struct instruction **new_insn)
  1622. {
  1623. if (orig_insn->type != INSN_JUMP_UNCONDITIONAL &&
  1624. orig_insn->type != INSN_NOP) {
  1625. WARN_INSN(orig_insn, "unsupported instruction at jump label");
  1626. return -1;
  1627. }
  1628. if (opts.hack_jump_label && special_alt->key_addend & 2) {
  1629. struct reloc *reloc = insn_reloc(file, orig_insn);
  1630. if (reloc)
  1631. set_reloc_type(file->elf, reloc, R_NONE);
  1632. elf_write_insn(file->elf, orig_insn->sec,
  1633. orig_insn->offset, orig_insn->len,
  1634. arch_nop_insn(orig_insn->len));
  1635. orig_insn->type = INSN_NOP;
  1636. }
  1637. if (orig_insn->type == INSN_NOP) {
  1638. if (orig_insn->len == 2)
  1639. file->jl_nop_short++;
  1640. else
  1641. file->jl_nop_long++;
  1642. return 0;
  1643. }
  1644. if (orig_insn->len == 2)
  1645. file->jl_short++;
  1646. else
  1647. file->jl_long++;
  1648. *new_insn = next_insn_same_sec(file, orig_insn);
  1649. return 0;
  1650. }
  1651. /*
  1652. * Read all the special sections which have alternate instructions which can be
  1653. * patched in or redirected to at runtime. Each instruction having alternate
  1654. * instruction(s) has them added to its insn->alts list, which will be
  1655. * traversed in validate_branch().
  1656. */
  1657. static int add_special_section_alts(struct objtool_file *file)
  1658. {
  1659. struct list_head special_alts;
  1660. struct instruction *orig_insn, *new_insn;
  1661. struct special_alt *special_alt, *tmp;
  1662. struct alternative *alt;
  1663. int ret;
  1664. ret = special_get_alts(file->elf, &special_alts);
  1665. if (ret)
  1666. return ret;
  1667. list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
  1668. orig_insn = find_insn(file, special_alt->orig_sec,
  1669. special_alt->orig_off);
  1670. if (!orig_insn) {
  1671. WARN_FUNC("special: can't find orig instruction",
  1672. special_alt->orig_sec, special_alt->orig_off);
  1673. ret = -1;
  1674. goto out;
  1675. }
  1676. new_insn = NULL;
  1677. if (!special_alt->group || special_alt->new_len) {
  1678. new_insn = find_insn(file, special_alt->new_sec,
  1679. special_alt->new_off);
  1680. if (!new_insn) {
  1681. WARN_FUNC("special: can't find new instruction",
  1682. special_alt->new_sec,
  1683. special_alt->new_off);
  1684. ret = -1;
  1685. goto out;
  1686. }
  1687. }
  1688. if (special_alt->group) {
  1689. if (!special_alt->orig_len) {
  1690. WARN_INSN(orig_insn, "empty alternative entry");
  1691. continue;
  1692. }
  1693. ret = handle_group_alt(file, special_alt, orig_insn,
  1694. &new_insn);
  1695. if (ret)
  1696. goto out;
  1697. } else if (special_alt->jump_or_nop) {
  1698. ret = handle_jump_alt(file, special_alt, orig_insn,
  1699. &new_insn);
  1700. if (ret)
  1701. goto out;
  1702. }
  1703. alt = malloc(sizeof(*alt));
  1704. if (!alt) {
  1705. WARN("malloc failed");
  1706. ret = -1;
  1707. goto out;
  1708. }
  1709. alt->insn = new_insn;
  1710. alt->skip_orig = special_alt->skip_orig;
  1711. orig_insn->ignore_alts |= special_alt->skip_alt;
  1712. alt->next = orig_insn->alts;
  1713. orig_insn->alts = alt;
  1714. list_del(&special_alt->list);
  1715. free(special_alt);
  1716. }
  1717. if (opts.stats) {
  1718. printf("jl\\\tNOP\tJMP\n");
  1719. printf("short:\t%ld\t%ld\n", file->jl_nop_short, file->jl_short);
  1720. printf("long:\t%ld\t%ld\n", file->jl_nop_long, file->jl_long);
  1721. }
  1722. out:
  1723. return ret;
  1724. }
  1725. static int add_jump_table(struct objtool_file *file, struct instruction *insn,
  1726. struct reloc *next_table)
  1727. {
  1728. struct symbol *pfunc = insn_func(insn)->pfunc;
  1729. struct reloc *table = insn_jump_table(insn);
  1730. struct instruction *dest_insn;
  1731. unsigned int prev_offset = 0;
  1732. struct reloc *reloc = table;
  1733. struct alternative *alt;
  1734. /*
  1735. * Each @reloc is a switch table relocation which points to the target
  1736. * instruction.
  1737. */
  1738. for_each_reloc_from(table->sec, reloc) {
  1739. /* Check for the end of the table: */
  1740. if (reloc != table && reloc == next_table)
  1741. break;
  1742. /* Make sure the table entries are consecutive: */
  1743. if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
  1744. break;
  1745. /* Detect function pointers from contiguous objects: */
  1746. if (reloc->sym->sec == pfunc->sec &&
  1747. reloc_addend(reloc) == pfunc->offset)
  1748. break;
  1749. /*
  1750. * Clang sometimes leaves dangling unused jump table entries
  1751. * which point to the end of the function. Ignore them.
  1752. */
  1753. if (reloc->sym->sec == pfunc->sec &&
  1754. reloc_addend(reloc) == pfunc->offset + pfunc->len)
  1755. goto next;
  1756. dest_insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
  1757. if (!dest_insn)
  1758. break;
  1759. /* Make sure the destination is in the same function: */
  1760. if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc != pfunc)
  1761. break;
  1762. alt = malloc(sizeof(*alt));
  1763. if (!alt) {
  1764. WARN("malloc failed");
  1765. return -1;
  1766. }
  1767. alt->insn = dest_insn;
  1768. alt->next = insn->alts;
  1769. insn->alts = alt;
  1770. next:
  1771. prev_offset = reloc_offset(reloc);
  1772. }
  1773. if (!prev_offset) {
  1774. WARN_INSN(insn, "can't find switch jump table");
  1775. return -1;
  1776. }
  1777. return 0;
  1778. }
  1779. /*
  1780. * find_jump_table() - Given a dynamic jump, find the switch jump table
  1781. * associated with it.
  1782. */
  1783. static struct reloc *find_jump_table(struct objtool_file *file,
  1784. struct symbol *func,
  1785. struct instruction *insn)
  1786. {
  1787. struct reloc *table_reloc;
  1788. struct instruction *dest_insn, *orig_insn = insn;
  1789. /*
  1790. * Backward search using the @first_jump_src links, these help avoid
  1791. * much of the 'in between' code. Which avoids us getting confused by
  1792. * it.
  1793. */
  1794. for (;
  1795. insn && insn_func(insn) && insn_func(insn)->pfunc == func;
  1796. insn = insn->first_jump_src ?: prev_insn_same_sym(file, insn)) {
  1797. if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
  1798. break;
  1799. /* allow small jumps within the range */
  1800. if (insn->type == INSN_JUMP_UNCONDITIONAL &&
  1801. insn->jump_dest &&
  1802. (insn->jump_dest->offset <= insn->offset ||
  1803. insn->jump_dest->offset > orig_insn->offset))
  1804. break;
  1805. table_reloc = arch_find_switch_table(file, insn);
  1806. if (!table_reloc)
  1807. continue;
  1808. dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc));
  1809. if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func)
  1810. continue;
  1811. return table_reloc;
  1812. }
  1813. return NULL;
  1814. }
  1815. /*
  1816. * First pass: Mark the head of each jump table so that in the next pass,
  1817. * we know when a given jump table ends and the next one starts.
  1818. */
  1819. static void mark_func_jump_tables(struct objtool_file *file,
  1820. struct symbol *func)
  1821. {
  1822. struct instruction *insn, *last = NULL;
  1823. struct reloc *reloc;
  1824. func_for_each_insn(file, func, insn) {
  1825. if (!last)
  1826. last = insn;
  1827. /*
  1828. * Store back-pointers for unconditional forward jumps such
  1829. * that find_jump_table() can back-track using those and
  1830. * avoid some potentially confusing code.
  1831. */
  1832. if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
  1833. insn->offset > last->offset &&
  1834. insn->jump_dest->offset > insn->offset &&
  1835. !insn->jump_dest->first_jump_src) {
  1836. insn->jump_dest->first_jump_src = insn;
  1837. last = insn->jump_dest;
  1838. }
  1839. if (insn->type != INSN_JUMP_DYNAMIC)
  1840. continue;
  1841. reloc = find_jump_table(file, func, insn);
  1842. if (reloc)
  1843. insn->_jump_table = reloc;
  1844. }
  1845. }
  1846. static int add_func_jump_tables(struct objtool_file *file,
  1847. struct symbol *func)
  1848. {
  1849. struct instruction *insn, *insn_t1 = NULL, *insn_t2;
  1850. int ret = 0;
  1851. func_for_each_insn(file, func, insn) {
  1852. if (!insn_jump_table(insn))
  1853. continue;
  1854. if (!insn_t1) {
  1855. insn_t1 = insn;
  1856. continue;
  1857. }
  1858. insn_t2 = insn;
  1859. ret = add_jump_table(file, insn_t1, insn_jump_table(insn_t2));
  1860. if (ret)
  1861. return ret;
  1862. insn_t1 = insn_t2;
  1863. }
  1864. if (insn_t1)
  1865. ret = add_jump_table(file, insn_t1, NULL);
  1866. return ret;
  1867. }
  1868. /*
  1869. * For some switch statements, gcc generates a jump table in the .rodata
  1870. * section which contains a list of addresses within the function to jump to.
  1871. * This finds these jump tables and adds them to the insn->alts lists.
  1872. */
  1873. static int add_jump_table_alts(struct objtool_file *file)
  1874. {
  1875. struct symbol *func;
  1876. int ret;
  1877. if (!file->rodata)
  1878. return 0;
  1879. for_each_sym(file, func) {
  1880. if (func->type != STT_FUNC)
  1881. continue;
  1882. mark_func_jump_tables(file, func);
  1883. ret = add_func_jump_tables(file, func);
  1884. if (ret)
  1885. return ret;
  1886. }
  1887. return 0;
  1888. }
  1889. static void set_func_state(struct cfi_state *state)
  1890. {
  1891. state->cfa = initial_func_cfi.cfa;
  1892. memcpy(&state->regs, &initial_func_cfi.regs,
  1893. CFI_NUM_REGS * sizeof(struct cfi_reg));
  1894. state->stack_size = initial_func_cfi.cfa.offset;
  1895. state->type = UNWIND_HINT_TYPE_CALL;
  1896. }
  1897. static int read_unwind_hints(struct objtool_file *file)
  1898. {
  1899. struct cfi_state cfi = init_cfi;
  1900. struct section *sec;
  1901. struct unwind_hint *hint;
  1902. struct instruction *insn;
  1903. struct reloc *reloc;
  1904. unsigned long offset;
  1905. int i;
  1906. sec = find_section_by_name(file->elf, ".discard.unwind_hints");
  1907. if (!sec)
  1908. return 0;
  1909. if (!sec->rsec) {
  1910. WARN("missing .rela.discard.unwind_hints section");
  1911. return -1;
  1912. }
  1913. if (sec->sh.sh_size % sizeof(struct unwind_hint)) {
  1914. WARN("struct unwind_hint size mismatch");
  1915. return -1;
  1916. }
  1917. file->hints = true;
  1918. for (i = 0; i < sec->sh.sh_size / sizeof(struct unwind_hint); i++) {
  1919. hint = (struct unwind_hint *)sec->data->d_buf + i;
  1920. reloc = find_reloc_by_dest(file->elf, sec, i * sizeof(*hint));
  1921. if (!reloc) {
  1922. WARN("can't find reloc for unwind_hints[%d]", i);
  1923. return -1;
  1924. }
  1925. if (reloc->sym->type == STT_SECTION) {
  1926. offset = reloc_addend(reloc);
  1927. } else if (reloc->sym->local_label) {
  1928. offset = reloc->sym->offset;
  1929. } else {
  1930. WARN("unexpected relocation symbol type in %s", sec->rsec->name);
  1931. return -1;
  1932. }
  1933. insn = find_insn(file, reloc->sym->sec, offset);
  1934. if (!insn) {
  1935. WARN("can't find insn for unwind_hints[%d]", i);
  1936. return -1;
  1937. }
  1938. insn->hint = true;
  1939. if (hint->type == UNWIND_HINT_TYPE_UNDEFINED) {
  1940. insn->cfi = &force_undefined_cfi;
  1941. continue;
  1942. }
  1943. if (hint->type == UNWIND_HINT_TYPE_SAVE) {
  1944. insn->hint = false;
  1945. insn->save = true;
  1946. continue;
  1947. }
  1948. if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
  1949. insn->restore = true;
  1950. continue;
  1951. }
  1952. if (hint->type == UNWIND_HINT_TYPE_REGS_PARTIAL) {
  1953. struct symbol *sym = find_symbol_by_offset(insn->sec, insn->offset);
  1954. if (sym && sym->bind == STB_GLOBAL) {
  1955. if (opts.ibt && insn->type != INSN_ENDBR && !insn->noendbr) {
  1956. WARN_INSN(insn, "UNWIND_HINT_IRET_REGS without ENDBR");
  1957. }
  1958. }
  1959. }
  1960. if (hint->type == UNWIND_HINT_TYPE_FUNC) {
  1961. insn->cfi = &func_cfi;
  1962. continue;
  1963. }
  1964. if (insn->cfi)
  1965. cfi = *(insn->cfi);
  1966. if (arch_decode_hint_reg(hint->sp_reg, &cfi.cfa.base)) {
  1967. WARN_INSN(insn, "unsupported unwind_hint sp base reg %d", hint->sp_reg);
  1968. return -1;
  1969. }
  1970. cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
  1971. cfi.type = hint->type;
  1972. cfi.signal = hint->signal;
  1973. insn->cfi = cfi_hash_find_or_add(&cfi);
  1974. }
  1975. return 0;
  1976. }
  1977. static int read_noendbr_hints(struct objtool_file *file)
  1978. {
  1979. struct instruction *insn;
  1980. struct section *rsec;
  1981. struct reloc *reloc;
  1982. rsec = find_section_by_name(file->elf, ".rela.discard.noendbr");
  1983. if (!rsec)
  1984. return 0;
  1985. for_each_reloc(rsec, reloc) {
  1986. insn = find_insn(file, reloc->sym->sec,
  1987. reloc->sym->offset + reloc_addend(reloc));
  1988. if (!insn) {
  1989. WARN("bad .discard.noendbr entry");
  1990. return -1;
  1991. }
  1992. insn->noendbr = 1;
  1993. }
  1994. return 0;
  1995. }
  1996. static int read_retpoline_hints(struct objtool_file *file)
  1997. {
  1998. struct section *rsec;
  1999. struct instruction *insn;
  2000. struct reloc *reloc;
  2001. rsec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
  2002. if (!rsec)
  2003. return 0;
  2004. for_each_reloc(rsec, reloc) {
  2005. if (reloc->sym->type != STT_SECTION) {
  2006. WARN("unexpected relocation symbol type in %s", rsec->name);
  2007. return -1;
  2008. }
  2009. insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
  2010. if (!insn) {
  2011. WARN("bad .discard.retpoline_safe entry");
  2012. return -1;
  2013. }
  2014. if (insn->type != INSN_JUMP_DYNAMIC &&
  2015. insn->type != INSN_CALL_DYNAMIC &&
  2016. insn->type != INSN_RETURN &&
  2017. insn->type != INSN_NOP) {
  2018. WARN_INSN(insn, "retpoline_safe hint not an indirect jump/call/ret/nop");
  2019. return -1;
  2020. }
  2021. insn->retpoline_safe = true;
  2022. }
  2023. return 0;
  2024. }
  2025. static int read_instr_hints(struct objtool_file *file)
  2026. {
  2027. struct section *rsec;
  2028. struct instruction *insn;
  2029. struct reloc *reloc;
  2030. rsec = find_section_by_name(file->elf, ".rela.discard.instr_end");
  2031. if (!rsec)
  2032. return 0;
  2033. for_each_reloc(rsec, reloc) {
  2034. if (reloc->sym->type != STT_SECTION) {
  2035. WARN("unexpected relocation symbol type in %s", rsec->name);
  2036. return -1;
  2037. }
  2038. insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
  2039. if (!insn) {
  2040. WARN("bad .discard.instr_end entry");
  2041. return -1;
  2042. }
  2043. insn->instr--;
  2044. }
  2045. rsec = find_section_by_name(file->elf, ".rela.discard.instr_begin");
  2046. if (!rsec)
  2047. return 0;
  2048. for_each_reloc(rsec, reloc) {
  2049. if (reloc->sym->type != STT_SECTION) {
  2050. WARN("unexpected relocation symbol type in %s", rsec->name);
  2051. return -1;
  2052. }
  2053. insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
  2054. if (!insn) {
  2055. WARN("bad .discard.instr_begin entry");
  2056. return -1;
  2057. }
  2058. insn->instr++;
  2059. }
  2060. return 0;
  2061. }
  2062. static int read_validate_unret_hints(struct objtool_file *file)
  2063. {
  2064. struct section *rsec;
  2065. struct instruction *insn;
  2066. struct reloc *reloc;
  2067. rsec = find_section_by_name(file->elf, ".rela.discard.validate_unret");
  2068. if (!rsec)
  2069. return 0;
  2070. for_each_reloc(rsec, reloc) {
  2071. if (reloc->sym->type != STT_SECTION) {
  2072. WARN("unexpected relocation symbol type in %s", rsec->name);
  2073. return -1;
  2074. }
  2075. insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
  2076. if (!insn) {
  2077. WARN("bad .discard.instr_end entry");
  2078. return -1;
  2079. }
  2080. insn->unret = 1;
  2081. }
  2082. return 0;
  2083. }
  2084. static int read_intra_function_calls(struct objtool_file *file)
  2085. {
  2086. struct instruction *insn;
  2087. struct section *rsec;
  2088. struct reloc *reloc;
  2089. rsec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls");
  2090. if (!rsec)
  2091. return 0;
  2092. for_each_reloc(rsec, reloc) {
  2093. unsigned long dest_off;
  2094. if (reloc->sym->type != STT_SECTION) {
  2095. WARN("unexpected relocation symbol type in %s",
  2096. rsec->name);
  2097. return -1;
  2098. }
  2099. insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
  2100. if (!insn) {
  2101. WARN("bad .discard.intra_function_call entry");
  2102. return -1;
  2103. }
  2104. if (insn->type != INSN_CALL) {
  2105. WARN_INSN(insn, "intra_function_call not a direct call");
  2106. return -1;
  2107. }
  2108. /*
  2109. * Treat intra-function CALLs as JMPs, but with a stack_op.
  2110. * See add_call_destinations(), which strips stack_ops from
  2111. * normal CALLs.
  2112. */
  2113. insn->type = INSN_JUMP_UNCONDITIONAL;
  2114. dest_off = arch_jump_destination(insn);
  2115. insn->jump_dest = find_insn(file, insn->sec, dest_off);
  2116. if (!insn->jump_dest) {
  2117. WARN_INSN(insn, "can't find call dest at %s+0x%lx",
  2118. insn->sec->name, dest_off);
  2119. return -1;
  2120. }
  2121. }
  2122. return 0;
  2123. }
  2124. /*
  2125. * Return true if name matches an instrumentation function, where calls to that
  2126. * function from noinstr code can safely be removed, but compilers won't do so.
  2127. */
  2128. static bool is_profiling_func(const char *name)
  2129. {
  2130. /*
  2131. * Many compilers cannot disable KCOV with a function attribute.
  2132. */
  2133. if (!strncmp(name, "__sanitizer_cov_", 16))
  2134. return true;
  2135. /*
  2136. * Some compilers currently do not remove __tsan_func_entry/exit nor
  2137. * __tsan_atomic_signal_fence (used for barrier instrumentation) with
  2138. * the __no_sanitize_thread attribute, remove them. Once the kernel's
  2139. * minimum Clang version is 14.0, this can be removed.
  2140. */
  2141. if (!strncmp(name, "__tsan_func_", 12) ||
  2142. !strcmp(name, "__tsan_atomic_signal_fence"))
  2143. return true;
  2144. return false;
  2145. }
  2146. static int classify_symbols(struct objtool_file *file)
  2147. {
  2148. struct symbol *func;
  2149. for_each_sym(file, func) {
  2150. if (func->type == STT_NOTYPE && strstarts(func->name, ".L"))
  2151. func->local_label = true;
  2152. if (func->bind != STB_GLOBAL)
  2153. continue;
  2154. if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
  2155. strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
  2156. func->static_call_tramp = true;
  2157. if (arch_is_retpoline(func))
  2158. func->retpoline_thunk = true;
  2159. if (arch_is_rethunk(func))
  2160. func->return_thunk = true;
  2161. if (arch_is_embedded_insn(func))
  2162. func->embedded_insn = true;
  2163. if (arch_ftrace_match(func->name))
  2164. func->fentry = true;
  2165. if (is_profiling_func(func->name))
  2166. func->profiling_func = true;
  2167. }
  2168. return 0;
  2169. }
  2170. static void mark_rodata(struct objtool_file *file)
  2171. {
  2172. struct section *sec;
  2173. bool found = false;
  2174. /*
  2175. * Search for the following rodata sections, each of which can
  2176. * potentially contain jump tables:
  2177. *
  2178. * - .rodata: can contain GCC switch tables
  2179. * - .rodata.<func>: same, if -fdata-sections is being used
  2180. * - .data.rel.ro.c_jump_table: contains C annotated jump tables
  2181. *
  2182. * .rodata.str1.* sections are ignored; they don't contain jump tables.
  2183. */
  2184. for_each_sec(file, sec) {
  2185. if ((!strncmp(sec->name, ".rodata", 7) &&
  2186. !strstr(sec->name, ".str1.")) ||
  2187. !strncmp(sec->name, ".data.rel.ro", 12)) {
  2188. sec->rodata = true;
  2189. found = true;
  2190. }
  2191. }
  2192. file->rodata = found;
  2193. }
  2194. static int decode_sections(struct objtool_file *file)
  2195. {
  2196. int ret;
  2197. mark_rodata(file);
  2198. ret = init_pv_ops(file);
  2199. if (ret)
  2200. return ret;
  2201. /*
  2202. * Must be before add_{jump_call}_destination.
  2203. */
  2204. ret = classify_symbols(file);
  2205. if (ret)
  2206. return ret;
  2207. ret = decode_instructions(file);
  2208. if (ret)
  2209. return ret;
  2210. add_ignores(file);
  2211. add_uaccess_safe(file);
  2212. ret = add_ignore_alternatives(file);
  2213. if (ret)
  2214. return ret;
  2215. /*
  2216. * Must be before read_unwind_hints() since that needs insn->noendbr.
  2217. */
  2218. ret = read_noendbr_hints(file);
  2219. if (ret)
  2220. return ret;
  2221. /*
  2222. * Must be before add_jump_destinations(), which depends on 'func'
  2223. * being set for alternatives, to enable proper sibling call detection.
  2224. */
  2225. if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) {
  2226. ret = add_special_section_alts(file);
  2227. if (ret)
  2228. return ret;
  2229. }
  2230. ret = add_jump_destinations(file);
  2231. if (ret)
  2232. return ret;
  2233. /*
  2234. * Must be before add_call_destination(); it changes INSN_CALL to
  2235. * INSN_JUMP.
  2236. */
  2237. ret = read_intra_function_calls(file);
  2238. if (ret)
  2239. return ret;
  2240. ret = add_call_destinations(file);
  2241. if (ret)
  2242. return ret;
  2243. /*
  2244. * Must be after add_call_destinations() such that it can override
  2245. * dead_end_function() marks.
  2246. */
  2247. ret = add_dead_ends(file);
  2248. if (ret)
  2249. return ret;
  2250. ret = add_jump_table_alts(file);
  2251. if (ret)
  2252. return ret;
  2253. ret = read_unwind_hints(file);
  2254. if (ret)
  2255. return ret;
  2256. ret = read_retpoline_hints(file);
  2257. if (ret)
  2258. return ret;
  2259. ret = read_instr_hints(file);
  2260. if (ret)
  2261. return ret;
  2262. ret = read_validate_unret_hints(file);
  2263. if (ret)
  2264. return ret;
  2265. return 0;
  2266. }
  2267. static bool is_special_call(struct instruction *insn)
  2268. {
  2269. if (insn->type == INSN_CALL) {
  2270. struct symbol *dest = insn_call_dest(insn);
  2271. if (!dest)
  2272. return false;
  2273. if (dest->fentry || dest->embedded_insn)
  2274. return true;
  2275. }
  2276. return false;
  2277. }
  2278. static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
  2279. {
  2280. struct cfi_state *cfi = &state->cfi;
  2281. int i;
  2282. if (cfi->cfa.base != initial_func_cfi.cfa.base || cfi->drap)
  2283. return true;
  2284. if (cfi->cfa.offset != initial_func_cfi.cfa.offset)
  2285. return true;
  2286. if (cfi->stack_size != initial_func_cfi.cfa.offset)
  2287. return true;
  2288. for (i = 0; i < CFI_NUM_REGS; i++) {
  2289. if (cfi->regs[i].base != initial_func_cfi.regs[i].base ||
  2290. cfi->regs[i].offset != initial_func_cfi.regs[i].offset)
  2291. return true;
  2292. }
  2293. return false;
  2294. }
  2295. static bool check_reg_frame_pos(const struct cfi_reg *reg,
  2296. int expected_offset)
  2297. {
  2298. return reg->base == CFI_CFA &&
  2299. reg->offset == expected_offset;
  2300. }
  2301. static bool has_valid_stack_frame(struct insn_state *state)
  2302. {
  2303. struct cfi_state *cfi = &state->cfi;
  2304. if (cfi->cfa.base == CFI_BP &&
  2305. check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
  2306. check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
  2307. return true;
  2308. if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
  2309. return true;
  2310. return false;
  2311. }
  2312. static int update_cfi_state_regs(struct instruction *insn,
  2313. struct cfi_state *cfi,
  2314. struct stack_op *op)
  2315. {
  2316. struct cfi_reg *cfa = &cfi->cfa;
  2317. if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
  2318. return 0;
  2319. /* push */
  2320. if (op->dest.type == OP_DEST_PUSH || op->dest.type == OP_DEST_PUSHF)
  2321. cfa->offset += 8;
  2322. /* pop */
  2323. if (op->src.type == OP_SRC_POP || op->src.type == OP_SRC_POPF)
  2324. cfa->offset -= 8;
  2325. /* add immediate to sp */
  2326. if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
  2327. op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
  2328. cfa->offset -= op->src.offset;
  2329. return 0;
  2330. }
  2331. static void save_reg(struct cfi_state *cfi, unsigned char reg, int base, int offset)
  2332. {
  2333. if (arch_callee_saved_reg(reg) &&
  2334. cfi->regs[reg].base == CFI_UNDEFINED) {
  2335. cfi->regs[reg].base = base;
  2336. cfi->regs[reg].offset = offset;
  2337. }
  2338. }
  2339. static void restore_reg(struct cfi_state *cfi, unsigned char reg)
  2340. {
  2341. cfi->regs[reg].base = initial_func_cfi.regs[reg].base;
  2342. cfi->regs[reg].offset = initial_func_cfi.regs[reg].offset;
  2343. }
  2344. /*
  2345. * A note about DRAP stack alignment:
  2346. *
  2347. * GCC has the concept of a DRAP register, which is used to help keep track of
  2348. * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
  2349. * register. The typical DRAP pattern is:
  2350. *
  2351. * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
  2352. * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
  2353. * 41 ff 72 f8 pushq -0x8(%r10)
  2354. * 55 push %rbp
  2355. * 48 89 e5 mov %rsp,%rbp
  2356. * (more pushes)
  2357. * 41 52 push %r10
  2358. * ...
  2359. * 41 5a pop %r10
  2360. * (more pops)
  2361. * 5d pop %rbp
  2362. * 49 8d 62 f8 lea -0x8(%r10),%rsp
  2363. * c3 retq
  2364. *
  2365. * There are some variations in the epilogues, like:
  2366. *
  2367. * 5b pop %rbx
  2368. * 41 5a pop %r10
  2369. * 41 5c pop %r12
  2370. * 41 5d pop %r13
  2371. * 41 5e pop %r14
  2372. * c9 leaveq
  2373. * 49 8d 62 f8 lea -0x8(%r10),%rsp
  2374. * c3 retq
  2375. *
  2376. * and:
  2377. *
  2378. * 4c 8b 55 e8 mov -0x18(%rbp),%r10
  2379. * 48 8b 5d e0 mov -0x20(%rbp),%rbx
  2380. * 4c 8b 65 f0 mov -0x10(%rbp),%r12
  2381. * 4c 8b 6d f8 mov -0x8(%rbp),%r13
  2382. * c9 leaveq
  2383. * 49 8d 62 f8 lea -0x8(%r10),%rsp
  2384. * c3 retq
  2385. *
  2386. * Sometimes r13 is used as the DRAP register, in which case it's saved and
  2387. * restored beforehand:
  2388. *
  2389. * 41 55 push %r13
  2390. * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
  2391. * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
  2392. * ...
  2393. * 49 8d 65 f0 lea -0x10(%r13),%rsp
  2394. * 41 5d pop %r13
  2395. * c3 retq
  2396. */
  2397. static int update_cfi_state(struct instruction *insn,
  2398. struct instruction *next_insn,
  2399. struct cfi_state *cfi, struct stack_op *op)
  2400. {
  2401. struct cfi_reg *cfa = &cfi->cfa;
  2402. struct cfi_reg *regs = cfi->regs;
  2403. /* ignore UNWIND_HINT_UNDEFINED regions */
  2404. if (cfi->force_undefined)
  2405. return 0;
  2406. /* stack operations don't make sense with an undefined CFA */
  2407. if (cfa->base == CFI_UNDEFINED) {
  2408. if (insn_func(insn)) {
  2409. WARN_INSN(insn, "undefined stack state");
  2410. return -1;
  2411. }
  2412. return 0;
  2413. }
  2414. if (cfi->type == UNWIND_HINT_TYPE_REGS ||
  2415. cfi->type == UNWIND_HINT_TYPE_REGS_PARTIAL)
  2416. return update_cfi_state_regs(insn, cfi, op);
  2417. switch (op->dest.type) {
  2418. case OP_DEST_REG:
  2419. switch (op->src.type) {
  2420. case OP_SRC_REG:
  2421. if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
  2422. cfa->base == CFI_SP &&
  2423. check_reg_frame_pos(&regs[CFI_BP], -cfa->offset)) {
  2424. /* mov %rsp, %rbp */
  2425. cfa->base = op->dest.reg;
  2426. cfi->bp_scratch = false;
  2427. }
  2428. else if (op->src.reg == CFI_SP &&
  2429. op->dest.reg == CFI_BP && cfi->drap) {
  2430. /* drap: mov %rsp, %rbp */
  2431. regs[CFI_BP].base = CFI_BP;
  2432. regs[CFI_BP].offset = -cfi->stack_size;
  2433. cfi->bp_scratch = false;
  2434. }
  2435. else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
  2436. /*
  2437. * mov %rsp, %reg
  2438. *
  2439. * This is needed for the rare case where GCC
  2440. * does:
  2441. *
  2442. * mov %rsp, %rax
  2443. * ...
  2444. * mov %rax, %rsp
  2445. */
  2446. cfi->vals[op->dest.reg].base = CFI_CFA;
  2447. cfi->vals[op->dest.reg].offset = -cfi->stack_size;
  2448. }
  2449. else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
  2450. (cfa->base == CFI_BP || cfa->base == cfi->drap_reg)) {
  2451. /*
  2452. * mov %rbp, %rsp
  2453. *
  2454. * Restore the original stack pointer (Clang).
  2455. */
  2456. cfi->stack_size = -cfi->regs[CFI_BP].offset;
  2457. }
  2458. else if (op->dest.reg == cfa->base) {
  2459. /* mov %reg, %rsp */
  2460. if (cfa->base == CFI_SP &&
  2461. cfi->vals[op->src.reg].base == CFI_CFA) {
  2462. /*
  2463. * This is needed for the rare case
  2464. * where GCC does something dumb like:
  2465. *
  2466. * lea 0x8(%rsp), %rcx
  2467. * ...
  2468. * mov %rcx, %rsp
  2469. */
  2470. cfa->offset = -cfi->vals[op->src.reg].offset;
  2471. cfi->stack_size = cfa->offset;
  2472. } else if (cfa->base == CFI_SP &&
  2473. cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
  2474. cfi->vals[op->src.reg].offset == cfa->offset) {
  2475. /*
  2476. * Stack swizzle:
  2477. *
  2478. * 1: mov %rsp, (%[tos])
  2479. * 2: mov %[tos], %rsp
  2480. * ...
  2481. * 3: pop %rsp
  2482. *
  2483. * Where:
  2484. *
  2485. * 1 - places a pointer to the previous
  2486. * stack at the Top-of-Stack of the
  2487. * new stack.
  2488. *
  2489. * 2 - switches to the new stack.
  2490. *
  2491. * 3 - pops the Top-of-Stack to restore
  2492. * the original stack.
  2493. *
  2494. * Note: we set base to SP_INDIRECT
  2495. * here and preserve offset. Therefore
  2496. * when the unwinder reaches ToS it
  2497. * will dereference SP and then add the
  2498. * offset to find the next frame, IOW:
  2499. * (%rsp) + offset.
  2500. */
  2501. cfa->base = CFI_SP_INDIRECT;
  2502. } else {
  2503. cfa->base = CFI_UNDEFINED;
  2504. cfa->offset = 0;
  2505. }
  2506. }
  2507. else if (op->dest.reg == CFI_SP &&
  2508. cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
  2509. cfi->vals[op->src.reg].offset == cfa->offset) {
  2510. /*
  2511. * The same stack swizzle case 2) as above. But
  2512. * because we can't change cfa->base, case 3)
  2513. * will become a regular POP. Pretend we're a
  2514. * PUSH so things don't go unbalanced.
  2515. */
  2516. cfi->stack_size += 8;
  2517. }
  2518. break;
  2519. case OP_SRC_ADD:
  2520. if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
  2521. /* add imm, %rsp */
  2522. cfi->stack_size -= op->src.offset;
  2523. if (cfa->base == CFI_SP)
  2524. cfa->offset -= op->src.offset;
  2525. break;
  2526. }
  2527. if (op->dest.reg == CFI_BP && op->src.reg == CFI_SP &&
  2528. insn->sym->frame_pointer) {
  2529. /* addi.d fp,sp,imm on LoongArch */
  2530. if (cfa->base == CFI_SP && cfa->offset == op->src.offset) {
  2531. cfa->base = CFI_BP;
  2532. cfa->offset = 0;
  2533. }
  2534. break;
  2535. }
  2536. if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
  2537. /* addi.d sp,fp,imm on LoongArch */
  2538. if (cfa->base == CFI_BP && cfa->offset == 0) {
  2539. if (insn->sym->frame_pointer) {
  2540. cfa->base = CFI_SP;
  2541. cfa->offset = -op->src.offset;
  2542. }
  2543. } else {
  2544. /* lea disp(%rbp), %rsp */
  2545. cfi->stack_size = -(op->src.offset + regs[CFI_BP].offset);
  2546. }
  2547. break;
  2548. }
  2549. if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
  2550. /* drap: lea disp(%rsp), %drap */
  2551. cfi->drap_reg = op->dest.reg;
  2552. /*
  2553. * lea disp(%rsp), %reg
  2554. *
  2555. * This is needed for the rare case where GCC
  2556. * does something dumb like:
  2557. *
  2558. * lea 0x8(%rsp), %rcx
  2559. * ...
  2560. * mov %rcx, %rsp
  2561. */
  2562. cfi->vals[op->dest.reg].base = CFI_CFA;
  2563. cfi->vals[op->dest.reg].offset = \
  2564. -cfi->stack_size + op->src.offset;
  2565. break;
  2566. }
  2567. if (cfi->drap && op->dest.reg == CFI_SP &&
  2568. op->src.reg == cfi->drap_reg) {
  2569. /* drap: lea disp(%drap), %rsp */
  2570. cfa->base = CFI_SP;
  2571. cfa->offset = cfi->stack_size = -op->src.offset;
  2572. cfi->drap_reg = CFI_UNDEFINED;
  2573. cfi->drap = false;
  2574. break;
  2575. }
  2576. if (op->dest.reg == cfi->cfa.base && !(next_insn && next_insn->hint)) {
  2577. WARN_INSN(insn, "unsupported stack register modification");
  2578. return -1;
  2579. }
  2580. break;
  2581. case OP_SRC_AND:
  2582. if (op->dest.reg != CFI_SP ||
  2583. (cfi->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
  2584. (cfi->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
  2585. WARN_INSN(insn, "unsupported stack pointer realignment");
  2586. return -1;
  2587. }
  2588. if (cfi->drap_reg != CFI_UNDEFINED) {
  2589. /* drap: and imm, %rsp */
  2590. cfa->base = cfi->drap_reg;
  2591. cfa->offset = cfi->stack_size = 0;
  2592. cfi->drap = true;
  2593. }
  2594. /*
  2595. * Older versions of GCC (4.8ish) realign the stack
  2596. * without DRAP, with a frame pointer.
  2597. */
  2598. break;
  2599. case OP_SRC_POP:
  2600. case OP_SRC_POPF:
  2601. if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
  2602. /* pop %rsp; # restore from a stack swizzle */
  2603. cfa->base = CFI_SP;
  2604. break;
  2605. }
  2606. if (!cfi->drap && op->dest.reg == cfa->base) {
  2607. /* pop %rbp */
  2608. cfa->base = CFI_SP;
  2609. }
  2610. if (cfi->drap && cfa->base == CFI_BP_INDIRECT &&
  2611. op->dest.reg == cfi->drap_reg &&
  2612. cfi->drap_offset == -cfi->stack_size) {
  2613. /* drap: pop %drap */
  2614. cfa->base = cfi->drap_reg;
  2615. cfa->offset = 0;
  2616. cfi->drap_offset = -1;
  2617. } else if (cfi->stack_size == -regs[op->dest.reg].offset) {
  2618. /* pop %reg */
  2619. restore_reg(cfi, op->dest.reg);
  2620. }
  2621. cfi->stack_size -= 8;
  2622. if (cfa->base == CFI_SP)
  2623. cfa->offset -= 8;
  2624. break;
  2625. case OP_SRC_REG_INDIRECT:
  2626. if (!cfi->drap && op->dest.reg == cfa->base &&
  2627. op->dest.reg == CFI_BP) {
  2628. /* mov disp(%rsp), %rbp */
  2629. cfa->base = CFI_SP;
  2630. cfa->offset = cfi->stack_size;
  2631. }
  2632. if (cfi->drap && op->src.reg == CFI_BP &&
  2633. op->src.offset == cfi->drap_offset) {
  2634. /* drap: mov disp(%rbp), %drap */
  2635. cfa->base = cfi->drap_reg;
  2636. cfa->offset = 0;
  2637. cfi->drap_offset = -1;
  2638. }
  2639. if (cfi->drap && op->src.reg == CFI_BP &&
  2640. op->src.offset == regs[op->dest.reg].offset) {
  2641. /* drap: mov disp(%rbp), %reg */
  2642. restore_reg(cfi, op->dest.reg);
  2643. } else if (op->src.reg == cfa->base &&
  2644. op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
  2645. /* mov disp(%rbp), %reg */
  2646. /* mov disp(%rsp), %reg */
  2647. restore_reg(cfi, op->dest.reg);
  2648. } else if (op->src.reg == CFI_SP &&
  2649. op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) {
  2650. /* mov disp(%rsp), %reg */
  2651. restore_reg(cfi, op->dest.reg);
  2652. }
  2653. break;
  2654. default:
  2655. WARN_INSN(insn, "unknown stack-related instruction");
  2656. return -1;
  2657. }
  2658. break;
  2659. case OP_DEST_PUSH:
  2660. case OP_DEST_PUSHF:
  2661. cfi->stack_size += 8;
  2662. if (cfa->base == CFI_SP)
  2663. cfa->offset += 8;
  2664. if (op->src.type != OP_SRC_REG)
  2665. break;
  2666. if (cfi->drap) {
  2667. if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
  2668. /* drap: push %drap */
  2669. cfa->base = CFI_BP_INDIRECT;
  2670. cfa->offset = -cfi->stack_size;
  2671. /* save drap so we know when to restore it */
  2672. cfi->drap_offset = -cfi->stack_size;
  2673. } else if (op->src.reg == CFI_BP && cfa->base == cfi->drap_reg) {
  2674. /* drap: push %rbp */
  2675. cfi->stack_size = 0;
  2676. } else {
  2677. /* drap: push %reg */
  2678. save_reg(cfi, op->src.reg, CFI_BP, -cfi->stack_size);
  2679. }
  2680. } else {
  2681. /* push %reg */
  2682. save_reg(cfi, op->src.reg, CFI_CFA, -cfi->stack_size);
  2683. }
  2684. /* detect when asm code uses rbp as a scratch register */
  2685. if (opts.stackval && insn_func(insn) && op->src.reg == CFI_BP &&
  2686. cfa->base != CFI_BP)
  2687. cfi->bp_scratch = true;
  2688. break;
  2689. case OP_DEST_REG_INDIRECT:
  2690. if (cfi->drap) {
  2691. if (op->src.reg == cfa->base && op->src.reg == cfi->drap_reg) {
  2692. /* drap: mov %drap, disp(%rbp) */
  2693. cfa->base = CFI_BP_INDIRECT;
  2694. cfa->offset = op->dest.offset;
  2695. /* save drap offset so we know when to restore it */
  2696. cfi->drap_offset = op->dest.offset;
  2697. } else {
  2698. /* drap: mov reg, disp(%rbp) */
  2699. save_reg(cfi, op->src.reg, CFI_BP, op->dest.offset);
  2700. }
  2701. } else if (op->dest.reg == cfa->base) {
  2702. /* mov reg, disp(%rbp) */
  2703. /* mov reg, disp(%rsp) */
  2704. save_reg(cfi, op->src.reg, CFI_CFA,
  2705. op->dest.offset - cfi->cfa.offset);
  2706. } else if (op->dest.reg == CFI_SP) {
  2707. /* mov reg, disp(%rsp) */
  2708. save_reg(cfi, op->src.reg, CFI_CFA,
  2709. op->dest.offset - cfi->stack_size);
  2710. } else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
  2711. /* mov %rsp, (%reg); # setup a stack swizzle. */
  2712. cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
  2713. cfi->vals[op->dest.reg].offset = cfa->offset;
  2714. }
  2715. break;
  2716. case OP_DEST_MEM:
  2717. if (op->src.type != OP_SRC_POP && op->src.type != OP_SRC_POPF) {
  2718. WARN_INSN(insn, "unknown stack-related memory operation");
  2719. return -1;
  2720. }
  2721. /* pop mem */
  2722. cfi->stack_size -= 8;
  2723. if (cfa->base == CFI_SP)
  2724. cfa->offset -= 8;
  2725. break;
  2726. default:
  2727. WARN_INSN(insn, "unknown stack-related instruction");
  2728. return -1;
  2729. }
  2730. return 0;
  2731. }
  2732. /*
  2733. * The stack layouts of alternatives instructions can sometimes diverge when
  2734. * they have stack modifications. That's fine as long as the potential stack
  2735. * layouts don't conflict at any given potential instruction boundary.
  2736. *
  2737. * Flatten the CFIs of the different alternative code streams (both original
  2738. * and replacement) into a single shared CFI array which can be used to detect
  2739. * conflicts and nicely feed a linear array of ORC entries to the unwinder.
  2740. */
  2741. static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn)
  2742. {
  2743. struct cfi_state **alt_cfi;
  2744. int group_off;
  2745. if (!insn->alt_group)
  2746. return 0;
  2747. if (!insn->cfi) {
  2748. WARN("CFI missing");
  2749. return -1;
  2750. }
  2751. alt_cfi = insn->alt_group->cfi;
  2752. group_off = insn->offset - insn->alt_group->first_insn->offset;
  2753. if (!alt_cfi[group_off]) {
  2754. alt_cfi[group_off] = insn->cfi;
  2755. } else {
  2756. if (cficmp(alt_cfi[group_off], insn->cfi)) {
  2757. struct alt_group *orig_group = insn->alt_group->orig_group ?: insn->alt_group;
  2758. struct instruction *orig = orig_group->first_insn;
  2759. char *where = offstr(insn->sec, insn->offset);
  2760. WARN_INSN(orig, "stack layout conflict in alternatives: %s", where);
  2761. free(where);
  2762. return -1;
  2763. }
  2764. }
  2765. return 0;
  2766. }
  2767. static int handle_insn_ops(struct instruction *insn,
  2768. struct instruction *next_insn,
  2769. struct insn_state *state)
  2770. {
  2771. struct stack_op *op;
  2772. for (op = insn->stack_ops; op; op = op->next) {
  2773. if (update_cfi_state(insn, next_insn, &state->cfi, op))
  2774. return 1;
  2775. if (!opts.uaccess || !insn->alt_group)
  2776. continue;
  2777. if (op->dest.type == OP_DEST_PUSHF) {
  2778. if (!state->uaccess_stack) {
  2779. state->uaccess_stack = 1;
  2780. } else if (state->uaccess_stack >> 31) {
  2781. WARN_INSN(insn, "PUSHF stack exhausted");
  2782. return 1;
  2783. }
  2784. state->uaccess_stack <<= 1;
  2785. state->uaccess_stack |= state->uaccess;
  2786. }
  2787. if (op->src.type == OP_SRC_POPF) {
  2788. if (state->uaccess_stack) {
  2789. state->uaccess = state->uaccess_stack & 1;
  2790. state->uaccess_stack >>= 1;
  2791. if (state->uaccess_stack == 1)
  2792. state->uaccess_stack = 0;
  2793. }
  2794. }
  2795. }
  2796. return 0;
  2797. }
  2798. static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
  2799. {
  2800. struct cfi_state *cfi1 = insn->cfi;
  2801. int i;
  2802. if (!cfi1) {
  2803. WARN("CFI missing");
  2804. return false;
  2805. }
  2806. if (memcmp(&cfi1->cfa, &cfi2->cfa, sizeof(cfi1->cfa))) {
  2807. WARN_INSN(insn, "stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
  2808. cfi1->cfa.base, cfi1->cfa.offset,
  2809. cfi2->cfa.base, cfi2->cfa.offset);
  2810. } else if (memcmp(&cfi1->regs, &cfi2->regs, sizeof(cfi1->regs))) {
  2811. for (i = 0; i < CFI_NUM_REGS; i++) {
  2812. if (!memcmp(&cfi1->regs[i], &cfi2->regs[i],
  2813. sizeof(struct cfi_reg)))
  2814. continue;
  2815. WARN_INSN(insn, "stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
  2816. i, cfi1->regs[i].base, cfi1->regs[i].offset,
  2817. i, cfi2->regs[i].base, cfi2->regs[i].offset);
  2818. break;
  2819. }
  2820. } else if (cfi1->type != cfi2->type) {
  2821. WARN_INSN(insn, "stack state mismatch: type1=%d type2=%d",
  2822. cfi1->type, cfi2->type);
  2823. } else if (cfi1->drap != cfi2->drap ||
  2824. (cfi1->drap && cfi1->drap_reg != cfi2->drap_reg) ||
  2825. (cfi1->drap && cfi1->drap_offset != cfi2->drap_offset)) {
  2826. WARN_INSN(insn, "stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
  2827. cfi1->drap, cfi1->drap_reg, cfi1->drap_offset,
  2828. cfi2->drap, cfi2->drap_reg, cfi2->drap_offset);
  2829. } else
  2830. return true;
  2831. return false;
  2832. }
  2833. static inline bool func_uaccess_safe(struct symbol *func)
  2834. {
  2835. if (func)
  2836. return func->uaccess_safe;
  2837. return false;
  2838. }
  2839. static inline const char *call_dest_name(struct instruction *insn)
  2840. {
  2841. static char pvname[19];
  2842. struct reloc *reloc;
  2843. int idx;
  2844. if (insn_call_dest(insn))
  2845. return insn_call_dest(insn)->name;
  2846. reloc = insn_reloc(NULL, insn);
  2847. if (reloc && !strcmp(reloc->sym->name, "pv_ops")) {
  2848. idx = (reloc_addend(reloc) / sizeof(void *));
  2849. snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
  2850. return pvname;
  2851. }
  2852. return "{dynamic}";
  2853. }
  2854. static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
  2855. {
  2856. struct symbol *target;
  2857. struct reloc *reloc;
  2858. int idx;
  2859. reloc = insn_reloc(file, insn);
  2860. if (!reloc || strcmp(reloc->sym->name, "pv_ops"))
  2861. return false;
  2862. idx = (arch_dest_reloc_offset(reloc_addend(reloc)) / sizeof(void *));
  2863. if (file->pv_ops[idx].clean)
  2864. return true;
  2865. file->pv_ops[idx].clean = true;
  2866. list_for_each_entry(target, &file->pv_ops[idx].targets, pv_target) {
  2867. if (!target->sec->noinstr) {
  2868. WARN("pv_ops[%d]: %s", idx, target->name);
  2869. file->pv_ops[idx].clean = false;
  2870. }
  2871. }
  2872. return file->pv_ops[idx].clean;
  2873. }
  2874. static inline bool noinstr_call_dest(struct objtool_file *file,
  2875. struct instruction *insn,
  2876. struct symbol *func)
  2877. {
  2878. /*
  2879. * We can't deal with indirect function calls at present;
  2880. * assume they're instrumented.
  2881. */
  2882. if (!func) {
  2883. if (file->pv_ops)
  2884. return pv_call_dest(file, insn);
  2885. return false;
  2886. }
  2887. /*
  2888. * If the symbol is from a noinstr section; we good.
  2889. */
  2890. if (func->sec->noinstr)
  2891. return true;
  2892. /*
  2893. * If the symbol is a static_call trampoline, we can't tell.
  2894. */
  2895. if (func->static_call_tramp)
  2896. return true;
  2897. /*
  2898. * The __ubsan_handle_*() calls are like WARN(), they only happen when
  2899. * something 'BAD' happened. At the risk of taking the machine down,
  2900. * let them proceed to get the message out.
  2901. */
  2902. if (!strncmp(func->name, "__ubsan_handle_", 15))
  2903. return true;
  2904. return false;
  2905. }
  2906. static int validate_call(struct objtool_file *file,
  2907. struct instruction *insn,
  2908. struct insn_state *state)
  2909. {
  2910. if (state->noinstr && state->instr <= 0 &&
  2911. !noinstr_call_dest(file, insn, insn_call_dest(insn))) {
  2912. WARN_INSN(insn, "call to %s() leaves .noinstr.text section", call_dest_name(insn));
  2913. return 1;
  2914. }
  2915. if (state->uaccess && !func_uaccess_safe(insn_call_dest(insn))) {
  2916. WARN_INSN(insn, "call to %s() with UACCESS enabled", call_dest_name(insn));
  2917. return 1;
  2918. }
  2919. if (state->df) {
  2920. WARN_INSN(insn, "call to %s() with DF set", call_dest_name(insn));
  2921. return 1;
  2922. }
  2923. return 0;
  2924. }
  2925. static int validate_sibling_call(struct objtool_file *file,
  2926. struct instruction *insn,
  2927. struct insn_state *state)
  2928. {
  2929. if (insn_func(insn) && has_modified_stack_frame(insn, state)) {
  2930. WARN_INSN(insn, "sibling call from callable instruction with modified stack frame");
  2931. return 1;
  2932. }
  2933. return validate_call(file, insn, state);
  2934. }
  2935. static int validate_return(struct symbol *func, struct instruction *insn, struct insn_state *state)
  2936. {
  2937. if (state->noinstr && state->instr > 0) {
  2938. WARN_INSN(insn, "return with instrumentation enabled");
  2939. return 1;
  2940. }
  2941. if (state->uaccess && !func_uaccess_safe(func)) {
  2942. WARN_INSN(insn, "return with UACCESS enabled");
  2943. return 1;
  2944. }
  2945. if (!state->uaccess && func_uaccess_safe(func)) {
  2946. WARN_INSN(insn, "return with UACCESS disabled from a UACCESS-safe function");
  2947. return 1;
  2948. }
  2949. if (state->df) {
  2950. WARN_INSN(insn, "return with DF set");
  2951. return 1;
  2952. }
  2953. if (func && has_modified_stack_frame(insn, state)) {
  2954. WARN_INSN(insn, "return with modified stack frame");
  2955. return 1;
  2956. }
  2957. if (state->cfi.bp_scratch) {
  2958. WARN_INSN(insn, "BP used as a scratch register");
  2959. return 1;
  2960. }
  2961. return 0;
  2962. }
  2963. static struct instruction *next_insn_to_validate(struct objtool_file *file,
  2964. struct instruction *insn)
  2965. {
  2966. struct alt_group *alt_group = insn->alt_group;
  2967. /*
  2968. * Simulate the fact that alternatives are patched in-place. When the
  2969. * end of a replacement alt_group is reached, redirect objtool flow to
  2970. * the end of the original alt_group.
  2971. *
  2972. * insn->alts->insn -> alt_group->first_insn
  2973. * ...
  2974. * alt_group->last_insn
  2975. * [alt_group->nop] -> next(orig_group->last_insn)
  2976. */
  2977. if (alt_group) {
  2978. if (alt_group->nop) {
  2979. /* ->nop implies ->orig_group */
  2980. if (insn == alt_group->last_insn)
  2981. return alt_group->nop;
  2982. if (insn == alt_group->nop)
  2983. goto next_orig;
  2984. }
  2985. if (insn == alt_group->last_insn && alt_group->orig_group)
  2986. goto next_orig;
  2987. }
  2988. return next_insn_same_sec(file, insn);
  2989. next_orig:
  2990. return next_insn_same_sec(file, alt_group->orig_group->last_insn);
  2991. }
  2992. /*
  2993. * Follow the branch starting at the given instruction, and recursively follow
  2994. * any other branches (jumps). Meanwhile, track the frame pointer state at
  2995. * each instruction and validate all the rules described in
  2996. * tools/objtool/Documentation/objtool.txt.
  2997. */
  2998. static int validate_branch(struct objtool_file *file, struct symbol *func,
  2999. struct instruction *insn, struct insn_state state)
  3000. {
  3001. struct alternative *alt;
  3002. struct instruction *next_insn, *prev_insn = NULL;
  3003. struct section *sec;
  3004. u8 visited;
  3005. int ret;
  3006. sec = insn->sec;
  3007. while (1) {
  3008. next_insn = next_insn_to_validate(file, insn);
  3009. if (func && insn_func(insn) && func != insn_func(insn)->pfunc) {
  3010. /* Ignore KCFI type preambles, which always fall through */
  3011. if (!strncmp(func->name, "__cfi_", 6) ||
  3012. !strncmp(func->name, "__pfx_", 6))
  3013. return 0;
  3014. if (file->ignore_unreachables)
  3015. return 0;
  3016. WARN("%s() falls through to next function %s()",
  3017. func->name, insn_func(insn)->name);
  3018. return 1;
  3019. }
  3020. if (func && insn->ignore) {
  3021. WARN_INSN(insn, "BUG: why am I validating an ignored function?");
  3022. return 1;
  3023. }
  3024. visited = VISITED_BRANCH << state.uaccess;
  3025. if (insn->visited & VISITED_BRANCH_MASK) {
  3026. if (!insn->hint && !insn_cfi_match(insn, &state.cfi))
  3027. return 1;
  3028. if (insn->visited & visited)
  3029. return 0;
  3030. } else {
  3031. nr_insns_visited++;
  3032. }
  3033. if (state.noinstr)
  3034. state.instr += insn->instr;
  3035. if (insn->hint) {
  3036. if (insn->restore) {
  3037. struct instruction *save_insn, *i;
  3038. i = insn;
  3039. save_insn = NULL;
  3040. sym_for_each_insn_continue_reverse(file, func, i) {
  3041. if (i->save) {
  3042. save_insn = i;
  3043. break;
  3044. }
  3045. }
  3046. if (!save_insn) {
  3047. WARN_INSN(insn, "no corresponding CFI save for CFI restore");
  3048. return 1;
  3049. }
  3050. if (!save_insn->visited) {
  3051. /*
  3052. * If the restore hint insn is at the
  3053. * beginning of a basic block and was
  3054. * branched to from elsewhere, and the
  3055. * save insn hasn't been visited yet,
  3056. * defer following this branch for now.
  3057. * It will be seen later via the
  3058. * straight-line path.
  3059. */
  3060. if (!prev_insn)
  3061. return 0;
  3062. WARN_INSN(insn, "objtool isn't smart enough to handle this CFI save/restore combo");
  3063. return 1;
  3064. }
  3065. insn->cfi = save_insn->cfi;
  3066. nr_cfi_reused++;
  3067. }
  3068. state.cfi = *insn->cfi;
  3069. } else {
  3070. /* XXX track if we actually changed state.cfi */
  3071. if (prev_insn && !cficmp(prev_insn->cfi, &state.cfi)) {
  3072. insn->cfi = prev_insn->cfi;
  3073. nr_cfi_reused++;
  3074. } else {
  3075. insn->cfi = cfi_hash_find_or_add(&state.cfi);
  3076. }
  3077. }
  3078. insn->visited |= visited;
  3079. if (propagate_alt_cfi(file, insn))
  3080. return 1;
  3081. if (!insn->ignore_alts && insn->alts) {
  3082. bool skip_orig = false;
  3083. for (alt = insn->alts; alt; alt = alt->next) {
  3084. if (alt->skip_orig)
  3085. skip_orig = true;
  3086. ret = validate_branch(file, func, alt->insn, state);
  3087. if (ret) {
  3088. BT_INSN(insn, "(alt)");
  3089. return ret;
  3090. }
  3091. }
  3092. if (skip_orig)
  3093. return 0;
  3094. }
  3095. if (handle_insn_ops(insn, next_insn, &state))
  3096. return 1;
  3097. switch (insn->type) {
  3098. case INSN_RETURN:
  3099. return validate_return(func, insn, &state);
  3100. case INSN_CALL:
  3101. case INSN_CALL_DYNAMIC:
  3102. ret = validate_call(file, insn, &state);
  3103. if (ret)
  3104. return ret;
  3105. if (opts.stackval && func && !is_special_call(insn) &&
  3106. !has_valid_stack_frame(&state)) {
  3107. WARN_INSN(insn, "call without frame pointer save/setup");
  3108. return 1;
  3109. }
  3110. if (insn->dead_end)
  3111. return 0;
  3112. break;
  3113. case INSN_JUMP_CONDITIONAL:
  3114. case INSN_JUMP_UNCONDITIONAL:
  3115. if (is_sibling_call(insn)) {
  3116. ret = validate_sibling_call(file, insn, &state);
  3117. if (ret)
  3118. return ret;
  3119. } else if (insn->jump_dest) {
  3120. ret = validate_branch(file, func,
  3121. insn->jump_dest, state);
  3122. if (ret) {
  3123. BT_INSN(insn, "(branch)");
  3124. return ret;
  3125. }
  3126. }
  3127. if (insn->type == INSN_JUMP_UNCONDITIONAL)
  3128. return 0;
  3129. break;
  3130. case INSN_JUMP_DYNAMIC:
  3131. case INSN_JUMP_DYNAMIC_CONDITIONAL:
  3132. if (is_sibling_call(insn)) {
  3133. ret = validate_sibling_call(file, insn, &state);
  3134. if (ret)
  3135. return ret;
  3136. }
  3137. if (insn->type == INSN_JUMP_DYNAMIC)
  3138. return 0;
  3139. break;
  3140. case INSN_CONTEXT_SWITCH:
  3141. if (func) {
  3142. if (!next_insn || !next_insn->hint) {
  3143. WARN_INSN(insn, "unsupported instruction in callable function");
  3144. return 1;
  3145. }
  3146. break;
  3147. }
  3148. return 0;
  3149. case INSN_STAC:
  3150. if (!opts.uaccess)
  3151. break;
  3152. if (state.uaccess) {
  3153. WARN_INSN(insn, "recursive UACCESS enable");
  3154. return 1;
  3155. }
  3156. state.uaccess = true;
  3157. break;
  3158. case INSN_CLAC:
  3159. if (!opts.uaccess)
  3160. break;
  3161. if (!state.uaccess && func) {
  3162. WARN_INSN(insn, "redundant UACCESS disable");
  3163. return 1;
  3164. }
  3165. if (func_uaccess_safe(func) && !state.uaccess_stack) {
  3166. WARN_INSN(insn, "UACCESS-safe disables UACCESS");
  3167. return 1;
  3168. }
  3169. state.uaccess = false;
  3170. break;
  3171. case INSN_STD:
  3172. if (state.df) {
  3173. WARN_INSN(insn, "recursive STD");
  3174. return 1;
  3175. }
  3176. state.df = true;
  3177. break;
  3178. case INSN_CLD:
  3179. if (!state.df && func) {
  3180. WARN_INSN(insn, "redundant CLD");
  3181. return 1;
  3182. }
  3183. state.df = false;
  3184. break;
  3185. default:
  3186. break;
  3187. }
  3188. if (insn->dead_end)
  3189. return 0;
  3190. if (!next_insn) {
  3191. if (state.cfi.cfa.base == CFI_UNDEFINED)
  3192. return 0;
  3193. if (file->ignore_unreachables)
  3194. return 0;
  3195. WARN("%s: unexpected end of section", sec->name);
  3196. return 1;
  3197. }
  3198. prev_insn = insn;
  3199. insn = next_insn;
  3200. }
  3201. return 0;
  3202. }
  3203. static int validate_unwind_hint(struct objtool_file *file,
  3204. struct instruction *insn,
  3205. struct insn_state *state)
  3206. {
  3207. if (insn->hint && !insn->visited && !insn->ignore) {
  3208. int ret = validate_branch(file, insn_func(insn), insn, *state);
  3209. if (ret)
  3210. BT_INSN(insn, "<=== (hint)");
  3211. return ret;
  3212. }
  3213. return 0;
  3214. }
  3215. static int validate_unwind_hints(struct objtool_file *file, struct section *sec)
  3216. {
  3217. struct instruction *insn;
  3218. struct insn_state state;
  3219. int warnings = 0;
  3220. if (!file->hints)
  3221. return 0;
  3222. init_insn_state(file, &state, sec);
  3223. if (sec) {
  3224. sec_for_each_insn(file, sec, insn)
  3225. warnings += validate_unwind_hint(file, insn, &state);
  3226. } else {
  3227. for_each_insn(file, insn)
  3228. warnings += validate_unwind_hint(file, insn, &state);
  3229. }
  3230. return warnings;
  3231. }
  3232. /*
  3233. * Validate rethunk entry constraint: must untrain RET before the first RET.
  3234. *
  3235. * Follow every branch (intra-function) and ensure VALIDATE_UNRET_END comes
  3236. * before an actual RET instruction.
  3237. */
  3238. static int validate_unret(struct objtool_file *file, struct instruction *insn)
  3239. {
  3240. struct instruction *next, *dest;
  3241. int ret;
  3242. for (;;) {
  3243. next = next_insn_to_validate(file, insn);
  3244. if (insn->visited & VISITED_UNRET)
  3245. return 0;
  3246. insn->visited |= VISITED_UNRET;
  3247. if (!insn->ignore_alts && insn->alts) {
  3248. struct alternative *alt;
  3249. bool skip_orig = false;
  3250. for (alt = insn->alts; alt; alt = alt->next) {
  3251. if (alt->skip_orig)
  3252. skip_orig = true;
  3253. ret = validate_unret(file, alt->insn);
  3254. if (ret) {
  3255. BT_INSN(insn, "(alt)");
  3256. return ret;
  3257. }
  3258. }
  3259. if (skip_orig)
  3260. return 0;
  3261. }
  3262. switch (insn->type) {
  3263. case INSN_CALL_DYNAMIC:
  3264. case INSN_JUMP_DYNAMIC:
  3265. case INSN_JUMP_DYNAMIC_CONDITIONAL:
  3266. WARN_INSN(insn, "early indirect call");
  3267. return 1;
  3268. case INSN_JUMP_UNCONDITIONAL:
  3269. case INSN_JUMP_CONDITIONAL:
  3270. if (!is_sibling_call(insn)) {
  3271. if (!insn->jump_dest) {
  3272. WARN_INSN(insn, "unresolved jump target after linking?!?");
  3273. return -1;
  3274. }
  3275. ret = validate_unret(file, insn->jump_dest);
  3276. if (ret) {
  3277. BT_INSN(insn, "(branch%s)",
  3278. insn->type == INSN_JUMP_CONDITIONAL ? "-cond" : "");
  3279. return ret;
  3280. }
  3281. if (insn->type == INSN_JUMP_UNCONDITIONAL)
  3282. return 0;
  3283. break;
  3284. }
  3285. /* fallthrough */
  3286. case INSN_CALL:
  3287. dest = find_insn(file, insn_call_dest(insn)->sec,
  3288. insn_call_dest(insn)->offset);
  3289. if (!dest) {
  3290. WARN("Unresolved function after linking!?: %s",
  3291. insn_call_dest(insn)->name);
  3292. return -1;
  3293. }
  3294. ret = validate_unret(file, dest);
  3295. if (ret) {
  3296. BT_INSN(insn, "(call)");
  3297. return ret;
  3298. }
  3299. /*
  3300. * If a call returns without error, it must have seen UNTRAIN_RET.
  3301. * Therefore any non-error return is a success.
  3302. */
  3303. return 0;
  3304. case INSN_RETURN:
  3305. WARN_INSN(insn, "RET before UNTRAIN");
  3306. return 1;
  3307. case INSN_CONTEXT_SWITCH:
  3308. if (insn_func(insn))
  3309. break;
  3310. return 0;
  3311. case INSN_NOP:
  3312. if (insn->retpoline_safe)
  3313. return 0;
  3314. break;
  3315. default:
  3316. break;
  3317. }
  3318. if (insn->dead_end)
  3319. return 0;
  3320. if (!next) {
  3321. WARN_INSN(insn, "teh end!");
  3322. return -1;
  3323. }
  3324. insn = next;
  3325. }
  3326. return 0;
  3327. }
  3328. /*
  3329. * Validate that all branches starting at VALIDATE_UNRET_BEGIN encounter
  3330. * VALIDATE_UNRET_END before RET.
  3331. */
  3332. static int validate_unrets(struct objtool_file *file)
  3333. {
  3334. struct instruction *insn;
  3335. int ret, warnings = 0;
  3336. for_each_insn(file, insn) {
  3337. if (!insn->unret)
  3338. continue;
  3339. ret = validate_unret(file, insn);
  3340. if (ret < 0) {
  3341. WARN_INSN(insn, "Failed UNRET validation");
  3342. return ret;
  3343. }
  3344. warnings += ret;
  3345. }
  3346. return warnings;
  3347. }
  3348. static int validate_retpoline(struct objtool_file *file)
  3349. {
  3350. struct instruction *insn;
  3351. int warnings = 0;
  3352. for_each_insn(file, insn) {
  3353. if (insn->type != INSN_JUMP_DYNAMIC &&
  3354. insn->type != INSN_CALL_DYNAMIC &&
  3355. insn->type != INSN_RETURN)
  3356. continue;
  3357. if (insn->retpoline_safe)
  3358. continue;
  3359. if (insn->sec->init)
  3360. continue;
  3361. if (insn->type == INSN_RETURN) {
  3362. if (opts.rethunk) {
  3363. WARN_INSN(insn, "'naked' return found in MITIGATION_RETHUNK build");
  3364. } else
  3365. continue;
  3366. } else {
  3367. WARN_INSN(insn, "indirect %s found in MITIGATION_RETPOLINE build",
  3368. insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
  3369. }
  3370. warnings++;
  3371. }
  3372. return warnings;
  3373. }
  3374. static bool is_kasan_insn(struct instruction *insn)
  3375. {
  3376. return (insn->type == INSN_CALL &&
  3377. !strcmp(insn_call_dest(insn)->name, "__asan_handle_no_return"));
  3378. }
  3379. static bool is_ubsan_insn(struct instruction *insn)
  3380. {
  3381. return (insn->type == INSN_CALL &&
  3382. !strcmp(insn_call_dest(insn)->name,
  3383. "__ubsan_handle_builtin_unreachable"));
  3384. }
  3385. static bool ignore_unreachable_insn(struct objtool_file *file, struct instruction *insn)
  3386. {
  3387. int i;
  3388. struct instruction *prev_insn;
  3389. if (insn->ignore || insn->type == INSN_NOP || insn->type == INSN_TRAP)
  3390. return true;
  3391. /*
  3392. * Ignore alternative replacement instructions. This can happen
  3393. * when a whitelisted function uses one of the ALTERNATIVE macros.
  3394. */
  3395. if (!strcmp(insn->sec->name, ".altinstr_replacement") ||
  3396. !strcmp(insn->sec->name, ".altinstr_aux"))
  3397. return true;
  3398. /*
  3399. * Whole archive runs might encounter dead code from weak symbols.
  3400. * This is where the linker will have dropped the weak symbol in
  3401. * favour of a regular symbol, but leaves the code in place.
  3402. *
  3403. * In this case we'll find a piece of code (whole function) that is not
  3404. * covered by a !section symbol. Ignore them.
  3405. */
  3406. if (opts.link && !insn_func(insn)) {
  3407. int size = find_symbol_hole_containing(insn->sec, insn->offset);
  3408. unsigned long end = insn->offset + size;
  3409. if (!size) /* not a hole */
  3410. return false;
  3411. if (size < 0) /* hole until the end */
  3412. return true;
  3413. sec_for_each_insn_continue(file, insn) {
  3414. /*
  3415. * If we reach a visited instruction at or before the
  3416. * end of the hole, ignore the unreachable.
  3417. */
  3418. if (insn->visited)
  3419. return true;
  3420. if (insn->offset >= end)
  3421. break;
  3422. /*
  3423. * If this hole jumps to a .cold function, mark it ignore too.
  3424. */
  3425. if (insn->jump_dest && insn_func(insn->jump_dest) &&
  3426. strstr(insn_func(insn->jump_dest)->name, ".cold")) {
  3427. struct instruction *dest = insn->jump_dest;
  3428. func_for_each_insn(file, insn_func(dest), dest)
  3429. dest->ignore = true;
  3430. }
  3431. }
  3432. return false;
  3433. }
  3434. if (!insn_func(insn))
  3435. return false;
  3436. if (insn_func(insn)->static_call_tramp)
  3437. return true;
  3438. /*
  3439. * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
  3440. * __builtin_unreachable(). The BUG() macro has an unreachable() after
  3441. * the UD2, which causes GCC's undefined trap logic to emit another UD2
  3442. * (or occasionally a JMP to UD2).
  3443. *
  3444. * It may also insert a UD2 after calling a __noreturn function.
  3445. */
  3446. prev_insn = prev_insn_same_sec(file, insn);
  3447. if (prev_insn && prev_insn->dead_end &&
  3448. (insn->type == INSN_BUG ||
  3449. (insn->type == INSN_JUMP_UNCONDITIONAL &&
  3450. insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
  3451. return true;
  3452. /*
  3453. * Check if this (or a subsequent) instruction is related to
  3454. * CONFIG_UBSAN or CONFIG_KASAN.
  3455. *
  3456. * End the search at 5 instructions to avoid going into the weeds.
  3457. */
  3458. for (i = 0; i < 5; i++) {
  3459. if (is_kasan_insn(insn) || is_ubsan_insn(insn))
  3460. return true;
  3461. if (insn->type == INSN_JUMP_UNCONDITIONAL) {
  3462. if (insn->jump_dest &&
  3463. insn_func(insn->jump_dest) == insn_func(insn)) {
  3464. insn = insn->jump_dest;
  3465. continue;
  3466. }
  3467. break;
  3468. }
  3469. if (insn->offset + insn->len >= insn_func(insn)->offset + insn_func(insn)->len)
  3470. break;
  3471. insn = next_insn_same_sec(file, insn);
  3472. }
  3473. return false;
  3474. }
  3475. static int add_prefix_symbol(struct objtool_file *file, struct symbol *func)
  3476. {
  3477. struct instruction *insn, *prev;
  3478. struct cfi_state *cfi;
  3479. insn = find_insn(file, func->sec, func->offset);
  3480. if (!insn)
  3481. return -1;
  3482. for (prev = prev_insn_same_sec(file, insn);
  3483. prev;
  3484. prev = prev_insn_same_sec(file, prev)) {
  3485. u64 offset;
  3486. if (prev->type != INSN_NOP)
  3487. return -1;
  3488. offset = func->offset - prev->offset;
  3489. if (offset > opts.prefix)
  3490. return -1;
  3491. if (offset < opts.prefix)
  3492. continue;
  3493. elf_create_prefix_symbol(file->elf, func, opts.prefix);
  3494. break;
  3495. }
  3496. if (!prev)
  3497. return -1;
  3498. if (!insn->cfi) {
  3499. /*
  3500. * This can happen if stack validation isn't enabled or the
  3501. * function is annotated with STACK_FRAME_NON_STANDARD.
  3502. */
  3503. return 0;
  3504. }
  3505. /* Propagate insn->cfi to the prefix code */
  3506. cfi = cfi_hash_find_or_add(insn->cfi);
  3507. for (; prev != insn; prev = next_insn_same_sec(file, prev))
  3508. prev->cfi = cfi;
  3509. return 0;
  3510. }
  3511. static int add_prefix_symbols(struct objtool_file *file)
  3512. {
  3513. struct section *sec;
  3514. struct symbol *func;
  3515. for_each_sec(file, sec) {
  3516. if (!(sec->sh.sh_flags & SHF_EXECINSTR))
  3517. continue;
  3518. sec_for_each_sym(sec, func) {
  3519. if (func->type != STT_FUNC)
  3520. continue;
  3521. add_prefix_symbol(file, func);
  3522. }
  3523. }
  3524. return 0;
  3525. }
  3526. static int validate_symbol(struct objtool_file *file, struct section *sec,
  3527. struct symbol *sym, struct insn_state *state)
  3528. {
  3529. struct instruction *insn;
  3530. int ret;
  3531. if (!sym->len) {
  3532. WARN("%s() is missing an ELF size annotation", sym->name);
  3533. return 1;
  3534. }
  3535. if (sym->pfunc != sym || sym->alias != sym)
  3536. return 0;
  3537. insn = find_insn(file, sec, sym->offset);
  3538. if (!insn || insn->ignore || insn->visited)
  3539. return 0;
  3540. if (opts.uaccess)
  3541. state->uaccess = sym->uaccess_safe;
  3542. ret = validate_branch(file, insn_func(insn), insn, *state);
  3543. if (ret)
  3544. BT_INSN(insn, "<=== (sym)");
  3545. return ret;
  3546. }
  3547. static int validate_section(struct objtool_file *file, struct section *sec)
  3548. {
  3549. struct insn_state state;
  3550. struct symbol *func;
  3551. int warnings = 0;
  3552. sec_for_each_sym(sec, func) {
  3553. if (func->type != STT_FUNC)
  3554. continue;
  3555. init_insn_state(file, &state, sec);
  3556. set_func_state(&state.cfi);
  3557. warnings += validate_symbol(file, sec, func, &state);
  3558. }
  3559. return warnings;
  3560. }
  3561. static int validate_noinstr_sections(struct objtool_file *file)
  3562. {
  3563. struct section *sec;
  3564. int warnings = 0;
  3565. sec = find_section_by_name(file->elf, ".noinstr.text");
  3566. if (sec) {
  3567. warnings += validate_section(file, sec);
  3568. warnings += validate_unwind_hints(file, sec);
  3569. }
  3570. sec = find_section_by_name(file->elf, ".entry.text");
  3571. if (sec) {
  3572. warnings += validate_section(file, sec);
  3573. warnings += validate_unwind_hints(file, sec);
  3574. }
  3575. sec = find_section_by_name(file->elf, ".cpuidle.text");
  3576. if (sec) {
  3577. warnings += validate_section(file, sec);
  3578. warnings += validate_unwind_hints(file, sec);
  3579. }
  3580. return warnings;
  3581. }
  3582. static int validate_functions(struct objtool_file *file)
  3583. {
  3584. struct section *sec;
  3585. int warnings = 0;
  3586. for_each_sec(file, sec) {
  3587. if (!(sec->sh.sh_flags & SHF_EXECINSTR))
  3588. continue;
  3589. warnings += validate_section(file, sec);
  3590. }
  3591. return warnings;
  3592. }
  3593. static void mark_endbr_used(struct instruction *insn)
  3594. {
  3595. if (!list_empty(&insn->call_node))
  3596. list_del_init(&insn->call_node);
  3597. }
  3598. static bool noendbr_range(struct objtool_file *file, struct instruction *insn)
  3599. {
  3600. struct symbol *sym = find_symbol_containing(insn->sec, insn->offset-1);
  3601. struct instruction *first;
  3602. if (!sym)
  3603. return false;
  3604. first = find_insn(file, sym->sec, sym->offset);
  3605. if (!first)
  3606. return false;
  3607. if (first->type != INSN_ENDBR && !first->noendbr)
  3608. return false;
  3609. return insn->offset == sym->offset + sym->len;
  3610. }
  3611. static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn)
  3612. {
  3613. struct instruction *dest;
  3614. struct reloc *reloc;
  3615. unsigned long off;
  3616. int warnings = 0;
  3617. /*
  3618. * Looking for function pointer load relocations. Ignore
  3619. * direct/indirect branches:
  3620. */
  3621. switch (insn->type) {
  3622. case INSN_CALL:
  3623. case INSN_CALL_DYNAMIC:
  3624. case INSN_JUMP_CONDITIONAL:
  3625. case INSN_JUMP_UNCONDITIONAL:
  3626. case INSN_JUMP_DYNAMIC:
  3627. case INSN_JUMP_DYNAMIC_CONDITIONAL:
  3628. case INSN_RETURN:
  3629. case INSN_NOP:
  3630. return 0;
  3631. default:
  3632. break;
  3633. }
  3634. for (reloc = insn_reloc(file, insn);
  3635. reloc;
  3636. reloc = find_reloc_by_dest_range(file->elf, insn->sec,
  3637. reloc_offset(reloc) + 1,
  3638. (insn->offset + insn->len) - (reloc_offset(reloc) + 1))) {
  3639. /*
  3640. * static_call_update() references the trampoline, which
  3641. * doesn't have (or need) ENDBR. Skip warning in that case.
  3642. */
  3643. if (reloc->sym->static_call_tramp)
  3644. continue;
  3645. off = reloc->sym->offset;
  3646. if (reloc_type(reloc) == R_X86_64_PC32 ||
  3647. reloc_type(reloc) == R_X86_64_PLT32)
  3648. off += arch_dest_reloc_offset(reloc_addend(reloc));
  3649. else
  3650. off += reloc_addend(reloc);
  3651. dest = find_insn(file, reloc->sym->sec, off);
  3652. if (!dest)
  3653. continue;
  3654. if (dest->type == INSN_ENDBR) {
  3655. mark_endbr_used(dest);
  3656. continue;
  3657. }
  3658. if (insn_func(dest) && insn_func(insn) &&
  3659. insn_func(dest)->pfunc == insn_func(insn)->pfunc) {
  3660. /*
  3661. * Anything from->to self is either _THIS_IP_ or
  3662. * IRET-to-self.
  3663. *
  3664. * There is no sane way to annotate _THIS_IP_ since the
  3665. * compiler treats the relocation as a constant and is
  3666. * happy to fold in offsets, skewing any annotation we
  3667. * do, leading to vast amounts of false-positives.
  3668. *
  3669. * There's also compiler generated _THIS_IP_ through
  3670. * KCOV and such which we have no hope of annotating.
  3671. *
  3672. * As such, blanket accept self-references without
  3673. * issue.
  3674. */
  3675. continue;
  3676. }
  3677. /*
  3678. * Accept anything ANNOTATE_NOENDBR.
  3679. */
  3680. if (dest->noendbr)
  3681. continue;
  3682. /*
  3683. * Accept if this is the instruction after a symbol
  3684. * that is (no)endbr -- typical code-range usage.
  3685. */
  3686. if (noendbr_range(file, dest))
  3687. continue;
  3688. WARN_INSN(insn, "relocation to !ENDBR: %s", offstr(dest->sec, dest->offset));
  3689. warnings++;
  3690. }
  3691. return warnings;
  3692. }
  3693. static int validate_ibt_data_reloc(struct objtool_file *file,
  3694. struct reloc *reloc)
  3695. {
  3696. struct instruction *dest;
  3697. dest = find_insn(file, reloc->sym->sec,
  3698. reloc->sym->offset + reloc_addend(reloc));
  3699. if (!dest)
  3700. return 0;
  3701. if (dest->type == INSN_ENDBR) {
  3702. mark_endbr_used(dest);
  3703. return 0;
  3704. }
  3705. if (dest->noendbr)
  3706. return 0;
  3707. WARN_FUNC("data relocation to !ENDBR: %s",
  3708. reloc->sec->base, reloc_offset(reloc),
  3709. offstr(dest->sec, dest->offset));
  3710. return 1;
  3711. }
  3712. /*
  3713. * Validate IBT rules and remove used ENDBR instructions from the seal list.
  3714. * Unused ENDBR instructions will be annotated for sealing (i.e., replaced with
  3715. * NOPs) later, in create_ibt_endbr_seal_sections().
  3716. */
  3717. static int validate_ibt(struct objtool_file *file)
  3718. {
  3719. struct section *sec;
  3720. struct reloc *reloc;
  3721. struct instruction *insn;
  3722. int warnings = 0;
  3723. for_each_insn(file, insn)
  3724. warnings += validate_ibt_insn(file, insn);
  3725. for_each_sec(file, sec) {
  3726. /* Already done by validate_ibt_insn() */
  3727. if (sec->sh.sh_flags & SHF_EXECINSTR)
  3728. continue;
  3729. if (!sec->rsec)
  3730. continue;
  3731. /*
  3732. * These sections can reference text addresses, but not with
  3733. * the intent to indirect branch to them.
  3734. */
  3735. if ((!strncmp(sec->name, ".discard", 8) &&
  3736. strcmp(sec->name, ".discard.ibt_endbr_noseal")) ||
  3737. !strncmp(sec->name, ".debug", 6) ||
  3738. !strcmp(sec->name, ".altinstructions") ||
  3739. !strcmp(sec->name, ".ibt_endbr_seal") ||
  3740. !strcmp(sec->name, ".orc_unwind_ip") ||
  3741. !strcmp(sec->name, ".parainstructions") ||
  3742. !strcmp(sec->name, ".retpoline_sites") ||
  3743. !strcmp(sec->name, ".smp_locks") ||
  3744. !strcmp(sec->name, ".static_call_sites") ||
  3745. !strcmp(sec->name, "_error_injection_whitelist") ||
  3746. !strcmp(sec->name, "_kprobe_blacklist") ||
  3747. !strcmp(sec->name, "__bug_table") ||
  3748. !strcmp(sec->name, "__ex_table") ||
  3749. !strcmp(sec->name, "__jump_table") ||
  3750. !strcmp(sec->name, "__mcount_loc") ||
  3751. !strcmp(sec->name, ".kcfi_traps") ||
  3752. strstr(sec->name, "__patchable_function_entries"))
  3753. continue;
  3754. for_each_reloc(sec->rsec, reloc)
  3755. warnings += validate_ibt_data_reloc(file, reloc);
  3756. }
  3757. return warnings;
  3758. }
  3759. static int validate_sls(struct objtool_file *file)
  3760. {
  3761. struct instruction *insn, *next_insn;
  3762. int warnings = 0;
  3763. for_each_insn(file, insn) {
  3764. next_insn = next_insn_same_sec(file, insn);
  3765. if (insn->retpoline_safe)
  3766. continue;
  3767. switch (insn->type) {
  3768. case INSN_RETURN:
  3769. if (!next_insn || next_insn->type != INSN_TRAP) {
  3770. WARN_INSN(insn, "missing int3 after ret");
  3771. warnings++;
  3772. }
  3773. break;
  3774. case INSN_JUMP_DYNAMIC:
  3775. if (!next_insn || next_insn->type != INSN_TRAP) {
  3776. WARN_INSN(insn, "missing int3 after indirect jump");
  3777. warnings++;
  3778. }
  3779. break;
  3780. default:
  3781. break;
  3782. }
  3783. }
  3784. return warnings;
  3785. }
  3786. static int validate_reachable_instructions(struct objtool_file *file)
  3787. {
  3788. struct instruction *insn, *prev_insn;
  3789. struct symbol *call_dest;
  3790. int warnings = 0;
  3791. if (file->ignore_unreachables)
  3792. return 0;
  3793. for_each_insn(file, insn) {
  3794. if (insn->visited || ignore_unreachable_insn(file, insn))
  3795. continue;
  3796. prev_insn = prev_insn_same_sec(file, insn);
  3797. if (prev_insn && prev_insn->dead_end) {
  3798. call_dest = insn_call_dest(prev_insn);
  3799. if (call_dest) {
  3800. WARN_INSN(insn, "%s() is missing a __noreturn annotation",
  3801. call_dest->name);
  3802. warnings++;
  3803. continue;
  3804. }
  3805. }
  3806. WARN_INSN(insn, "unreachable instruction");
  3807. warnings++;
  3808. }
  3809. return warnings;
  3810. }
  3811. /* 'funcs' is a space-separated list of function names */
  3812. static int disas_funcs(const char *funcs)
  3813. {
  3814. const char *objdump_str, *cross_compile;
  3815. int size, ret;
  3816. char *cmd;
  3817. cross_compile = getenv("CROSS_COMPILE");
  3818. if (!cross_compile)
  3819. cross_compile = "";
  3820. objdump_str = "%sobjdump -wdr %s | gawk -M -v _funcs='%s' '"
  3821. "BEGIN { split(_funcs, funcs); }"
  3822. "/^$/ { func_match = 0; }"
  3823. "/<.*>:/ { "
  3824. "f = gensub(/.*<(.*)>:/, \"\\\\1\", 1);"
  3825. "for (i in funcs) {"
  3826. "if (funcs[i] == f) {"
  3827. "func_match = 1;"
  3828. "base = strtonum(\"0x\" $1);"
  3829. "break;"
  3830. "}"
  3831. "}"
  3832. "}"
  3833. "{"
  3834. "if (func_match) {"
  3835. "addr = strtonum(\"0x\" $1);"
  3836. "printf(\"%%04x \", addr - base);"
  3837. "print;"
  3838. "}"
  3839. "}' 1>&2";
  3840. /* fake snprintf() to calculate the size */
  3841. size = snprintf(NULL, 0, objdump_str, cross_compile, objname, funcs) + 1;
  3842. if (size <= 0) {
  3843. WARN("objdump string size calculation failed");
  3844. return -1;
  3845. }
  3846. cmd = malloc(size);
  3847. /* real snprintf() */
  3848. snprintf(cmd, size, objdump_str, cross_compile, objname, funcs);
  3849. ret = system(cmd);
  3850. if (ret) {
  3851. WARN("disassembly failed: %d", ret);
  3852. return -1;
  3853. }
  3854. return 0;
  3855. }
  3856. static int disas_warned_funcs(struct objtool_file *file)
  3857. {
  3858. struct symbol *sym;
  3859. char *funcs = NULL, *tmp;
  3860. for_each_sym(file, sym) {
  3861. if (sym->warned) {
  3862. if (!funcs) {
  3863. funcs = malloc(strlen(sym->name) + 1);
  3864. strcpy(funcs, sym->name);
  3865. } else {
  3866. tmp = malloc(strlen(funcs) + strlen(sym->name) + 2);
  3867. sprintf(tmp, "%s %s", funcs, sym->name);
  3868. free(funcs);
  3869. funcs = tmp;
  3870. }
  3871. }
  3872. }
  3873. if (funcs)
  3874. disas_funcs(funcs);
  3875. return 0;
  3876. }
  3877. struct insn_chunk {
  3878. void *addr;
  3879. struct insn_chunk *next;
  3880. };
  3881. /*
  3882. * Reduce peak RSS usage by freeing insns memory before writing the ELF file,
  3883. * which can trigger more allocations for .debug_* sections whose data hasn't
  3884. * been read yet.
  3885. */
  3886. static void free_insns(struct objtool_file *file)
  3887. {
  3888. struct instruction *insn;
  3889. struct insn_chunk *chunks = NULL, *chunk;
  3890. for_each_insn(file, insn) {
  3891. if (!insn->idx) {
  3892. chunk = malloc(sizeof(*chunk));
  3893. chunk->addr = insn;
  3894. chunk->next = chunks;
  3895. chunks = chunk;
  3896. }
  3897. }
  3898. for (chunk = chunks; chunk; chunk = chunk->next)
  3899. free(chunk->addr);
  3900. }
  3901. int check(struct objtool_file *file)
  3902. {
  3903. int ret, warnings = 0;
  3904. arch_initial_func_cfi_state(&initial_func_cfi);
  3905. init_cfi_state(&init_cfi);
  3906. init_cfi_state(&func_cfi);
  3907. set_func_state(&func_cfi);
  3908. init_cfi_state(&force_undefined_cfi);
  3909. force_undefined_cfi.force_undefined = true;
  3910. if (!cfi_hash_alloc(1UL << (file->elf->symbol_bits - 3))) {
  3911. ret = -1;
  3912. goto out;
  3913. }
  3914. cfi_hash_add(&init_cfi);
  3915. cfi_hash_add(&func_cfi);
  3916. ret = decode_sections(file);
  3917. if (ret < 0)
  3918. goto out;
  3919. warnings += ret;
  3920. if (!nr_insns)
  3921. goto out;
  3922. if (opts.retpoline) {
  3923. ret = validate_retpoline(file);
  3924. if (ret < 0)
  3925. goto out;
  3926. warnings += ret;
  3927. }
  3928. if (opts.stackval || opts.orc || opts.uaccess) {
  3929. ret = validate_functions(file);
  3930. if (ret < 0)
  3931. goto out;
  3932. warnings += ret;
  3933. ret = validate_unwind_hints(file, NULL);
  3934. if (ret < 0)
  3935. goto out;
  3936. warnings += ret;
  3937. if (!warnings) {
  3938. ret = validate_reachable_instructions(file);
  3939. if (ret < 0)
  3940. goto out;
  3941. warnings += ret;
  3942. }
  3943. } else if (opts.noinstr) {
  3944. ret = validate_noinstr_sections(file);
  3945. if (ret < 0)
  3946. goto out;
  3947. warnings += ret;
  3948. }
  3949. if (opts.unret) {
  3950. /*
  3951. * Must be after validate_branch() and friends, it plays
  3952. * further games with insn->visited.
  3953. */
  3954. ret = validate_unrets(file);
  3955. if (ret < 0)
  3956. goto out;
  3957. warnings += ret;
  3958. }
  3959. if (opts.ibt) {
  3960. ret = validate_ibt(file);
  3961. if (ret < 0)
  3962. goto out;
  3963. warnings += ret;
  3964. }
  3965. if (opts.sls) {
  3966. ret = validate_sls(file);
  3967. if (ret < 0)
  3968. goto out;
  3969. warnings += ret;
  3970. }
  3971. if (opts.static_call) {
  3972. ret = create_static_call_sections(file);
  3973. if (ret < 0)
  3974. goto out;
  3975. warnings += ret;
  3976. }
  3977. if (opts.retpoline) {
  3978. ret = create_retpoline_sites_sections(file);
  3979. if (ret < 0)
  3980. goto out;
  3981. warnings += ret;
  3982. }
  3983. if (opts.cfi) {
  3984. ret = create_cfi_sections(file);
  3985. if (ret < 0)
  3986. goto out;
  3987. warnings += ret;
  3988. }
  3989. if (opts.rethunk) {
  3990. ret = create_return_sites_sections(file);
  3991. if (ret < 0)
  3992. goto out;
  3993. warnings += ret;
  3994. if (opts.hack_skylake) {
  3995. ret = create_direct_call_sections(file);
  3996. if (ret < 0)
  3997. goto out;
  3998. warnings += ret;
  3999. }
  4000. }
  4001. if (opts.mcount) {
  4002. ret = create_mcount_loc_sections(file);
  4003. if (ret < 0)
  4004. goto out;
  4005. warnings += ret;
  4006. }
  4007. if (opts.prefix) {
  4008. ret = add_prefix_symbols(file);
  4009. if (ret < 0)
  4010. goto out;
  4011. warnings += ret;
  4012. }
  4013. if (opts.ibt) {
  4014. ret = create_ibt_endbr_seal_sections(file);
  4015. if (ret < 0)
  4016. goto out;
  4017. warnings += ret;
  4018. }
  4019. if (opts.orc && nr_insns) {
  4020. ret = orc_create(file);
  4021. if (ret < 0)
  4022. goto out;
  4023. warnings += ret;
  4024. }
  4025. free_insns(file);
  4026. if (opts.verbose)
  4027. disas_warned_funcs(file);
  4028. if (opts.stats) {
  4029. printf("nr_insns_visited: %ld\n", nr_insns_visited);
  4030. printf("nr_cfi: %ld\n", nr_cfi);
  4031. printf("nr_cfi_reused: %ld\n", nr_cfi_reused);
  4032. printf("nr_cfi_cache: %ld\n", nr_cfi_cache);
  4033. }
  4034. out:
  4035. /*
  4036. * For now, don't fail the kernel build on fatal warnings. These
  4037. * errors are still fairly common due to the growing matrix of
  4038. * supported toolchains and their recent pace of change.
  4039. */
  4040. return 0;
  4041. }