elfnn-riscv.c 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301
  1. /* RISC-V-specific support for NN-bit ELF.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by Andrew Waterman (andrew@sifive.com).
  4. Based on TILE-Gx and MIPS targets.
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING3. If not,
  16. see <http://www.gnu.org/licenses/>. */
  17. /* This file handles RISC-V ELF targets. */
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "libbfd.h"
  21. #include "bfdlink.h"
  22. #include "genlink.h"
  23. #include "elf-bfd.h"
  24. #include "elfxx-riscv.h"
  25. #include "elf/riscv.h"
  26. #include "opcode/riscv.h"
  27. #include "objalloc.h"
  28. #include <limits.h>
  29. #ifndef CHAR_BIT
  30. #define CHAR_BIT 8
  31. #endif
  32. /* Internal relocations used exclusively by the relaxation pass. */
  33. #define R_RISCV_DELETE (R_RISCV_max + 1)
  34. #define ARCH_SIZE NN
  35. #define MINUS_ONE ((bfd_vma)0 - 1)
  36. #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
  37. #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
  38. /* The name of the dynamic interpreter. This is put in the .interp
  39. section. */
  40. #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
  41. #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
  42. #define ELF_ARCH bfd_arch_riscv
  43. #define ELF_TARGET_ID RISCV_ELF_DATA
  44. #define ELF_MACHINE_CODE EM_RISCV
  45. #define ELF_MAXPAGESIZE 0x1000
  46. #define ELF_COMMONPAGESIZE 0x1000
  47. #define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
  48. /* RISC-V ELF linker hash entry. */
  49. struct riscv_elf_link_hash_entry
  50. {
  51. struct elf_link_hash_entry elf;
  52. #define GOT_UNKNOWN 0
  53. #define GOT_NORMAL 1
  54. #define GOT_TLS_GD 2
  55. #define GOT_TLS_IE 4
  56. #define GOT_TLS_LE 8
  57. char tls_type;
  58. };
  59. #define riscv_elf_hash_entry(ent) \
  60. ((struct riscv_elf_link_hash_entry *) (ent))
  61. struct _bfd_riscv_elf_obj_tdata
  62. {
  63. struct elf_obj_tdata root;
  64. /* tls_type for each local got entry. */
  65. char *local_got_tls_type;
  66. };
  67. #define _bfd_riscv_elf_tdata(abfd) \
  68. ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
  69. #define _bfd_riscv_elf_local_got_tls_type(abfd) \
  70. (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
  71. #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
  72. (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
  73. : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
  74. #define is_riscv_elf(bfd) \
  75. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  76. && elf_tdata (bfd) != NULL \
  77. && elf_object_id (bfd) == RISCV_ELF_DATA)
  78. static bool
  79. elfNN_riscv_mkobject (bfd *abfd)
  80. {
  81. return bfd_elf_allocate_object (abfd,
  82. sizeof (struct _bfd_riscv_elf_obj_tdata),
  83. RISCV_ELF_DATA);
  84. }
  85. #include "elf/common.h"
  86. #include "elf/internal.h"
  87. struct riscv_elf_link_hash_table
  88. {
  89. struct elf_link_hash_table elf;
  90. /* Short-cuts to get to dynamic linker sections. */
  91. asection *sdyntdata;
  92. /* The max alignment of output sections. */
  93. bfd_vma max_alignment;
  94. /* Used by local STT_GNU_IFUNC symbols. */
  95. htab_t loc_hash_table;
  96. void * loc_hash_memory;
  97. /* The index of the last unused .rel.iplt slot. */
  98. bfd_vma last_iplt_index;
  99. /* The data segment phase, don't relax the section
  100. when it is exp_seg_relro_adjust. */
  101. int *data_segment_phase;
  102. /* Relocations for variant CC symbols may be present. */
  103. int variant_cc;
  104. };
  105. /* Instruction access functions. */
  106. #define riscv_get_insn(bits, ptr) \
  107. ((bits) == 16 ? bfd_getl16 (ptr) \
  108. : (bits) == 32 ? bfd_getl32 (ptr) \
  109. : (bits) == 64 ? bfd_getl64 (ptr) \
  110. : (abort (), (bfd_vma) - 1))
  111. #define riscv_put_insn(bits, val, ptr) \
  112. ((bits) == 16 ? bfd_putl16 (val, ptr) \
  113. : (bits) == 32 ? bfd_putl32 (val, ptr) \
  114. : (bits) == 64 ? bfd_putl64 (val, ptr) \
  115. : (abort (), (void) 0))
  116. /* Get the RISC-V ELF linker hash table from a link_info structure. */
  117. #define riscv_elf_hash_table(p) \
  118. ((is_elf_hash_table ((p)->hash) \
  119. && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
  120. ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
  121. static bool
  122. riscv_info_to_howto_rela (bfd *abfd,
  123. arelent *cache_ptr,
  124. Elf_Internal_Rela *dst)
  125. {
  126. cache_ptr->howto = riscv_elf_rtype_to_howto (abfd, ELFNN_R_TYPE (dst->r_info));
  127. return cache_ptr->howto != NULL;
  128. }
  129. static void
  130. riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  131. {
  132. const struct elf_backend_data *bed;
  133. bfd_byte *loc;
  134. bed = get_elf_backend_data (abfd);
  135. loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
  136. bed->s->swap_reloca_out (abfd, rel, loc);
  137. }
  138. /* Return true if a relocation is modifying an instruction. */
  139. static bool
  140. riscv_is_insn_reloc (const reloc_howto_type *howto)
  141. {
  142. /* Heuristic: A multibyte destination with a nontrivial mask
  143. is an instruction */
  144. return (howto->bitsize > 8
  145. && howto->dst_mask != 0
  146. && ~(howto->dst_mask | (howto->bitsize < sizeof(bfd_vma) * CHAR_BIT
  147. ? (MINUS_ONE << howto->bitsize) : (bfd_vma)0)) != 0);
  148. }
  149. /* PLT/GOT stuff. */
  150. #define PLT_HEADER_INSNS 8
  151. #define PLT_ENTRY_INSNS 4
  152. #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
  153. #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
  154. #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
  155. /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
  156. the other is used for link map. Other targets also reserve one more
  157. entry used for runtime profile? */
  158. #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
  159. #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
  160. #if ARCH_SIZE == 32
  161. # define MATCH_LREG MATCH_LW
  162. #else
  163. # define MATCH_LREG MATCH_LD
  164. #endif
  165. /* Generate a PLT header. */
  166. static bool
  167. riscv_make_plt_header (bfd *output_bfd, bfd_vma gotplt_addr, bfd_vma addr,
  168. uint32_t *entry)
  169. {
  170. bfd_vma gotplt_offset_high = RISCV_PCREL_HIGH_PART (gotplt_addr, addr);
  171. bfd_vma gotplt_offset_low = RISCV_PCREL_LOW_PART (gotplt_addr, addr);
  172. /* RVE has no t3 register, so this won't work, and is not supported. */
  173. if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
  174. {
  175. _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
  176. output_bfd);
  177. return false;
  178. }
  179. /* auipc t2, %hi(.got.plt)
  180. sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
  181. l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
  182. addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
  183. addi t0, t2, %lo(.got.plt) # &.got.plt
  184. srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
  185. l[w|d] t0, PTRSIZE(t0) # link map
  186. jr t3 */
  187. entry[0] = RISCV_UTYPE (AUIPC, X_T2, gotplt_offset_high);
  188. entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T3);
  189. entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, gotplt_offset_low);
  190. entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, (uint32_t) -(PLT_HEADER_SIZE + 12));
  191. entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, gotplt_offset_low);
  192. entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
  193. entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
  194. entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
  195. return true;
  196. }
  197. /* Generate a PLT entry. */
  198. static bool
  199. riscv_make_plt_entry (bfd *output_bfd, bfd_vma got, bfd_vma addr,
  200. uint32_t *entry)
  201. {
  202. /* RVE has no t3 register, so this won't work, and is not supported. */
  203. if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
  204. {
  205. _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
  206. output_bfd);
  207. return false;
  208. }
  209. /* auipc t3, %hi(.got.plt entry)
  210. l[w|d] t3, %lo(.got.plt entry)(t3)
  211. jalr t1, t3
  212. nop */
  213. entry[0] = RISCV_UTYPE (AUIPC, X_T3, RISCV_PCREL_HIGH_PART (got, addr));
  214. entry[1] = RISCV_ITYPE (LREG, X_T3, X_T3, RISCV_PCREL_LOW_PART (got, addr));
  215. entry[2] = RISCV_ITYPE (JALR, X_T1, X_T3, 0);
  216. entry[3] = RISCV_NOP;
  217. return true;
  218. }
  219. /* Create an entry in an RISC-V ELF linker hash table. */
  220. static struct bfd_hash_entry *
  221. link_hash_newfunc (struct bfd_hash_entry *entry,
  222. struct bfd_hash_table *table, const char *string)
  223. {
  224. /* Allocate the structure if it has not already been allocated by a
  225. subclass. */
  226. if (entry == NULL)
  227. {
  228. entry =
  229. bfd_hash_allocate (table,
  230. sizeof (struct riscv_elf_link_hash_entry));
  231. if (entry == NULL)
  232. return entry;
  233. }
  234. /* Call the allocation method of the superclass. */
  235. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  236. if (entry != NULL)
  237. {
  238. struct riscv_elf_link_hash_entry *eh;
  239. eh = (struct riscv_elf_link_hash_entry *) entry;
  240. eh->tls_type = GOT_UNKNOWN;
  241. }
  242. return entry;
  243. }
  244. /* Compute a hash of a local hash entry. We use elf_link_hash_entry
  245. for local symbol so that we can handle local STT_GNU_IFUNC symbols
  246. as global symbol. We reuse indx and dynstr_index for local symbol
  247. hash since they aren't used by global symbols in this backend. */
  248. static hashval_t
  249. riscv_elf_local_htab_hash (const void *ptr)
  250. {
  251. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) ptr;
  252. return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
  253. }
  254. /* Compare local hash entries. */
  255. static int
  256. riscv_elf_local_htab_eq (const void *ptr1, const void *ptr2)
  257. {
  258. struct elf_link_hash_entry *h1 = (struct elf_link_hash_entry *) ptr1;
  259. struct elf_link_hash_entry *h2 = (struct elf_link_hash_entry *) ptr2;
  260. return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
  261. }
  262. /* Find and/or create a hash entry for local symbol. */
  263. static struct elf_link_hash_entry *
  264. riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table *htab,
  265. bfd *abfd, const Elf_Internal_Rela *rel,
  266. bool create)
  267. {
  268. struct riscv_elf_link_hash_entry eh, *ret;
  269. asection *sec = abfd->sections;
  270. hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
  271. ELFNN_R_SYM (rel->r_info));
  272. void **slot;
  273. eh.elf.indx = sec->id;
  274. eh.elf.dynstr_index = ELFNN_R_SYM (rel->r_info);
  275. slot = htab_find_slot_with_hash (htab->loc_hash_table, &eh, h,
  276. create ? INSERT : NO_INSERT);
  277. if (!slot)
  278. return NULL;
  279. if (*slot)
  280. {
  281. ret = (struct riscv_elf_link_hash_entry *) *slot;
  282. return &ret->elf;
  283. }
  284. ret = (struct riscv_elf_link_hash_entry *)
  285. objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
  286. sizeof (struct riscv_elf_link_hash_entry));
  287. if (ret)
  288. {
  289. memset (ret, 0, sizeof (*ret));
  290. ret->elf.indx = sec->id;
  291. ret->elf.dynstr_index = ELFNN_R_SYM (rel->r_info);
  292. ret->elf.dynindx = -1;
  293. *slot = ret;
  294. }
  295. return &ret->elf;
  296. }
  297. /* Destroy a RISC-V elf linker hash table. */
  298. static void
  299. riscv_elf_link_hash_table_free (bfd *obfd)
  300. {
  301. struct riscv_elf_link_hash_table *ret
  302. = (struct riscv_elf_link_hash_table *) obfd->link.hash;
  303. if (ret->loc_hash_table)
  304. htab_delete (ret->loc_hash_table);
  305. if (ret->loc_hash_memory)
  306. objalloc_free ((struct objalloc *) ret->loc_hash_memory);
  307. _bfd_elf_link_hash_table_free (obfd);
  308. }
  309. /* Create a RISC-V ELF linker hash table. */
  310. static struct bfd_link_hash_table *
  311. riscv_elf_link_hash_table_create (bfd *abfd)
  312. {
  313. struct riscv_elf_link_hash_table *ret;
  314. size_t amt = sizeof (struct riscv_elf_link_hash_table);
  315. ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
  316. if (ret == NULL)
  317. return NULL;
  318. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
  319. sizeof (struct riscv_elf_link_hash_entry),
  320. RISCV_ELF_DATA))
  321. {
  322. free (ret);
  323. return NULL;
  324. }
  325. ret->max_alignment = (bfd_vma) -1;
  326. /* Create hash table for local ifunc. */
  327. ret->loc_hash_table = htab_try_create (1024,
  328. riscv_elf_local_htab_hash,
  329. riscv_elf_local_htab_eq,
  330. NULL);
  331. ret->loc_hash_memory = objalloc_create ();
  332. if (!ret->loc_hash_table || !ret->loc_hash_memory)
  333. {
  334. riscv_elf_link_hash_table_free (abfd);
  335. return NULL;
  336. }
  337. ret->elf.root.hash_table_free = riscv_elf_link_hash_table_free;
  338. return &ret->elf.root;
  339. }
  340. /* Create the .got section. */
  341. static bool
  342. riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
  343. {
  344. flagword flags;
  345. asection *s, *s_got;
  346. struct elf_link_hash_entry *h;
  347. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  348. struct elf_link_hash_table *htab = elf_hash_table (info);
  349. /* This function may be called more than once. */
  350. if (htab->sgot != NULL)
  351. return true;
  352. flags = bed->dynamic_sec_flags;
  353. s = bfd_make_section_anyway_with_flags (abfd,
  354. (bed->rela_plts_and_copies_p
  355. ? ".rela.got" : ".rel.got"),
  356. (bed->dynamic_sec_flags
  357. | SEC_READONLY));
  358. if (s == NULL
  359. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  360. return false;
  361. htab->srelgot = s;
  362. s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  363. if (s == NULL
  364. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  365. return false;
  366. htab->sgot = s;
  367. /* The first bit of the global offset table is the header. */
  368. s->size += bed->got_header_size;
  369. if (bed->want_got_plt)
  370. {
  371. s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  372. if (s == NULL
  373. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  374. return false;
  375. htab->sgotplt = s;
  376. /* Reserve room for the header. */
  377. s->size += GOTPLT_HEADER_SIZE;
  378. }
  379. if (bed->want_got_sym)
  380. {
  381. /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  382. section. We don't do this in the linker script because we don't want
  383. to define the symbol if we are not creating a global offset
  384. table. */
  385. h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
  386. "_GLOBAL_OFFSET_TABLE_");
  387. elf_hash_table (info)->hgot = h;
  388. if (h == NULL)
  389. return false;
  390. }
  391. return true;
  392. }
  393. /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
  394. .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
  395. hash table. */
  396. static bool
  397. riscv_elf_create_dynamic_sections (bfd *dynobj,
  398. struct bfd_link_info *info)
  399. {
  400. struct riscv_elf_link_hash_table *htab;
  401. htab = riscv_elf_hash_table (info);
  402. BFD_ASSERT (htab != NULL);
  403. if (!riscv_elf_create_got_section (dynobj, info))
  404. return false;
  405. if (!_bfd_elf_create_dynamic_sections (dynobj, info))
  406. return false;
  407. if (!bfd_link_pic (info))
  408. {
  409. /* Technically, this section doesn't have contents. It is used as the
  410. target of TLS copy relocs, to copy TLS data from shared libraries into
  411. the executable. However, if we don't mark it as loadable, then it
  412. matches the IS_TBSS test in ldlang.c, and there is no run-time address
  413. space allocated for it even though it has SEC_ALLOC. That test is
  414. correct for .tbss, but not correct for this section. There is also
  415. a second problem that having a section with no contents can only work
  416. if it comes after all sections with contents in the same segment,
  417. but the linker script does not guarantee that. This is just mixed in
  418. with other .tdata.* sections. We can fix both problems by lying and
  419. saying that there are contents. This section is expected to be small
  420. so this should not cause a significant extra program startup cost. */
  421. htab->sdyntdata =
  422. bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
  423. (SEC_ALLOC | SEC_THREAD_LOCAL
  424. | SEC_LOAD | SEC_DATA
  425. | SEC_HAS_CONTENTS
  426. | SEC_LINKER_CREATED));
  427. }
  428. if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
  429. || (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
  430. abort ();
  431. return true;
  432. }
  433. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  434. static void
  435. riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
  436. struct elf_link_hash_entry *dir,
  437. struct elf_link_hash_entry *ind)
  438. {
  439. struct riscv_elf_link_hash_entry *edir, *eind;
  440. edir = (struct riscv_elf_link_hash_entry *) dir;
  441. eind = (struct riscv_elf_link_hash_entry *) ind;
  442. if (ind->root.type == bfd_link_hash_indirect
  443. && dir->got.refcount <= 0)
  444. {
  445. edir->tls_type = eind->tls_type;
  446. eind->tls_type = GOT_UNKNOWN;
  447. }
  448. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  449. }
  450. static bool
  451. riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
  452. unsigned long symndx, char tls_type)
  453. {
  454. char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
  455. *new_tls_type |= tls_type;
  456. if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
  457. {
  458. (*_bfd_error_handler)
  459. (_("%pB: `%s' accessed both as normal and thread local symbol"),
  460. abfd, h ? h->root.root.string : "<local>");
  461. return false;
  462. }
  463. return true;
  464. }
  465. static bool
  466. riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
  467. struct elf_link_hash_entry *h, long symndx)
  468. {
  469. struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
  470. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  471. if (htab->elf.sgot == NULL)
  472. {
  473. if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
  474. return false;
  475. }
  476. if (h != NULL)
  477. {
  478. h->got.refcount += 1;
  479. return true;
  480. }
  481. /* This is a global offset table entry for a local symbol. */
  482. if (elf_local_got_refcounts (abfd) == NULL)
  483. {
  484. bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
  485. if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
  486. return false;
  487. _bfd_riscv_elf_local_got_tls_type (abfd)
  488. = (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
  489. }
  490. elf_local_got_refcounts (abfd) [symndx] += 1;
  491. return true;
  492. }
  493. static bool
  494. bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
  495. {
  496. reloc_howto_type * r = riscv_elf_rtype_to_howto (abfd, r_type);
  497. /* We propably can improve the information to tell users that they
  498. should be recompile the code with -fPIC or -fPIE, just like what
  499. x86 does. */
  500. (*_bfd_error_handler)
  501. (_("%pB: relocation %s against `%s' can not be used when making a shared "
  502. "object; recompile with -fPIC"),
  503. abfd, r ? r->name : _("<unknown>"),
  504. h != NULL ? h->root.root.string : "a local symbol");
  505. bfd_set_error (bfd_error_bad_value);
  506. return false;
  507. }
  508. /* Look through the relocs for a section during the first phase, and
  509. allocate space in the global offset table or procedure linkage
  510. table. */
  511. static bool
  512. riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
  513. asection *sec, const Elf_Internal_Rela *relocs)
  514. {
  515. struct riscv_elf_link_hash_table *htab;
  516. Elf_Internal_Shdr *symtab_hdr;
  517. struct elf_link_hash_entry **sym_hashes;
  518. const Elf_Internal_Rela *rel;
  519. asection *sreloc = NULL;
  520. if (bfd_link_relocatable (info))
  521. return true;
  522. htab = riscv_elf_hash_table (info);
  523. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  524. sym_hashes = elf_sym_hashes (abfd);
  525. if (htab->elf.dynobj == NULL)
  526. htab->elf.dynobj = abfd;
  527. for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
  528. {
  529. unsigned int r_type;
  530. unsigned int r_symndx;
  531. struct elf_link_hash_entry *h;
  532. r_symndx = ELFNN_R_SYM (rel->r_info);
  533. r_type = ELFNN_R_TYPE (rel->r_info);
  534. if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
  535. {
  536. (*_bfd_error_handler) (_("%pB: bad symbol index: %d"),
  537. abfd, r_symndx);
  538. return false;
  539. }
  540. if (r_symndx < symtab_hdr->sh_info)
  541. {
  542. /* A local symbol. */
  543. Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
  544. abfd, r_symndx);
  545. if (isym == NULL)
  546. return false;
  547. /* Check relocation against local STT_GNU_IFUNC symbol. */
  548. if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  549. {
  550. h = riscv_elf_get_local_sym_hash (htab, abfd, rel, true);
  551. if (h == NULL)
  552. return false;
  553. /* Fake STT_GNU_IFUNC global symbol. */
  554. h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
  555. isym, NULL);
  556. h->type = STT_GNU_IFUNC;
  557. h->def_regular = 1;
  558. h->ref_regular = 1;
  559. h->forced_local = 1;
  560. h->root.type = bfd_link_hash_defined;
  561. }
  562. else
  563. h = NULL;
  564. }
  565. else
  566. {
  567. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  568. while (h->root.type == bfd_link_hash_indirect
  569. || h->root.type == bfd_link_hash_warning)
  570. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  571. }
  572. if (h != NULL)
  573. {
  574. switch (r_type)
  575. {
  576. case R_RISCV_32:
  577. case R_RISCV_64:
  578. case R_RISCV_CALL:
  579. case R_RISCV_CALL_PLT:
  580. case R_RISCV_HI20:
  581. case R_RISCV_GOT_HI20:
  582. case R_RISCV_PCREL_HI20:
  583. /* Create the ifunc sections, iplt and ipltgot, for static
  584. executables. */
  585. if (h->type == STT_GNU_IFUNC
  586. && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
  587. return false;
  588. break;
  589. default:
  590. break;
  591. }
  592. /* It is referenced by a non-shared object. */
  593. h->ref_regular = 1;
  594. }
  595. switch (r_type)
  596. {
  597. case R_RISCV_TLS_GD_HI20:
  598. if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
  599. || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
  600. return false;
  601. break;
  602. case R_RISCV_TLS_GOT_HI20:
  603. if (bfd_link_pic (info))
  604. info->flags |= DF_STATIC_TLS;
  605. if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
  606. || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
  607. return false;
  608. break;
  609. case R_RISCV_GOT_HI20:
  610. if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
  611. || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
  612. return false;
  613. break;
  614. case R_RISCV_CALL:
  615. case R_RISCV_CALL_PLT:
  616. /* These symbol requires a procedure linkage table entry.
  617. We actually build the entry in adjust_dynamic_symbol,
  618. because these might be a case of linking PIC code without
  619. linking in any dynamic objects, in which case we don't
  620. need to generate a procedure linkage table after all. */
  621. /* If it is a local symbol, then we resolve it directly
  622. without creating a PLT entry. */
  623. if (h == NULL)
  624. continue;
  625. h->needs_plt = 1;
  626. h->plt.refcount += 1;
  627. break;
  628. case R_RISCV_PCREL_HI20:
  629. if (h != NULL
  630. && h->type == STT_GNU_IFUNC)
  631. {
  632. h->non_got_ref = 1;
  633. h->pointer_equality_needed = 1;
  634. /* We don't use the PCREL_HI20 in the data section,
  635. so we always need the plt when it refers to
  636. ifunc symbol. */
  637. h->plt.refcount += 1;
  638. }
  639. /* Fall through. */
  640. case R_RISCV_JAL:
  641. case R_RISCV_BRANCH:
  642. case R_RISCV_RVC_BRANCH:
  643. case R_RISCV_RVC_JUMP:
  644. /* In shared libraries and pie, these relocs are known
  645. to bind locally. */
  646. if (bfd_link_pic (info))
  647. break;
  648. goto static_reloc;
  649. case R_RISCV_TPREL_HI20:
  650. if (!bfd_link_executable (info))
  651. return bad_static_reloc (abfd, r_type, h);
  652. if (h != NULL)
  653. riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
  654. goto static_reloc;
  655. case R_RISCV_HI20:
  656. if (bfd_link_pic (info))
  657. return bad_static_reloc (abfd, r_type, h);
  658. /* Fall through. */
  659. case R_RISCV_COPY:
  660. case R_RISCV_JUMP_SLOT:
  661. case R_RISCV_RELATIVE:
  662. case R_RISCV_64:
  663. case R_RISCV_32:
  664. /* Fall through. */
  665. static_reloc:
  666. if (h != NULL
  667. && (!bfd_link_pic (info)
  668. || h->type == STT_GNU_IFUNC))
  669. {
  670. /* This reloc might not bind locally. */
  671. h->non_got_ref = 1;
  672. h->pointer_equality_needed = 1;
  673. if (!h->def_regular
  674. || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
  675. {
  676. /* We may need a .plt entry if the symbol is a function
  677. defined in a shared lib or is a function referenced
  678. from the code or read-only section. */
  679. h->plt.refcount += 1;
  680. }
  681. }
  682. /* If we are creating a shared library, and this is a reloc
  683. against a global symbol, or a non PC relative reloc
  684. against a local symbol, then we need to copy the reloc
  685. into the shared library. However, if we are linking with
  686. -Bsymbolic, we do not need to copy a reloc against a
  687. global symbol which is defined in an object we are
  688. including in the link (i.e., DEF_REGULAR is set). At
  689. this point we have not seen all the input files, so it is
  690. possible that DEF_REGULAR is not set now but will be set
  691. later (it is never cleared). In case of a weak definition,
  692. DEF_REGULAR may be cleared later by a strong definition in
  693. a shared library. We account for that possibility below by
  694. storing information in the relocs_copied field of the hash
  695. table entry. A similar situation occurs when creating
  696. shared libraries and symbol visibility changes render the
  697. symbol local.
  698. If on the other hand, we are creating an executable, we
  699. may need to keep relocations for symbols satisfied by a
  700. dynamic library if we manage to avoid copy relocs for the
  701. symbol.
  702. Generate dynamic pointer relocation against STT_GNU_IFUNC
  703. symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
  704. reloc_howto_type * r = riscv_elf_rtype_to_howto (abfd, r_type);
  705. if ((bfd_link_pic (info)
  706. && (sec->flags & SEC_ALLOC) != 0
  707. && ((r != NULL && !r->pc_relative)
  708. || (h != NULL
  709. && (!info->symbolic
  710. || h->root.type == bfd_link_hash_defweak
  711. || !h->def_regular))))
  712. || (!bfd_link_pic (info)
  713. && (sec->flags & SEC_ALLOC) != 0
  714. && h != NULL
  715. && (h->root.type == bfd_link_hash_defweak
  716. || !h->def_regular))
  717. || (!bfd_link_pic (info)
  718. && h != NULL
  719. && h->type == STT_GNU_IFUNC
  720. && (sec->flags & SEC_CODE) == 0))
  721. {
  722. struct elf_dyn_relocs *p;
  723. struct elf_dyn_relocs **head;
  724. /* When creating a shared object, we must copy these
  725. relocs into the output file. We create a reloc
  726. section in dynobj and make room for the reloc. */
  727. if (sreloc == NULL)
  728. {
  729. sreloc = _bfd_elf_make_dynamic_reloc_section
  730. (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES,
  731. abfd, /*rela?*/ true);
  732. if (sreloc == NULL)
  733. return false;
  734. }
  735. /* If this is a global symbol, we count the number of
  736. relocations we need for this symbol. */
  737. if (h != NULL)
  738. head = &h->dyn_relocs;
  739. else
  740. {
  741. /* Track dynamic relocs needed for local syms too.
  742. We really need local syms available to do this
  743. easily. Oh well. */
  744. asection *s;
  745. void *vpp;
  746. Elf_Internal_Sym *isym;
  747. isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
  748. abfd, r_symndx);
  749. if (isym == NULL)
  750. return false;
  751. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  752. if (s == NULL)
  753. s = sec;
  754. vpp = &elf_section_data (s)->local_dynrel;
  755. head = (struct elf_dyn_relocs **) vpp;
  756. }
  757. p = *head;
  758. if (p == NULL || p->sec != sec)
  759. {
  760. size_t amt = sizeof *p;
  761. p = ((struct elf_dyn_relocs *)
  762. bfd_alloc (htab->elf.dynobj, amt));
  763. if (p == NULL)
  764. return false;
  765. p->next = *head;
  766. *head = p;
  767. p->sec = sec;
  768. p->count = 0;
  769. p->pc_count = 0;
  770. }
  771. p->count += 1;
  772. p->pc_count += r == NULL ? 0 : r->pc_relative;
  773. }
  774. break;
  775. case R_RISCV_GNU_VTINHERIT:
  776. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  777. return false;
  778. break;
  779. case R_RISCV_GNU_VTENTRY:
  780. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  781. return false;
  782. break;
  783. default:
  784. break;
  785. }
  786. }
  787. return true;
  788. }
  789. static asection *
  790. riscv_elf_gc_mark_hook (asection *sec,
  791. struct bfd_link_info *info,
  792. Elf_Internal_Rela *rel,
  793. struct elf_link_hash_entry *h,
  794. Elf_Internal_Sym *sym)
  795. {
  796. if (h != NULL)
  797. switch (ELFNN_R_TYPE (rel->r_info))
  798. {
  799. case R_RISCV_GNU_VTINHERIT:
  800. case R_RISCV_GNU_VTENTRY:
  801. return NULL;
  802. }
  803. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  804. }
  805. /* Adjust a symbol defined by a dynamic object and referenced by a
  806. regular object. The current definition is in some section of the
  807. dynamic object, but we're not including those sections. We have to
  808. change the definition to something the rest of the link can
  809. understand. */
  810. static bool
  811. riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  812. struct elf_link_hash_entry *h)
  813. {
  814. struct riscv_elf_link_hash_table *htab;
  815. struct riscv_elf_link_hash_entry * eh;
  816. bfd *dynobj;
  817. asection *s, *srel;
  818. htab = riscv_elf_hash_table (info);
  819. BFD_ASSERT (htab != NULL);
  820. dynobj = htab->elf.dynobj;
  821. /* Make sure we know what is going on here. */
  822. BFD_ASSERT (dynobj != NULL
  823. && (h->needs_plt
  824. || h->type == STT_GNU_IFUNC
  825. || h->is_weakalias
  826. || (h->def_dynamic
  827. && h->ref_regular
  828. && !h->def_regular)));
  829. /* If this is a function, put it in the procedure linkage table. We
  830. will fill in the contents of the procedure linkage table later
  831. (although we could actually do it here). */
  832. if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
  833. {
  834. if (h->plt.refcount <= 0
  835. || (h->type != STT_GNU_IFUNC
  836. && (SYMBOL_CALLS_LOCAL (info, h)
  837. || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  838. && h->root.type == bfd_link_hash_undefweak))))
  839. {
  840. /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
  841. input file, but the symbol was never referred to by a dynamic
  842. object, or if all references were garbage collected. In such
  843. a case, we don't actually need to build a PLT entry. */
  844. h->plt.offset = (bfd_vma) -1;
  845. h->needs_plt = 0;
  846. }
  847. return true;
  848. }
  849. else
  850. h->plt.offset = (bfd_vma) -1;
  851. /* If this is a weak symbol, and there is a real definition, the
  852. processor independent code will have arranged for us to see the
  853. real definition first, and we can just use the same value. */
  854. if (h->is_weakalias)
  855. {
  856. struct elf_link_hash_entry *def = weakdef (h);
  857. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  858. h->root.u.def.section = def->root.u.def.section;
  859. h->root.u.def.value = def->root.u.def.value;
  860. return true;
  861. }
  862. /* This is a reference to a symbol defined by a dynamic object which
  863. is not a function. */
  864. /* If we are creating a shared library, we must presume that the
  865. only references to the symbol are via the global offset table.
  866. For such cases we need not do anything here; the relocations will
  867. be handled correctly by relocate_section. */
  868. if (bfd_link_pic (info))
  869. return true;
  870. /* If there are no references to this symbol that do not use the
  871. GOT, we don't need to generate a copy reloc. */
  872. if (!h->non_got_ref)
  873. return true;
  874. /* If -z nocopyreloc was given, we won't generate them either. */
  875. if (info->nocopyreloc)
  876. {
  877. h->non_got_ref = 0;
  878. return true;
  879. }
  880. /* If we don't find any dynamic relocs in read-only sections, then
  881. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  882. if (!_bfd_elf_readonly_dynrelocs (h))
  883. {
  884. h->non_got_ref = 0;
  885. return true;
  886. }
  887. /* We must allocate the symbol in our .dynbss section, which will
  888. become part of the .bss section of the executable. There will be
  889. an entry for this symbol in the .dynsym section. The dynamic
  890. object will contain position independent code, so all references
  891. from the dynamic object to this symbol will go through the global
  892. offset table. The dynamic linker will use the .dynsym entry to
  893. determine the address it must put in the global offset table, so
  894. both the dynamic object and the regular object will refer to the
  895. same memory location for the variable. */
  896. /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
  897. to copy the initial value out of the dynamic object and into the
  898. runtime process image. We need to remember the offset into the
  899. .rel.bss section we are going to use. */
  900. eh = (struct riscv_elf_link_hash_entry *) h;
  901. if (eh->tls_type & ~GOT_NORMAL)
  902. {
  903. s = htab->sdyntdata;
  904. srel = htab->elf.srelbss;
  905. }
  906. else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  907. {
  908. s = htab->elf.sdynrelro;
  909. srel = htab->elf.sreldynrelro;
  910. }
  911. else
  912. {
  913. s = htab->elf.sdynbss;
  914. srel = htab->elf.srelbss;
  915. }
  916. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  917. {
  918. srel->size += sizeof (ElfNN_External_Rela);
  919. h->needs_copy = 1;
  920. }
  921. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  922. }
  923. /* Allocate space in .plt, .got and associated reloc sections for
  924. dynamic relocs. */
  925. static bool
  926. allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  927. {
  928. struct bfd_link_info *info;
  929. struct riscv_elf_link_hash_table *htab;
  930. struct elf_dyn_relocs *p;
  931. if (h->root.type == bfd_link_hash_indirect)
  932. return true;
  933. info = (struct bfd_link_info *) inf;
  934. htab = riscv_elf_hash_table (info);
  935. BFD_ASSERT (htab != NULL);
  936. /* When we are generating pde, make sure gp symbol is output as a
  937. dynamic symbol. Then ld.so can set the gp register earlier, before
  938. resolving the ifunc. */
  939. if (!bfd_link_pic (info)
  940. && htab->elf.dynamic_sections_created
  941. && strcmp (h->root.root.string, RISCV_GP_SYMBOL) == 0
  942. && !bfd_elf_link_record_dynamic_symbol (info, h))
  943. return false;
  944. /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
  945. in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
  946. if they are defined and referenced in a non-shared object. */
  947. if (h->type == STT_GNU_IFUNC
  948. && h->def_regular)
  949. return true;
  950. else if (htab->elf.dynamic_sections_created
  951. && h->plt.refcount > 0)
  952. {
  953. /* Make sure this symbol is output as a dynamic symbol.
  954. Undefined weak syms won't yet be marked as dynamic. */
  955. if (h->dynindx == -1
  956. && !h->forced_local)
  957. {
  958. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  959. return false;
  960. }
  961. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
  962. {
  963. asection *s = htab->elf.splt;
  964. if (s->size == 0)
  965. s->size = PLT_HEADER_SIZE;
  966. h->plt.offset = s->size;
  967. /* Make room for this entry. */
  968. s->size += PLT_ENTRY_SIZE;
  969. /* We also need to make an entry in the .got.plt section. */
  970. htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
  971. /* We also need to make an entry in the .rela.plt section. */
  972. htab->elf.srelplt->size += sizeof (ElfNN_External_Rela);
  973. /* If this symbol is not defined in a regular file, and we are
  974. not generating a shared library, then set the symbol to this
  975. location in the .plt. This is required to make function
  976. pointers compare as equal between the normal executable and
  977. the shared library. */
  978. if (! bfd_link_pic (info)
  979. && !h->def_regular)
  980. {
  981. h->root.u.def.section = s;
  982. h->root.u.def.value = h->plt.offset;
  983. }
  984. /* If the symbol has STO_RISCV_VARIANT_CC flag, then raise the
  985. variant_cc flag of riscv_elf_link_hash_table. */
  986. if (h->other & STO_RISCV_VARIANT_CC)
  987. htab->variant_cc = 1;
  988. }
  989. else
  990. {
  991. h->plt.offset = (bfd_vma) -1;
  992. h->needs_plt = 0;
  993. }
  994. }
  995. else
  996. {
  997. h->plt.offset = (bfd_vma) -1;
  998. h->needs_plt = 0;
  999. }
  1000. if (h->got.refcount > 0)
  1001. {
  1002. asection *s;
  1003. bool dyn;
  1004. int tls_type = riscv_elf_hash_entry (h)->tls_type;
  1005. /* Make sure this symbol is output as a dynamic symbol.
  1006. Undefined weak syms won't yet be marked as dynamic. */
  1007. if (h->dynindx == -1
  1008. && !h->forced_local)
  1009. {
  1010. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1011. return false;
  1012. }
  1013. s = htab->elf.sgot;
  1014. h->got.offset = s->size;
  1015. dyn = htab->elf.dynamic_sections_created;
  1016. if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
  1017. {
  1018. /* TLS_GD needs two dynamic relocs and two GOT slots. */
  1019. if (tls_type & GOT_TLS_GD)
  1020. {
  1021. s->size += 2 * RISCV_ELF_WORD_BYTES;
  1022. htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela);
  1023. }
  1024. /* TLS_IE needs one dynamic reloc and one GOT slot. */
  1025. if (tls_type & GOT_TLS_IE)
  1026. {
  1027. s->size += RISCV_ELF_WORD_BYTES;
  1028. htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
  1029. }
  1030. }
  1031. else
  1032. {
  1033. s->size += RISCV_ELF_WORD_BYTES;
  1034. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
  1035. && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  1036. htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
  1037. }
  1038. }
  1039. else
  1040. h->got.offset = (bfd_vma) -1;
  1041. if (h->dyn_relocs == NULL)
  1042. return true;
  1043. /* In the shared -Bsymbolic case, discard space allocated for
  1044. dynamic pc-relative relocs against symbols which turn out to be
  1045. defined in regular objects. For the normal shared case, discard
  1046. space for pc-relative relocs that have become local due to symbol
  1047. visibility changes. */
  1048. if (bfd_link_pic (info))
  1049. {
  1050. if (SYMBOL_CALLS_LOCAL (info, h))
  1051. {
  1052. struct elf_dyn_relocs **pp;
  1053. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  1054. {
  1055. p->count -= p->pc_count;
  1056. p->pc_count = 0;
  1057. if (p->count == 0)
  1058. *pp = p->next;
  1059. else
  1060. pp = &p->next;
  1061. }
  1062. }
  1063. /* Also discard relocs on undefined weak syms with non-default
  1064. visibility. */
  1065. if (h->dyn_relocs != NULL
  1066. && h->root.type == bfd_link_hash_undefweak)
  1067. {
  1068. if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1069. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  1070. h->dyn_relocs = NULL;
  1071. /* Make sure undefined weak symbols are output as a dynamic
  1072. symbol in PIEs. */
  1073. else if (h->dynindx == -1
  1074. && !h->forced_local)
  1075. {
  1076. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1077. return false;
  1078. }
  1079. }
  1080. }
  1081. else
  1082. {
  1083. /* For the non-shared case, discard space for relocs against
  1084. symbols which turn out to need copy relocs or are not
  1085. dynamic. */
  1086. if (!h->non_got_ref
  1087. && ((h->def_dynamic
  1088. && !h->def_regular)
  1089. || (htab->elf.dynamic_sections_created
  1090. && (h->root.type == bfd_link_hash_undefweak
  1091. || h->root.type == bfd_link_hash_undefined))))
  1092. {
  1093. /* Make sure this symbol is output as a dynamic symbol.
  1094. Undefined weak syms won't yet be marked as dynamic. */
  1095. if (h->dynindx == -1
  1096. && !h->forced_local)
  1097. {
  1098. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1099. return false;
  1100. }
  1101. /* If that succeeded, we know we'll be keeping all the
  1102. relocs. */
  1103. if (h->dynindx != -1)
  1104. goto keep;
  1105. }
  1106. h->dyn_relocs = NULL;
  1107. keep: ;
  1108. }
  1109. /* Finally, allocate space. */
  1110. for (p = h->dyn_relocs; p != NULL; p = p->next)
  1111. {
  1112. asection *sreloc = elf_section_data (p->sec)->sreloc;
  1113. sreloc->size += p->count * sizeof (ElfNN_External_Rela);
  1114. }
  1115. return true;
  1116. }
  1117. /* Allocate space in .plt, .got and associated reloc sections for
  1118. ifunc dynamic relocs. */
  1119. static bool
  1120. allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
  1121. void *inf)
  1122. {
  1123. struct bfd_link_info *info;
  1124. if (h->root.type == bfd_link_hash_indirect)
  1125. return true;
  1126. if (h->root.type == bfd_link_hash_warning)
  1127. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1128. info = (struct bfd_link_info *) inf;
  1129. /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
  1130. here if it is defined and referenced in a non-shared object. */
  1131. if (h->type == STT_GNU_IFUNC
  1132. && h->def_regular)
  1133. return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
  1134. &h->dyn_relocs,
  1135. PLT_ENTRY_SIZE,
  1136. PLT_HEADER_SIZE,
  1137. GOT_ENTRY_SIZE,
  1138. true);
  1139. return true;
  1140. }
  1141. /* Allocate space in .plt, .got and associated reloc sections for
  1142. local ifunc dynamic relocs. */
  1143. static int
  1144. allocate_local_ifunc_dynrelocs (void **slot, void *inf)
  1145. {
  1146. struct elf_link_hash_entry *h
  1147. = (struct elf_link_hash_entry *) *slot;
  1148. if (h->type != STT_GNU_IFUNC
  1149. || !h->def_regular
  1150. || !h->ref_regular
  1151. || !h->forced_local
  1152. || h->root.type != bfd_link_hash_defined)
  1153. abort ();
  1154. return allocate_ifunc_dynrelocs (h, inf);
  1155. }
  1156. static bool
  1157. riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
  1158. {
  1159. struct riscv_elf_link_hash_table *htab;
  1160. bfd *dynobj;
  1161. asection *s;
  1162. bfd *ibfd;
  1163. htab = riscv_elf_hash_table (info);
  1164. BFD_ASSERT (htab != NULL);
  1165. dynobj = htab->elf.dynobj;
  1166. BFD_ASSERT (dynobj != NULL);
  1167. if (elf_hash_table (info)->dynamic_sections_created)
  1168. {
  1169. /* Set the contents of the .interp section to the interpreter. */
  1170. if (bfd_link_executable (info) && !info->nointerp)
  1171. {
  1172. s = bfd_get_linker_section (dynobj, ".interp");
  1173. BFD_ASSERT (s != NULL);
  1174. s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1;
  1175. s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER;
  1176. }
  1177. }
  1178. /* Set up .got offsets for local syms, and space for local dynamic
  1179. relocs. */
  1180. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  1181. {
  1182. bfd_signed_vma *local_got;
  1183. bfd_signed_vma *end_local_got;
  1184. char *local_tls_type;
  1185. bfd_size_type locsymcount;
  1186. Elf_Internal_Shdr *symtab_hdr;
  1187. asection *srel;
  1188. if (! is_riscv_elf (ibfd))
  1189. continue;
  1190. for (s = ibfd->sections; s != NULL; s = s->next)
  1191. {
  1192. struct elf_dyn_relocs *p;
  1193. for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
  1194. {
  1195. if (!bfd_is_abs_section (p->sec)
  1196. && bfd_is_abs_section (p->sec->output_section))
  1197. {
  1198. /* Input section has been discarded, either because
  1199. it is a copy of a linkonce section or due to
  1200. linker script /DISCARD/, so we'll be discarding
  1201. the relocs too. */
  1202. }
  1203. else if (p->count != 0)
  1204. {
  1205. srel = elf_section_data (p->sec)->sreloc;
  1206. srel->size += p->count * sizeof (ElfNN_External_Rela);
  1207. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  1208. info->flags |= DF_TEXTREL;
  1209. }
  1210. }
  1211. }
  1212. local_got = elf_local_got_refcounts (ibfd);
  1213. if (!local_got)
  1214. continue;
  1215. symtab_hdr = &elf_symtab_hdr (ibfd);
  1216. locsymcount = symtab_hdr->sh_info;
  1217. end_local_got = local_got + locsymcount;
  1218. local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd);
  1219. s = htab->elf.sgot;
  1220. srel = htab->elf.srelgot;
  1221. for (; local_got < end_local_got; ++local_got, ++local_tls_type)
  1222. {
  1223. if (*local_got > 0)
  1224. {
  1225. *local_got = s->size;
  1226. s->size += RISCV_ELF_WORD_BYTES;
  1227. if (*local_tls_type & GOT_TLS_GD)
  1228. s->size += RISCV_ELF_WORD_BYTES;
  1229. if (bfd_link_pic (info)
  1230. || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
  1231. srel->size += sizeof (ElfNN_External_Rela);
  1232. }
  1233. else
  1234. *local_got = (bfd_vma) -1;
  1235. }
  1236. }
  1237. /* Allocate .plt and .got entries and space dynamic relocs for
  1238. global symbols. */
  1239. elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
  1240. /* Allocate .plt and .got entries and space dynamic relocs for
  1241. global ifunc symbols. */
  1242. elf_link_hash_traverse (&htab->elf, allocate_ifunc_dynrelocs, info);
  1243. /* Allocate .plt and .got entries and space dynamic relocs for
  1244. local ifunc symbols. */
  1245. htab_traverse (htab->loc_hash_table, allocate_local_ifunc_dynrelocs, info);
  1246. /* Used to resolve the dynamic relocs overwite problems when
  1247. generating static executable. */
  1248. if (htab->elf.irelplt)
  1249. htab->last_iplt_index = htab->elf.irelplt->reloc_count - 1;
  1250. if (htab->elf.sgotplt)
  1251. {
  1252. struct elf_link_hash_entry *got;
  1253. got = elf_link_hash_lookup (elf_hash_table (info),
  1254. "_GLOBAL_OFFSET_TABLE_",
  1255. false, false, false);
  1256. /* Don't allocate .got.plt section if there are no GOT nor PLT
  1257. entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
  1258. if ((got == NULL
  1259. || !got->ref_regular_nonweak)
  1260. && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE)
  1261. && (htab->elf.splt == NULL
  1262. || htab->elf.splt->size == 0)
  1263. && (htab->elf.sgot == NULL
  1264. || (htab->elf.sgot->size
  1265. == get_elf_backend_data (output_bfd)->got_header_size)))
  1266. htab->elf.sgotplt->size = 0;
  1267. }
  1268. /* The check_relocs and adjust_dynamic_symbol entry points have
  1269. determined the sizes of the various dynamic sections. Allocate
  1270. memory for them. */
  1271. for (s = dynobj->sections; s != NULL; s = s->next)
  1272. {
  1273. if ((s->flags & SEC_LINKER_CREATED) == 0)
  1274. continue;
  1275. if (s == htab->elf.splt
  1276. || s == htab->elf.sgot
  1277. || s == htab->elf.sgotplt
  1278. || s == htab->elf.iplt
  1279. || s == htab->elf.igotplt
  1280. || s == htab->elf.sdynbss
  1281. || s == htab->elf.sdynrelro
  1282. || s == htab->sdyntdata)
  1283. {
  1284. /* Strip this section if we don't need it; see the
  1285. comment below. */
  1286. }
  1287. else if (startswith (s->name, ".rela"))
  1288. {
  1289. if (s->size != 0)
  1290. {
  1291. /* We use the reloc_count field as a counter if we need
  1292. to copy relocs into the output file. */
  1293. s->reloc_count = 0;
  1294. }
  1295. }
  1296. else
  1297. {
  1298. /* It's not one of our sections. */
  1299. continue;
  1300. }
  1301. if (s->size == 0)
  1302. {
  1303. /* If we don't need this section, strip it from the
  1304. output file. This is mostly to handle .rela.bss and
  1305. .rela.plt. We must create both sections in
  1306. create_dynamic_sections, because they must be created
  1307. before the linker maps input sections to output
  1308. sections. The linker does that before
  1309. adjust_dynamic_symbol is called, and it is that
  1310. function which decides whether anything needs to go
  1311. into these sections. */
  1312. s->flags |= SEC_EXCLUDE;
  1313. continue;
  1314. }
  1315. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  1316. continue;
  1317. /* Allocate memory for the section contents. Zero the memory
  1318. for the benefit of .rela.plt, which has 4 unused entries
  1319. at the beginning, and we don't want garbage. */
  1320. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  1321. if (s->contents == NULL)
  1322. return false;
  1323. }
  1324. /* Add dynamic entries. */
  1325. if (elf_hash_table (info)->dynamic_sections_created)
  1326. {
  1327. if (!_bfd_elf_add_dynamic_tags (output_bfd, info, true))
  1328. return false;
  1329. if (htab->variant_cc
  1330. && !_bfd_elf_add_dynamic_entry (info, DT_RISCV_VARIANT_CC, 0))
  1331. return false;
  1332. }
  1333. return true;
  1334. }
  1335. #define TP_OFFSET 0
  1336. #define DTP_OFFSET 0x800
  1337. /* Return the relocation value for a TLS dtp-relative reloc. */
  1338. static bfd_vma
  1339. dtpoff (struct bfd_link_info *info, bfd_vma address)
  1340. {
  1341. /* If tls_sec is NULL, we should have signalled an error already. */
  1342. if (elf_hash_table (info)->tls_sec == NULL)
  1343. return 0;
  1344. return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
  1345. }
  1346. /* Return the relocation value for a static TLS tp-relative relocation. */
  1347. static bfd_vma
  1348. tpoff (struct bfd_link_info *info, bfd_vma address)
  1349. {
  1350. /* If tls_sec is NULL, we should have signalled an error already. */
  1351. if (elf_hash_table (info)->tls_sec == NULL)
  1352. return 0;
  1353. return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
  1354. }
  1355. /* Return the global pointer's value, or 0 if it is not in use. */
  1356. static bfd_vma
  1357. riscv_global_pointer_value (struct bfd_link_info *info)
  1358. {
  1359. struct bfd_link_hash_entry *h;
  1360. h = bfd_link_hash_lookup (info->hash, RISCV_GP_SYMBOL, false, false, true);
  1361. if (h == NULL || h->type != bfd_link_hash_defined)
  1362. return 0;
  1363. return h->u.def.value + sec_addr (h->u.def.section);
  1364. }
  1365. /* Emplace a static relocation. */
  1366. static bfd_reloc_status_type
  1367. perform_relocation (const reloc_howto_type *howto,
  1368. const Elf_Internal_Rela *rel,
  1369. bfd_vma value,
  1370. asection *input_section,
  1371. bfd *input_bfd,
  1372. bfd_byte *contents)
  1373. {
  1374. if (howto->pc_relative)
  1375. value -= sec_addr (input_section) + rel->r_offset;
  1376. value += rel->r_addend;
  1377. switch (ELFNN_R_TYPE (rel->r_info))
  1378. {
  1379. case R_RISCV_HI20:
  1380. case R_RISCV_TPREL_HI20:
  1381. case R_RISCV_PCREL_HI20:
  1382. case R_RISCV_GOT_HI20:
  1383. case R_RISCV_TLS_GOT_HI20:
  1384. case R_RISCV_TLS_GD_HI20:
  1385. if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
  1386. return bfd_reloc_overflow;
  1387. value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
  1388. break;
  1389. case R_RISCV_LO12_I:
  1390. case R_RISCV_GPREL_I:
  1391. case R_RISCV_TPREL_LO12_I:
  1392. case R_RISCV_TPREL_I:
  1393. case R_RISCV_PCREL_LO12_I:
  1394. value = ENCODE_ITYPE_IMM (value);
  1395. break;
  1396. case R_RISCV_LO12_S:
  1397. case R_RISCV_GPREL_S:
  1398. case R_RISCV_TPREL_LO12_S:
  1399. case R_RISCV_TPREL_S:
  1400. case R_RISCV_PCREL_LO12_S:
  1401. value = ENCODE_STYPE_IMM (value);
  1402. break;
  1403. case R_RISCV_CALL:
  1404. case R_RISCV_CALL_PLT:
  1405. if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
  1406. return bfd_reloc_overflow;
  1407. value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
  1408. | (ENCODE_ITYPE_IMM (value) << 32);
  1409. break;
  1410. case R_RISCV_JAL:
  1411. if (!VALID_JTYPE_IMM (value))
  1412. return bfd_reloc_overflow;
  1413. value = ENCODE_JTYPE_IMM (value);
  1414. break;
  1415. case R_RISCV_BRANCH:
  1416. if (!VALID_BTYPE_IMM (value))
  1417. return bfd_reloc_overflow;
  1418. value = ENCODE_BTYPE_IMM (value);
  1419. break;
  1420. case R_RISCV_RVC_BRANCH:
  1421. if (!VALID_CBTYPE_IMM (value))
  1422. return bfd_reloc_overflow;
  1423. value = ENCODE_CBTYPE_IMM (value);
  1424. break;
  1425. case R_RISCV_RVC_JUMP:
  1426. if (!VALID_CJTYPE_IMM (value))
  1427. return bfd_reloc_overflow;
  1428. value = ENCODE_CJTYPE_IMM (value);
  1429. break;
  1430. case R_RISCV_RVC_LUI:
  1431. if (RISCV_CONST_HIGH_PART (value) == 0)
  1432. {
  1433. /* Linker relaxation can convert an address equal to or greater than
  1434. 0x800 to slightly below 0x800. C.LUI does not accept zero as a
  1435. valid immediate. We can fix this by converting it to a C.LI. */
  1436. bfd_vma insn = riscv_get_insn (howto->bitsize,
  1437. contents + rel->r_offset);
  1438. insn = (insn & ~MATCH_C_LUI) | MATCH_C_LI;
  1439. riscv_put_insn (howto->bitsize, insn, contents + rel->r_offset);
  1440. value = ENCODE_CITYPE_IMM (0);
  1441. }
  1442. else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value)))
  1443. return bfd_reloc_overflow;
  1444. else
  1445. value = ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value));
  1446. break;
  1447. case R_RISCV_32:
  1448. case R_RISCV_64:
  1449. case R_RISCV_ADD8:
  1450. case R_RISCV_ADD16:
  1451. case R_RISCV_ADD32:
  1452. case R_RISCV_ADD64:
  1453. case R_RISCV_SUB6:
  1454. case R_RISCV_SUB8:
  1455. case R_RISCV_SUB16:
  1456. case R_RISCV_SUB32:
  1457. case R_RISCV_SUB64:
  1458. case R_RISCV_SET6:
  1459. case R_RISCV_SET8:
  1460. case R_RISCV_SET16:
  1461. case R_RISCV_SET32:
  1462. case R_RISCV_32_PCREL:
  1463. case R_RISCV_TLS_DTPREL32:
  1464. case R_RISCV_TLS_DTPREL64:
  1465. break;
  1466. case R_RISCV_DELETE:
  1467. return bfd_reloc_ok;
  1468. default:
  1469. return bfd_reloc_notsupported;
  1470. }
  1471. bfd_vma word;
  1472. if (riscv_is_insn_reloc (howto))
  1473. word = riscv_get_insn (howto->bitsize, contents + rel->r_offset);
  1474. else
  1475. word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
  1476. word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
  1477. if (riscv_is_insn_reloc (howto))
  1478. riscv_put_insn (howto->bitsize, word, contents + rel->r_offset);
  1479. else
  1480. bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
  1481. return bfd_reloc_ok;
  1482. }
  1483. /* Remember all PC-relative high-part relocs we've encountered to help us
  1484. later resolve the corresponding low-part relocs. */
  1485. typedef struct
  1486. {
  1487. /* PC value. */
  1488. bfd_vma address;
  1489. /* Relocation value with addend. */
  1490. bfd_vma value;
  1491. /* Original reloc type. */
  1492. int type;
  1493. } riscv_pcrel_hi_reloc;
  1494. typedef struct riscv_pcrel_lo_reloc
  1495. {
  1496. /* PC value of auipc. */
  1497. bfd_vma address;
  1498. /* Internal relocation. */
  1499. const Elf_Internal_Rela *reloc;
  1500. /* Record the following information helps to resolve the %pcrel
  1501. which cross different input section. For now we build a hash
  1502. for pcrel at the start of riscv_elf_relocate_section, and then
  1503. free the hash at the end. But riscv_elf_relocate_section only
  1504. handles an input section at a time, so that means we can only
  1505. resolve the %pcrel_hi and %pcrel_lo which are in the same input
  1506. section. Otherwise, we will report dangerous relocation errors
  1507. for those %pcrel which are not in the same input section. */
  1508. asection *input_section;
  1509. struct bfd_link_info *info;
  1510. reloc_howto_type *howto;
  1511. bfd_byte *contents;
  1512. /* The next riscv_pcrel_lo_reloc. */
  1513. struct riscv_pcrel_lo_reloc *next;
  1514. } riscv_pcrel_lo_reloc;
  1515. typedef struct
  1516. {
  1517. /* Hash table for riscv_pcrel_hi_reloc. */
  1518. htab_t hi_relocs;
  1519. /* Linked list for riscv_pcrel_lo_reloc. */
  1520. riscv_pcrel_lo_reloc *lo_relocs;
  1521. } riscv_pcrel_relocs;
  1522. static hashval_t
  1523. riscv_pcrel_reloc_hash (const void *entry)
  1524. {
  1525. const riscv_pcrel_hi_reloc *e = entry;
  1526. return (hashval_t)(e->address >> 2);
  1527. }
  1528. static int
  1529. riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
  1530. {
  1531. const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
  1532. return e1->address == e2->address;
  1533. }
  1534. static bool
  1535. riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
  1536. {
  1537. p->lo_relocs = NULL;
  1538. p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
  1539. riscv_pcrel_reloc_eq, free);
  1540. return p->hi_relocs != NULL;
  1541. }
  1542. static void
  1543. riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
  1544. {
  1545. riscv_pcrel_lo_reloc *cur = p->lo_relocs;
  1546. while (cur != NULL)
  1547. {
  1548. riscv_pcrel_lo_reloc *next = cur->next;
  1549. free (cur);
  1550. cur = next;
  1551. }
  1552. htab_delete (p->hi_relocs);
  1553. }
  1554. static bool
  1555. riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela *rel,
  1556. struct bfd_link_info *info,
  1557. bfd_vma pc,
  1558. bfd_vma addr,
  1559. bfd_byte *contents,
  1560. const reloc_howto_type *howto)
  1561. {
  1562. /* We may need to reference low addreses in PC-relative modes even when the
  1563. PC is far away from these addresses. For example, undefweak references
  1564. need to produce the address 0 when linked. As 0 is far from the arbitrary
  1565. addresses that we can link PC-relative programs at, the linker can't
  1566. actually relocate references to those symbols. In order to allow these
  1567. programs to work we simply convert the PC-relative auipc sequences to
  1568. 0-relative lui sequences. */
  1569. if (bfd_link_pic (info))
  1570. return false;
  1571. /* If it's possible to reference the symbol using auipc we do so, as that's
  1572. more in the spirit of the PC-relative relocations we're processing. */
  1573. bfd_vma offset = addr - pc;
  1574. if (ARCH_SIZE == 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset)))
  1575. return false;
  1576. /* If it's impossible to reference this with a LUI-based offset then don't
  1577. bother to convert it at all so users still see the PC-relative relocation
  1578. in the truncation message. */
  1579. if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr)))
  1580. return false;
  1581. rel->r_info = ELFNN_R_INFO (addr, R_RISCV_HI20);
  1582. bfd_vma insn = riscv_get_insn (howto->bitsize, contents + rel->r_offset);
  1583. insn = (insn & ~MASK_AUIPC) | MATCH_LUI;
  1584. riscv_put_insn (howto->bitsize, insn, contents + rel->r_offset);
  1585. return true;
  1586. }
  1587. static bool
  1588. riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p,
  1589. bfd_vma addr,
  1590. bfd_vma value,
  1591. int type,
  1592. bool absolute)
  1593. {
  1594. bfd_vma offset = absolute ? value : value - addr;
  1595. riscv_pcrel_hi_reloc entry = {addr, offset, type};
  1596. riscv_pcrel_hi_reloc **slot =
  1597. (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
  1598. BFD_ASSERT (*slot == NULL);
  1599. *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
  1600. if (*slot == NULL)
  1601. return false;
  1602. **slot = entry;
  1603. return true;
  1604. }
  1605. static bool
  1606. riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
  1607. bfd_vma addr,
  1608. const Elf_Internal_Rela *reloc,
  1609. asection *input_section,
  1610. struct bfd_link_info *info,
  1611. reloc_howto_type *howto,
  1612. bfd_byte *contents)
  1613. {
  1614. riscv_pcrel_lo_reloc *entry;
  1615. entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
  1616. if (entry == NULL)
  1617. return false;
  1618. *entry = (riscv_pcrel_lo_reloc) {addr, reloc, input_section, info,
  1619. howto, contents, p->lo_relocs};
  1620. p->lo_relocs = entry;
  1621. return true;
  1622. }
  1623. static bool
  1624. riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
  1625. {
  1626. riscv_pcrel_lo_reloc *r;
  1627. for (r = p->lo_relocs; r != NULL; r = r->next)
  1628. {
  1629. bfd *input_bfd = r->input_section->owner;
  1630. riscv_pcrel_hi_reloc search = {r->address, 0, 0};
  1631. riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
  1632. /* There may be a risk if the %pcrel_lo with addend refers to
  1633. an IFUNC symbol. The %pcrel_hi has been relocated to plt,
  1634. so the corresponding %pcrel_lo with addend looks wrong. */
  1635. char *string = NULL;
  1636. if (entry == NULL)
  1637. string = _("%pcrel_lo missing matching %pcrel_hi");
  1638. else if (entry->type == R_RISCV_GOT_HI20
  1639. && r->reloc->r_addend != 0)
  1640. string = _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
  1641. else if (RISCV_CONST_HIGH_PART (entry->value)
  1642. != RISCV_CONST_HIGH_PART (entry->value + r->reloc->r_addend))
  1643. {
  1644. /* Check the overflow when adding reloc addend. */
  1645. if (asprintf (&string,
  1646. _("%%pcrel_lo overflow with an addend, the "
  1647. "value of %%pcrel_hi is 0x%" PRIx64 " without "
  1648. "any addend, but may be 0x%" PRIx64 " after "
  1649. "adding the %%pcrel_lo addend"),
  1650. (int64_t) RISCV_CONST_HIGH_PART (entry->value),
  1651. (int64_t) RISCV_CONST_HIGH_PART
  1652. (entry->value + r->reloc->r_addend)) == -1)
  1653. string = _("%pcrel_lo overflow with an addend");
  1654. }
  1655. if (string != NULL)
  1656. {
  1657. (*r->info->callbacks->reloc_dangerous)
  1658. (r->info, string, input_bfd, r->input_section, r->reloc->r_offset);
  1659. return true;
  1660. }
  1661. perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
  1662. input_bfd, r->contents);
  1663. }
  1664. return true;
  1665. }
  1666. /* Relocate a RISC-V ELF section.
  1667. The RELOCATE_SECTION function is called by the new ELF backend linker
  1668. to handle the relocations for a section.
  1669. The relocs are always passed as Rela structures.
  1670. This function is responsible for adjusting the section contents as
  1671. necessary, and (if generating a relocatable output file) adjusting
  1672. the reloc addend as necessary.
  1673. This function does not have to worry about setting the reloc
  1674. address or the reloc symbol index.
  1675. LOCAL_SYMS is a pointer to the swapped in local symbols.
  1676. LOCAL_SECTIONS is an array giving the section in the input file
  1677. corresponding to the st_shndx field of each local symbol.
  1678. The global hash table entry for the global symbols can be found
  1679. via elf_sym_hashes (input_bfd).
  1680. When generating relocatable output, this function must handle
  1681. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  1682. going to be the section symbol corresponding to the output
  1683. section, which means that the addend must be adjusted
  1684. accordingly. */
  1685. static int
  1686. riscv_elf_relocate_section (bfd *output_bfd,
  1687. struct bfd_link_info *info,
  1688. bfd *input_bfd,
  1689. asection *input_section,
  1690. bfd_byte *contents,
  1691. Elf_Internal_Rela *relocs,
  1692. Elf_Internal_Sym *local_syms,
  1693. asection **local_sections)
  1694. {
  1695. Elf_Internal_Rela *rel;
  1696. Elf_Internal_Rela *relend;
  1697. riscv_pcrel_relocs pcrel_relocs;
  1698. bool ret = false;
  1699. struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
  1700. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
  1701. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  1702. bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
  1703. bool absolute;
  1704. if (!riscv_init_pcrel_relocs (&pcrel_relocs))
  1705. return false;
  1706. relend = relocs + input_section->reloc_count;
  1707. for (rel = relocs; rel < relend; rel++)
  1708. {
  1709. unsigned long r_symndx;
  1710. struct elf_link_hash_entry *h;
  1711. Elf_Internal_Sym *sym;
  1712. asection *sec;
  1713. bfd_vma relocation;
  1714. bfd_reloc_status_type r = bfd_reloc_ok;
  1715. const char *name = NULL;
  1716. bfd_vma off, ie_off;
  1717. bool unresolved_reloc, is_ie = false;
  1718. bfd_vma pc = sec_addr (input_section) + rel->r_offset;
  1719. int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
  1720. reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
  1721. const char *msg = NULL;
  1722. char *msg_buf = NULL;
  1723. bool resolved_to_zero;
  1724. if (howto == NULL
  1725. || r_type == R_RISCV_GNU_VTINHERIT || r_type == R_RISCV_GNU_VTENTRY)
  1726. continue;
  1727. /* This is a final link. */
  1728. r_symndx = ELFNN_R_SYM (rel->r_info);
  1729. h = NULL;
  1730. sym = NULL;
  1731. sec = NULL;
  1732. unresolved_reloc = false;
  1733. if (r_symndx < symtab_hdr->sh_info)
  1734. {
  1735. sym = local_syms + r_symndx;
  1736. sec = local_sections[r_symndx];
  1737. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1738. /* Relocate against local STT_GNU_IFUNC symbol. */
  1739. if (!bfd_link_relocatable (info)
  1740. && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  1741. {
  1742. h = riscv_elf_get_local_sym_hash (htab, input_bfd, rel, false);
  1743. if (h == NULL)
  1744. abort ();
  1745. /* Set STT_GNU_IFUNC symbol value. */
  1746. h->root.u.def.value = sym->st_value;
  1747. h->root.u.def.section = sec;
  1748. }
  1749. }
  1750. else
  1751. {
  1752. bool warned, ignored;
  1753. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1754. r_symndx, symtab_hdr, sym_hashes,
  1755. h, sec, relocation,
  1756. unresolved_reloc, warned, ignored);
  1757. if (warned)
  1758. {
  1759. /* To avoid generating warning messages about truncated
  1760. relocations, set the relocation's address to be the same as
  1761. the start of this section. */
  1762. if (input_section->output_section != NULL)
  1763. relocation = input_section->output_section->vma;
  1764. else
  1765. relocation = 0;
  1766. }
  1767. }
  1768. if (sec != NULL && discarded_section (sec))
  1769. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1770. rel, 1, relend, howto, 0, contents);
  1771. if (bfd_link_relocatable (info))
  1772. continue;
  1773. /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
  1774. it here if it is defined in a non-shared object. */
  1775. if (h != NULL
  1776. && h->type == STT_GNU_IFUNC
  1777. && h->def_regular)
  1778. {
  1779. asection *plt, *base_got;
  1780. if ((input_section->flags & SEC_ALLOC) == 0)
  1781. {
  1782. /* If this is a SHT_NOTE section without SHF_ALLOC, treat
  1783. STT_GNU_IFUNC symbol as STT_FUNC. */
  1784. if (elf_section_type (input_section) == SHT_NOTE)
  1785. goto skip_ifunc;
  1786. /* Dynamic relocs are not propagated for SEC_DEBUGGING
  1787. sections because such sections are not SEC_ALLOC and
  1788. thus ld.so will not process them. */
  1789. if ((input_section->flags & SEC_DEBUGGING) != 0)
  1790. continue;
  1791. abort ();
  1792. }
  1793. else if (h->plt.offset == (bfd_vma) -1
  1794. /* The following relocation may not need the .plt entries
  1795. when all references to a STT_GNU_IFUNC symbols are done
  1796. via GOT or static function pointers. */
  1797. && r_type != R_RISCV_32
  1798. && r_type != R_RISCV_64
  1799. && r_type != R_RISCV_HI20
  1800. && r_type != R_RISCV_GOT_HI20
  1801. && r_type != R_RISCV_LO12_I
  1802. && r_type != R_RISCV_LO12_S)
  1803. goto bad_ifunc_reloc;
  1804. /* STT_GNU_IFUNC symbol must go through PLT. */
  1805. plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
  1806. relocation = plt->output_section->vma
  1807. + plt->output_offset
  1808. + h->plt.offset;
  1809. switch (r_type)
  1810. {
  1811. case R_RISCV_32:
  1812. case R_RISCV_64:
  1813. if (rel->r_addend != 0)
  1814. {
  1815. if (h->root.root.string)
  1816. name = h->root.root.string;
  1817. else
  1818. name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
  1819. _bfd_error_handler
  1820. /* xgettext:c-format */
  1821. (_("%pB: relocation %s against STT_GNU_IFUNC "
  1822. "symbol `%s' has non-zero addend: %" PRId64),
  1823. input_bfd, howto->name, name, (int64_t) rel->r_addend);
  1824. bfd_set_error (bfd_error_bad_value);
  1825. return false;
  1826. }
  1827. /* Generate dynamic relocation only when there is a non-GOT
  1828. reference in a shared object or there is no PLT. */
  1829. if ((bfd_link_pic (info) && h->non_got_ref)
  1830. || h->plt.offset == (bfd_vma) -1)
  1831. {
  1832. Elf_Internal_Rela outrel;
  1833. asection *sreloc;
  1834. /* Need a dynamic relocation to get the real function
  1835. address. */
  1836. outrel.r_offset = _bfd_elf_section_offset (output_bfd,
  1837. info,
  1838. input_section,
  1839. rel->r_offset);
  1840. if (outrel.r_offset == (bfd_vma) -1
  1841. || outrel.r_offset == (bfd_vma) -2)
  1842. abort ();
  1843. outrel.r_offset += input_section->output_section->vma
  1844. + input_section->output_offset;
  1845. if (h->dynindx == -1
  1846. || h->forced_local
  1847. || bfd_link_executable (info))
  1848. {
  1849. info->callbacks->minfo
  1850. (_("Local IFUNC function `%s' in %pB\n"),
  1851. h->root.root.string,
  1852. h->root.u.def.section->owner);
  1853. /* This symbol is resolved locally. */
  1854. outrel.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
  1855. outrel.r_addend = h->root.u.def.value
  1856. + h->root.u.def.section->output_section->vma
  1857. + h->root.u.def.section->output_offset;
  1858. }
  1859. else
  1860. {
  1861. outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
  1862. outrel.r_addend = 0;
  1863. }
  1864. /* Dynamic relocations are stored in
  1865. 1. .rela.ifunc section in PIC object.
  1866. 2. .rela.got section in dynamic executable.
  1867. 3. .rela.iplt section in static executable. */
  1868. if (bfd_link_pic (info))
  1869. sreloc = htab->elf.irelifunc;
  1870. else if (htab->elf.splt != NULL)
  1871. sreloc = htab->elf.srelgot;
  1872. else
  1873. sreloc = htab->elf.irelplt;
  1874. riscv_elf_append_rela (output_bfd, sreloc, &outrel);
  1875. /* If this reloc is against an external symbol, we
  1876. do not want to fiddle with the addend. Otherwise,
  1877. we need to include the symbol value so that it
  1878. becomes an addend for the dynamic reloc. For an
  1879. internal symbol, we have updated addend. */
  1880. continue;
  1881. }
  1882. goto do_relocation;
  1883. case R_RISCV_GOT_HI20:
  1884. base_got = htab->elf.sgot;
  1885. off = h->got.offset;
  1886. if (base_got == NULL)
  1887. abort ();
  1888. if (off == (bfd_vma) -1)
  1889. {
  1890. bfd_vma plt_idx;
  1891. /* We can't use h->got.offset here to save state, or
  1892. even just remember the offset, as finish_dynamic_symbol
  1893. would use that as offset into .got. */
  1894. if (htab->elf.splt != NULL)
  1895. {
  1896. plt_idx = (h->plt.offset - PLT_HEADER_SIZE)
  1897. / PLT_ENTRY_SIZE;
  1898. off = GOTPLT_HEADER_SIZE + (plt_idx * GOT_ENTRY_SIZE);
  1899. base_got = htab->elf.sgotplt;
  1900. }
  1901. else
  1902. {
  1903. plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
  1904. off = plt_idx * GOT_ENTRY_SIZE;
  1905. base_got = htab->elf.igotplt;
  1906. }
  1907. if (h->dynindx == -1
  1908. || h->forced_local
  1909. || info->symbolic)
  1910. {
  1911. /* This references the local definition. We must
  1912. initialize this entry in the global offset table.
  1913. Since the offset must always be a multiple of 8,
  1914. we use the least significant bit to record
  1915. whether we have initialized it already.
  1916. When doing a dynamic link, we create a .rela.got
  1917. relocation entry to initialize the value. This
  1918. is done in the finish_dynamic_symbol routine. */
  1919. if ((off & 1) != 0)
  1920. off &= ~1;
  1921. else
  1922. {
  1923. bfd_put_NN (output_bfd, relocation,
  1924. base_got->contents + off);
  1925. /* Note that this is harmless for the case,
  1926. as -1 | 1 still is -1. */
  1927. h->got.offset |= 1;
  1928. }
  1929. }
  1930. }
  1931. relocation = base_got->output_section->vma
  1932. + base_got->output_offset + off;
  1933. if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
  1934. relocation, r_type,
  1935. false))
  1936. r = bfd_reloc_overflow;
  1937. goto do_relocation;
  1938. case R_RISCV_CALL:
  1939. case R_RISCV_CALL_PLT:
  1940. case R_RISCV_HI20:
  1941. case R_RISCV_LO12_I:
  1942. case R_RISCV_LO12_S:
  1943. goto do_relocation;
  1944. case R_RISCV_PCREL_HI20:
  1945. if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
  1946. relocation, r_type,
  1947. false))
  1948. r = bfd_reloc_overflow;
  1949. goto do_relocation;
  1950. default:
  1951. bad_ifunc_reloc:
  1952. if (h->root.root.string)
  1953. name = h->root.root.string;
  1954. else
  1955. /* The entry of local ifunc is fake in global hash table,
  1956. we should find the name by the original local symbol. */
  1957. name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
  1958. _bfd_error_handler
  1959. /* xgettext:c-format */
  1960. (_("%pB: relocation %s against STT_GNU_IFUNC "
  1961. "symbol `%s' isn't supported"), input_bfd,
  1962. howto->name, name);
  1963. bfd_set_error (bfd_error_bad_value);
  1964. return false;
  1965. }
  1966. }
  1967. skip_ifunc:
  1968. if (h != NULL)
  1969. name = h->root.root.string;
  1970. else
  1971. {
  1972. name = (bfd_elf_string_from_elf_section
  1973. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  1974. if (name == NULL || *name == '\0')
  1975. name = bfd_section_name (sec);
  1976. }
  1977. resolved_to_zero = (h != NULL
  1978. && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
  1979. switch (r_type)
  1980. {
  1981. case R_RISCV_NONE:
  1982. case R_RISCV_RELAX:
  1983. case R_RISCV_TPREL_ADD:
  1984. case R_RISCV_COPY:
  1985. case R_RISCV_JUMP_SLOT:
  1986. case R_RISCV_RELATIVE:
  1987. /* These require nothing of us at all. */
  1988. continue;
  1989. case R_RISCV_HI20:
  1990. case R_RISCV_BRANCH:
  1991. case R_RISCV_RVC_BRANCH:
  1992. case R_RISCV_RVC_LUI:
  1993. case R_RISCV_LO12_I:
  1994. case R_RISCV_LO12_S:
  1995. case R_RISCV_SET6:
  1996. case R_RISCV_SET8:
  1997. case R_RISCV_SET16:
  1998. case R_RISCV_SET32:
  1999. case R_RISCV_32_PCREL:
  2000. case R_RISCV_DELETE:
  2001. /* These require no special handling beyond perform_relocation. */
  2002. break;
  2003. case R_RISCV_GOT_HI20:
  2004. if (h != NULL)
  2005. {
  2006. bool dyn, pic;
  2007. off = h->got.offset;
  2008. BFD_ASSERT (off != (bfd_vma) -1);
  2009. dyn = elf_hash_table (info)->dynamic_sections_created;
  2010. pic = bfd_link_pic (info);
  2011. if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
  2012. || (pic && SYMBOL_REFERENCES_LOCAL (info, h)))
  2013. {
  2014. /* This is actually a static link, or it is a
  2015. -Bsymbolic link and the symbol is defined
  2016. locally, or the symbol was forced to be local
  2017. because of a version file. We must initialize
  2018. this entry in the global offset table. Since the
  2019. offset must always be a multiple of the word size,
  2020. we use the least significant bit to record whether
  2021. we have initialized it already.
  2022. When doing a dynamic link, we create a .rela.got
  2023. relocation entry to initialize the value. This
  2024. is done in the finish_dynamic_symbol routine. */
  2025. if ((off & 1) != 0)
  2026. off &= ~1;
  2027. else
  2028. {
  2029. bfd_put_NN (output_bfd, relocation,
  2030. htab->elf.sgot->contents + off);
  2031. h->got.offset |= 1;
  2032. }
  2033. }
  2034. else
  2035. unresolved_reloc = false;
  2036. }
  2037. else
  2038. {
  2039. BFD_ASSERT (local_got_offsets != NULL
  2040. && local_got_offsets[r_symndx] != (bfd_vma) -1);
  2041. off = local_got_offsets[r_symndx];
  2042. /* The offset must always be a multiple of the word size.
  2043. So, we can use the least significant bit to record
  2044. whether we have already processed this entry. */
  2045. if ((off & 1) != 0)
  2046. off &= ~1;
  2047. else
  2048. {
  2049. if (bfd_link_pic (info))
  2050. {
  2051. asection *s;
  2052. Elf_Internal_Rela outrel;
  2053. /* We need to generate a R_RISCV_RELATIVE reloc
  2054. for the dynamic linker. */
  2055. s = htab->elf.srelgot;
  2056. BFD_ASSERT (s != NULL);
  2057. outrel.r_offset = sec_addr (htab->elf.sgot) + off;
  2058. outrel.r_info =
  2059. ELFNN_R_INFO (0, R_RISCV_RELATIVE);
  2060. outrel.r_addend = relocation;
  2061. relocation = 0;
  2062. riscv_elf_append_rela (output_bfd, s, &outrel);
  2063. }
  2064. bfd_put_NN (output_bfd, relocation,
  2065. htab->elf.sgot->contents + off);
  2066. local_got_offsets[r_symndx] |= 1;
  2067. }
  2068. }
  2069. if (rel->r_addend != 0)
  2070. {
  2071. msg = _("The addend isn't allowed for R_RISCV_GOT_HI20");
  2072. r = bfd_reloc_dangerous;
  2073. }
  2074. else
  2075. {
  2076. /* Address of got entry. */
  2077. relocation = sec_addr (htab->elf.sgot) + off;
  2078. absolute = riscv_zero_pcrel_hi_reloc (rel, info, pc,
  2079. relocation, contents,
  2080. howto);
  2081. /* Update howto if relocation is changed. */
  2082. howto = riscv_elf_rtype_to_howto (input_bfd,
  2083. ELFNN_R_TYPE (rel->r_info));
  2084. if (howto == NULL)
  2085. r = bfd_reloc_notsupported;
  2086. else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
  2087. relocation, r_type,
  2088. absolute))
  2089. r = bfd_reloc_overflow;
  2090. }
  2091. break;
  2092. case R_RISCV_ADD8:
  2093. case R_RISCV_ADD16:
  2094. case R_RISCV_ADD32:
  2095. case R_RISCV_ADD64:
  2096. {
  2097. bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
  2098. contents + rel->r_offset);
  2099. relocation = old_value + relocation;
  2100. }
  2101. break;
  2102. case R_RISCV_SUB6:
  2103. case R_RISCV_SUB8:
  2104. case R_RISCV_SUB16:
  2105. case R_RISCV_SUB32:
  2106. case R_RISCV_SUB64:
  2107. {
  2108. bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
  2109. contents + rel->r_offset);
  2110. relocation = old_value - relocation;
  2111. }
  2112. break;
  2113. case R_RISCV_CALL:
  2114. case R_RISCV_CALL_PLT:
  2115. /* Handle a call to an undefined weak function. This won't be
  2116. relaxed, so we have to handle it here. */
  2117. if (h != NULL && h->root.type == bfd_link_hash_undefweak
  2118. && (!bfd_link_pic (info) || h->plt.offset == MINUS_ONE))
  2119. {
  2120. /* We can use x0 as the base register. */
  2121. bfd_vma insn = bfd_getl32 (contents + rel->r_offset + 4);
  2122. insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
  2123. bfd_putl32 (insn, contents + rel->r_offset + 4);
  2124. /* Set the relocation value so that we get 0 after the pc
  2125. relative adjustment. */
  2126. relocation = sec_addr (input_section) + rel->r_offset;
  2127. }
  2128. /* Fall through. */
  2129. case R_RISCV_JAL:
  2130. case R_RISCV_RVC_JUMP:
  2131. /* This line has to match the check in _bfd_riscv_relax_section. */
  2132. if (bfd_link_pic (info) && h != NULL && h->plt.offset != MINUS_ONE)
  2133. {
  2134. /* Refer to the PLT entry. */
  2135. relocation = sec_addr (htab->elf.splt) + h->plt.offset;
  2136. unresolved_reloc = false;
  2137. }
  2138. break;
  2139. case R_RISCV_TPREL_HI20:
  2140. relocation = tpoff (info, relocation);
  2141. break;
  2142. case R_RISCV_TPREL_LO12_I:
  2143. case R_RISCV_TPREL_LO12_S:
  2144. relocation = tpoff (info, relocation);
  2145. break;
  2146. case R_RISCV_TPREL_I:
  2147. case R_RISCV_TPREL_S:
  2148. relocation = tpoff (info, relocation);
  2149. if (VALID_ITYPE_IMM (relocation + rel->r_addend))
  2150. {
  2151. /* We can use tp as the base register. */
  2152. bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
  2153. insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
  2154. insn |= X_TP << OP_SH_RS1;
  2155. bfd_putl32 (insn, contents + rel->r_offset);
  2156. }
  2157. else
  2158. r = bfd_reloc_overflow;
  2159. break;
  2160. case R_RISCV_GPREL_I:
  2161. case R_RISCV_GPREL_S:
  2162. {
  2163. bfd_vma gp = riscv_global_pointer_value (info);
  2164. bool x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
  2165. if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
  2166. {
  2167. /* We can use x0 or gp as the base register. */
  2168. bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
  2169. insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
  2170. if (!x0_base)
  2171. {
  2172. rel->r_addend -= gp;
  2173. insn |= X_GP << OP_SH_RS1;
  2174. }
  2175. bfd_putl32 (insn, contents + rel->r_offset);
  2176. }
  2177. else
  2178. r = bfd_reloc_overflow;
  2179. break;
  2180. }
  2181. case R_RISCV_PCREL_HI20:
  2182. absolute = riscv_zero_pcrel_hi_reloc (rel, info, pc, relocation,
  2183. contents, howto);
  2184. /* Update howto if relocation is changed. */
  2185. howto = riscv_elf_rtype_to_howto (input_bfd,
  2186. ELFNN_R_TYPE (rel->r_info));
  2187. if (howto == NULL)
  2188. r = bfd_reloc_notsupported;
  2189. else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
  2190. relocation + rel->r_addend,
  2191. r_type, absolute))
  2192. r = bfd_reloc_overflow;
  2193. break;
  2194. case R_RISCV_PCREL_LO12_I:
  2195. case R_RISCV_PCREL_LO12_S:
  2196. /* We don't allow section symbols plus addends as the auipc address,
  2197. because then riscv_relax_delete_bytes would have to search through
  2198. all relocs to update these addends. This is also ambiguous, as
  2199. we do allow offsets to be added to the target address, which are
  2200. not to be used to find the auipc address. */
  2201. if (((sym != NULL && (ELF_ST_TYPE (sym->st_info) == STT_SECTION))
  2202. || (h != NULL && h->type == STT_SECTION))
  2203. && rel->r_addend)
  2204. {
  2205. msg = _("%pcrel_lo section symbol with an addend");
  2206. r = bfd_reloc_dangerous;
  2207. break;
  2208. }
  2209. if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, relocation, rel,
  2210. input_section, info, howto,
  2211. contents))
  2212. continue;
  2213. r = bfd_reloc_overflow;
  2214. break;
  2215. case R_RISCV_TLS_DTPREL32:
  2216. case R_RISCV_TLS_DTPREL64:
  2217. relocation = dtpoff (info, relocation);
  2218. break;
  2219. case R_RISCV_32:
  2220. case R_RISCV_64:
  2221. if ((input_section->flags & SEC_ALLOC) == 0)
  2222. break;
  2223. if ((bfd_link_pic (info)
  2224. && (h == NULL
  2225. || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2226. && !resolved_to_zero)
  2227. || h->root.type != bfd_link_hash_undefweak)
  2228. && (!howto->pc_relative
  2229. || !SYMBOL_CALLS_LOCAL (info, h)))
  2230. || (!bfd_link_pic (info)
  2231. && h != NULL
  2232. && h->dynindx != -1
  2233. && !h->non_got_ref
  2234. && ((h->def_dynamic
  2235. && !h->def_regular)
  2236. || h->root.type == bfd_link_hash_undefweak
  2237. || h->root.type == bfd_link_hash_undefined)))
  2238. {
  2239. Elf_Internal_Rela outrel;
  2240. asection *sreloc;
  2241. bool skip_static_relocation, skip_dynamic_relocation;
  2242. /* When generating a shared object, these relocations
  2243. are copied into the output file to be resolved at run
  2244. time. */
  2245. outrel.r_offset =
  2246. _bfd_elf_section_offset (output_bfd, info, input_section,
  2247. rel->r_offset);
  2248. skip_static_relocation = outrel.r_offset != (bfd_vma) -2;
  2249. skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2;
  2250. outrel.r_offset += sec_addr (input_section);
  2251. if (skip_dynamic_relocation)
  2252. memset (&outrel, 0, sizeof outrel);
  2253. else if (h != NULL && h->dynindx != -1
  2254. && !(bfd_link_pic (info)
  2255. && SYMBOLIC_BIND (info, h)
  2256. && h->def_regular))
  2257. {
  2258. outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
  2259. outrel.r_addend = rel->r_addend;
  2260. }
  2261. else
  2262. {
  2263. outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
  2264. outrel.r_addend = relocation + rel->r_addend;
  2265. }
  2266. sreloc = elf_section_data (input_section)->sreloc;
  2267. riscv_elf_append_rela (output_bfd, sreloc, &outrel);
  2268. if (skip_static_relocation)
  2269. continue;
  2270. }
  2271. break;
  2272. case R_RISCV_TLS_GOT_HI20:
  2273. is_ie = true;
  2274. /* Fall through. */
  2275. case R_RISCV_TLS_GD_HI20:
  2276. if (h != NULL)
  2277. {
  2278. off = h->got.offset;
  2279. h->got.offset |= 1;
  2280. }
  2281. else
  2282. {
  2283. off = local_got_offsets[r_symndx];
  2284. local_got_offsets[r_symndx] |= 1;
  2285. }
  2286. tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
  2287. BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
  2288. /* If this symbol is referenced by both GD and IE TLS, the IE
  2289. reference's GOT slot follows the GD reference's slots. */
  2290. ie_off = 0;
  2291. if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
  2292. ie_off = 2 * GOT_ENTRY_SIZE;
  2293. if ((off & 1) != 0)
  2294. off &= ~1;
  2295. else
  2296. {
  2297. Elf_Internal_Rela outrel;
  2298. int indx = 0;
  2299. bool need_relocs = false;
  2300. if (htab->elf.srelgot == NULL)
  2301. abort ();
  2302. if (h != NULL)
  2303. {
  2304. bool dyn, pic;
  2305. dyn = htab->elf.dynamic_sections_created;
  2306. pic = bfd_link_pic (info);
  2307. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
  2308. && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h)))
  2309. indx = h->dynindx;
  2310. }
  2311. /* The GOT entries have not been initialized yet. Do it
  2312. now, and emit any relocations. */
  2313. if ((bfd_link_pic (info) || indx != 0)
  2314. && (h == NULL
  2315. || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2316. || h->root.type != bfd_link_hash_undefweak))
  2317. need_relocs = true;
  2318. if (tls_type & GOT_TLS_GD)
  2319. {
  2320. if (need_relocs)
  2321. {
  2322. outrel.r_offset = sec_addr (htab->elf.sgot) + off;
  2323. outrel.r_addend = 0;
  2324. outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN);
  2325. bfd_put_NN (output_bfd, 0,
  2326. htab->elf.sgot->contents + off);
  2327. riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
  2328. if (indx == 0)
  2329. {
  2330. BFD_ASSERT (! unresolved_reloc);
  2331. bfd_put_NN (output_bfd,
  2332. dtpoff (info, relocation),
  2333. (htab->elf.sgot->contents
  2334. + off + RISCV_ELF_WORD_BYTES));
  2335. }
  2336. else
  2337. {
  2338. bfd_put_NN (output_bfd, 0,
  2339. (htab->elf.sgot->contents
  2340. + off + RISCV_ELF_WORD_BYTES));
  2341. outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN);
  2342. outrel.r_offset += RISCV_ELF_WORD_BYTES;
  2343. riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
  2344. }
  2345. }
  2346. else
  2347. {
  2348. /* If we are not emitting relocations for a
  2349. general dynamic reference, then we must be in a
  2350. static link or an executable link with the
  2351. symbol binding locally. Mark it as belonging
  2352. to module 1, the executable. */
  2353. bfd_put_NN (output_bfd, 1,
  2354. htab->elf.sgot->contents + off);
  2355. bfd_put_NN (output_bfd,
  2356. dtpoff (info, relocation),
  2357. (htab->elf.sgot->contents
  2358. + off + RISCV_ELF_WORD_BYTES));
  2359. }
  2360. }
  2361. if (tls_type & GOT_TLS_IE)
  2362. {
  2363. if (need_relocs)
  2364. {
  2365. bfd_put_NN (output_bfd, 0,
  2366. htab->elf.sgot->contents + off + ie_off);
  2367. outrel.r_offset = sec_addr (htab->elf.sgot)
  2368. + off + ie_off;
  2369. outrel.r_addend = 0;
  2370. if (indx == 0)
  2371. outrel.r_addend = tpoff (info, relocation);
  2372. outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN);
  2373. riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
  2374. }
  2375. else
  2376. {
  2377. bfd_put_NN (output_bfd, tpoff (info, relocation),
  2378. htab->elf.sgot->contents + off + ie_off);
  2379. }
  2380. }
  2381. }
  2382. BFD_ASSERT (off < (bfd_vma) -2);
  2383. relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
  2384. if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
  2385. relocation, r_type,
  2386. false))
  2387. r = bfd_reloc_overflow;
  2388. unresolved_reloc = false;
  2389. break;
  2390. default:
  2391. r = bfd_reloc_notsupported;
  2392. }
  2393. /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
  2394. because such sections are not SEC_ALLOC and thus ld.so will
  2395. not process them. */
  2396. if (unresolved_reloc
  2397. && !((input_section->flags & SEC_DEBUGGING) != 0
  2398. && h->def_dynamic)
  2399. && _bfd_elf_section_offset (output_bfd, info, input_section,
  2400. rel->r_offset) != (bfd_vma) -1)
  2401. {
  2402. switch (r_type)
  2403. {
  2404. case R_RISCV_JAL:
  2405. case R_RISCV_RVC_JUMP:
  2406. if (asprintf (&msg_buf,
  2407. _("%%X%%P: relocation %s against `%s' can "
  2408. "not be used when making a shared object; "
  2409. "recompile with -fPIC\n"),
  2410. howto->name,
  2411. h->root.root.string) == -1)
  2412. msg_buf = NULL;
  2413. break;
  2414. default:
  2415. if (asprintf (&msg_buf,
  2416. _("%%X%%P: unresolvable %s relocation against "
  2417. "symbol `%s'\n"),
  2418. howto->name,
  2419. h->root.root.string) == -1)
  2420. msg_buf = NULL;
  2421. break;
  2422. }
  2423. msg = msg_buf;
  2424. r = bfd_reloc_notsupported;
  2425. }
  2426. do_relocation:
  2427. if (r == bfd_reloc_ok)
  2428. r = perform_relocation (howto, rel, relocation, input_section,
  2429. input_bfd, contents);
  2430. /* We should have already detected the error and set message before.
  2431. If the error message isn't set since the linker runs out of memory
  2432. or we don't set it before, then we should set the default message
  2433. with the "internal error" string here. */
  2434. switch (r)
  2435. {
  2436. case bfd_reloc_ok:
  2437. continue;
  2438. case bfd_reloc_overflow:
  2439. info->callbacks->reloc_overflow
  2440. (info, (h ? &h->root : NULL), name, howto->name,
  2441. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  2442. break;
  2443. case bfd_reloc_undefined:
  2444. info->callbacks->undefined_symbol
  2445. (info, name, input_bfd, input_section, rel->r_offset,
  2446. true);
  2447. break;
  2448. case bfd_reloc_outofrange:
  2449. if (msg == NULL)
  2450. msg = _("%X%P: internal error: out of range error\n");
  2451. break;
  2452. case bfd_reloc_notsupported:
  2453. if (msg == NULL)
  2454. msg = _("%X%P: internal error: unsupported relocation error\n");
  2455. break;
  2456. case bfd_reloc_dangerous:
  2457. /* The error message should already be set. */
  2458. if (msg == NULL)
  2459. msg = _("dangerous relocation error");
  2460. info->callbacks->reloc_dangerous
  2461. (info, msg, input_bfd, input_section, rel->r_offset);
  2462. break;
  2463. default:
  2464. msg = _("%X%P: internal error: unknown error\n");
  2465. break;
  2466. }
  2467. /* Do not report error message for the dangerous relocation again. */
  2468. if (msg && r != bfd_reloc_dangerous)
  2469. info->callbacks->einfo (msg);
  2470. /* Free the unused `msg_buf`. */
  2471. free (msg_buf);
  2472. /* We already reported the error via a callback, so don't try to report
  2473. it again by returning false. That leads to spurious errors. */
  2474. ret = true;
  2475. goto out;
  2476. }
  2477. ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
  2478. out:
  2479. riscv_free_pcrel_relocs (&pcrel_relocs);
  2480. return ret;
  2481. }
  2482. /* Finish up dynamic symbol handling. We set the contents of various
  2483. dynamic sections here. */
  2484. static bool
  2485. riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
  2486. struct bfd_link_info *info,
  2487. struct elf_link_hash_entry *h,
  2488. Elf_Internal_Sym *sym)
  2489. {
  2490. struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
  2491. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  2492. if (h->plt.offset != (bfd_vma) -1)
  2493. {
  2494. /* We've decided to create a PLT entry for this symbol. */
  2495. bfd_byte *loc;
  2496. bfd_vma i, header_address, plt_idx, got_offset, got_address;
  2497. uint32_t plt_entry[PLT_ENTRY_INSNS];
  2498. Elf_Internal_Rela rela;
  2499. asection *plt, *gotplt, *relplt;
  2500. /* When building a static executable, use .iplt, .igot.plt and
  2501. .rela.iplt sections for STT_GNU_IFUNC symbols. */
  2502. if (htab->elf.splt != NULL)
  2503. {
  2504. plt = htab->elf.splt;
  2505. gotplt = htab->elf.sgotplt;
  2506. relplt = htab->elf.srelplt;
  2507. }
  2508. else
  2509. {
  2510. plt = htab->elf.iplt;
  2511. gotplt = htab->elf.igotplt;
  2512. relplt = htab->elf.irelplt;
  2513. }
  2514. /* This symbol has an entry in the procedure linkage table. Set
  2515. it up. */
  2516. if ((h->dynindx == -1
  2517. && !((h->forced_local || bfd_link_executable (info))
  2518. && h->def_regular
  2519. && h->type == STT_GNU_IFUNC))
  2520. || plt == NULL
  2521. || gotplt == NULL
  2522. || relplt == NULL)
  2523. return false;
  2524. /* Calculate the address of the PLT header. */
  2525. header_address = sec_addr (plt);
  2526. /* Calculate the index of the entry and the offset of .got.plt entry.
  2527. For static executables, we don't reserve anything. */
  2528. if (plt == htab->elf.splt)
  2529. {
  2530. plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
  2531. got_offset = GOTPLT_HEADER_SIZE + (plt_idx * GOT_ENTRY_SIZE);
  2532. }
  2533. else
  2534. {
  2535. plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
  2536. got_offset = plt_idx * GOT_ENTRY_SIZE;
  2537. }
  2538. /* Calculate the address of the .got.plt entry. */
  2539. got_address = sec_addr (gotplt) + got_offset;
  2540. /* Find out where the .plt entry should go. */
  2541. loc = plt->contents + h->plt.offset;
  2542. /* Fill in the PLT entry itself. */
  2543. if (! riscv_make_plt_entry (output_bfd, got_address,
  2544. header_address + h->plt.offset,
  2545. plt_entry))
  2546. return false;
  2547. for (i = 0; i < PLT_ENTRY_INSNS; i++)
  2548. bfd_putl32 (plt_entry[i], loc + 4*i);
  2549. /* Fill in the initial value of the .got.plt entry. */
  2550. loc = gotplt->contents + (got_address - sec_addr (gotplt));
  2551. bfd_put_NN (output_bfd, sec_addr (plt), loc);
  2552. rela.r_offset = got_address;
  2553. if (h->dynindx == -1
  2554. || ((bfd_link_executable (info)
  2555. || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  2556. && h->def_regular
  2557. && h->type == STT_GNU_IFUNC))
  2558. {
  2559. info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
  2560. h->root.root.string,
  2561. h->root.u.def.section->owner);
  2562. /* If an STT_GNU_IFUNC symbol is locally defined, generate
  2563. R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
  2564. asection *sec = h->root.u.def.section;
  2565. rela.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
  2566. rela.r_addend = h->root.u.def.value
  2567. + sec->output_section->vma
  2568. + sec->output_offset;
  2569. }
  2570. else
  2571. {
  2572. /* Fill in the entry in the .rela.plt section. */
  2573. rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
  2574. rela.r_addend = 0;
  2575. }
  2576. loc = relplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
  2577. bed->s->swap_reloca_out (output_bfd, &rela, loc);
  2578. if (!h->def_regular)
  2579. {
  2580. /* Mark the symbol as undefined, rather than as defined in
  2581. the .plt section. Leave the value alone. */
  2582. sym->st_shndx = SHN_UNDEF;
  2583. /* If the symbol is weak, we do need to clear the value.
  2584. Otherwise, the PLT entry would provide a definition for
  2585. the symbol even if the symbol wasn't defined anywhere,
  2586. and so the symbol would never be NULL. */
  2587. if (!h->ref_regular_nonweak)
  2588. sym->st_value = 0;
  2589. }
  2590. }
  2591. if (h->got.offset != (bfd_vma) -1
  2592. && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE))
  2593. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  2594. {
  2595. asection *sgot;
  2596. asection *srela;
  2597. Elf_Internal_Rela rela;
  2598. bool use_elf_append_rela = true;
  2599. /* This symbol has an entry in the GOT. Set it up. */
  2600. sgot = htab->elf.sgot;
  2601. srela = htab->elf.srelgot;
  2602. BFD_ASSERT (sgot != NULL && srela != NULL);
  2603. rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
  2604. /* Handle the ifunc symbol in GOT entry. */
  2605. if (h->def_regular
  2606. && h->type == STT_GNU_IFUNC)
  2607. {
  2608. if (h->plt.offset == (bfd_vma) -1)
  2609. {
  2610. /* STT_GNU_IFUNC is referenced without PLT. */
  2611. if (htab->elf.splt == NULL)
  2612. {
  2613. /* Use .rela.iplt section to store .got relocations
  2614. in static executable. */
  2615. srela = htab->elf.irelplt;
  2616. /* Do not use riscv_elf_append_rela to add dynamic
  2617. relocs. */
  2618. use_elf_append_rela = false;
  2619. }
  2620. if (SYMBOL_REFERENCES_LOCAL (info, h))
  2621. {
  2622. info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
  2623. h->root.root.string,
  2624. h->root.u.def.section->owner);
  2625. rela.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
  2626. rela.r_addend = (h->root.u.def.value
  2627. + h->root.u.def.section->output_section->vma
  2628. + h->root.u.def.section->output_offset);
  2629. }
  2630. else
  2631. {
  2632. /* Generate R_RISCV_NN. */
  2633. BFD_ASSERT ((h->got.offset & 1) == 0);
  2634. BFD_ASSERT (h->dynindx != -1);
  2635. rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
  2636. rela.r_addend = 0;
  2637. }
  2638. }
  2639. else if (bfd_link_pic (info))
  2640. {
  2641. /* Generate R_RISCV_NN. */
  2642. BFD_ASSERT ((h->got.offset & 1) == 0);
  2643. BFD_ASSERT (h->dynindx != -1);
  2644. rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
  2645. rela.r_addend = 0;
  2646. }
  2647. else
  2648. {
  2649. asection *plt;
  2650. if (!h->pointer_equality_needed)
  2651. abort ();
  2652. /* For non-shared object, we can't use .got.plt, which
  2653. contains the real function address if we need pointer
  2654. equality. We load the GOT entry with the PLT entry. */
  2655. plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
  2656. bfd_put_NN (output_bfd, (plt->output_section->vma
  2657. + plt->output_offset
  2658. + h->plt.offset),
  2659. htab->elf.sgot->contents
  2660. + (h->got.offset & ~(bfd_vma) 1));
  2661. return true;
  2662. }
  2663. }
  2664. else if (bfd_link_pic (info)
  2665. && SYMBOL_REFERENCES_LOCAL (info, h))
  2666. {
  2667. /* If this is a local symbol reference, we just want to emit
  2668. a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
  2669. or a pie link, or the symbol was forced to be local because
  2670. of a version file. The entry in the global offset table will
  2671. already have been initialized in the relocate_section function. */
  2672. BFD_ASSERT ((h->got.offset & 1) != 0);
  2673. asection *sec = h->root.u.def.section;
  2674. rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
  2675. rela.r_addend = (h->root.u.def.value
  2676. + sec->output_section->vma
  2677. + sec->output_offset);
  2678. }
  2679. else
  2680. {
  2681. BFD_ASSERT ((h->got.offset & 1) == 0);
  2682. BFD_ASSERT (h->dynindx != -1);
  2683. rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
  2684. rela.r_addend = 0;
  2685. }
  2686. bfd_put_NN (output_bfd, 0,
  2687. sgot->contents + (h->got.offset & ~(bfd_vma) 1));
  2688. if (use_elf_append_rela)
  2689. riscv_elf_append_rela (output_bfd, srela, &rela);
  2690. else
  2691. {
  2692. /* Use riscv_elf_append_rela to add the dynamic relocs into
  2693. .rela.iplt may cause the overwrite problems. Since we insert
  2694. the relocs for PLT didn't handle the reloc_index of .rela.iplt,
  2695. but the riscv_elf_append_rela adds the relocs to the place
  2696. that are calculated from the reloc_index (in seqential).
  2697. One solution is that add these dynamic relocs (GOT IFUNC)
  2698. from the last of .rela.iplt section. */
  2699. bfd_vma iplt_idx = htab->last_iplt_index--;
  2700. bfd_byte *loc = srela->contents
  2701. + iplt_idx * sizeof (ElfNN_External_Rela);
  2702. bed->s->swap_reloca_out (output_bfd, &rela, loc);
  2703. }
  2704. }
  2705. if (h->needs_copy)
  2706. {
  2707. Elf_Internal_Rela rela;
  2708. asection *s;
  2709. /* This symbols needs a copy reloc. Set it up. */
  2710. BFD_ASSERT (h->dynindx != -1);
  2711. rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
  2712. rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY);
  2713. rela.r_addend = 0;
  2714. if (h->root.u.def.section == htab->elf.sdynrelro)
  2715. s = htab->elf.sreldynrelro;
  2716. else
  2717. s = htab->elf.srelbss;
  2718. riscv_elf_append_rela (output_bfd, s, &rela);
  2719. }
  2720. /* Mark some specially defined symbols as absolute. */
  2721. if (h == htab->elf.hdynamic
  2722. || (h == htab->elf.hgot || h == htab->elf.hplt))
  2723. sym->st_shndx = SHN_ABS;
  2724. return true;
  2725. }
  2726. /* Finish up local dynamic symbol handling. We set the contents of
  2727. various dynamic sections here. */
  2728. static int
  2729. riscv_elf_finish_local_dynamic_symbol (void **slot, void *inf)
  2730. {
  2731. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot;
  2732. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  2733. return riscv_elf_finish_dynamic_symbol (info->output_bfd, info, h, NULL);
  2734. }
  2735. /* Finish up the dynamic sections. */
  2736. static bool
  2737. riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
  2738. bfd *dynobj, asection *sdyn)
  2739. {
  2740. struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
  2741. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  2742. size_t dynsize = bed->s->sizeof_dyn;
  2743. bfd_byte *dyncon, *dynconend;
  2744. dynconend = sdyn->contents + sdyn->size;
  2745. for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
  2746. {
  2747. Elf_Internal_Dyn dyn;
  2748. asection *s;
  2749. bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  2750. switch (dyn.d_tag)
  2751. {
  2752. case DT_PLTGOT:
  2753. s = htab->elf.sgotplt;
  2754. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2755. break;
  2756. case DT_JMPREL:
  2757. s = htab->elf.srelplt;
  2758. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2759. break;
  2760. case DT_PLTRELSZ:
  2761. s = htab->elf.srelplt;
  2762. dyn.d_un.d_val = s->size;
  2763. break;
  2764. default:
  2765. continue;
  2766. }
  2767. bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
  2768. }
  2769. return true;
  2770. }
  2771. static bool
  2772. riscv_elf_finish_dynamic_sections (bfd *output_bfd,
  2773. struct bfd_link_info *info)
  2774. {
  2775. bfd *dynobj;
  2776. asection *sdyn;
  2777. struct riscv_elf_link_hash_table *htab;
  2778. htab = riscv_elf_hash_table (info);
  2779. BFD_ASSERT (htab != NULL);
  2780. dynobj = htab->elf.dynobj;
  2781. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2782. if (elf_hash_table (info)->dynamic_sections_created)
  2783. {
  2784. asection *splt;
  2785. bool ret;
  2786. splt = htab->elf.splt;
  2787. BFD_ASSERT (splt != NULL && sdyn != NULL);
  2788. ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
  2789. if (!ret)
  2790. return ret;
  2791. /* Fill in the head and tail entries in the procedure linkage table. */
  2792. if (splt->size > 0)
  2793. {
  2794. int i;
  2795. uint32_t plt_header[PLT_HEADER_INSNS];
  2796. ret = riscv_make_plt_header (output_bfd,
  2797. sec_addr (htab->elf.sgotplt),
  2798. sec_addr (splt), plt_header);
  2799. if (!ret)
  2800. return ret;
  2801. for (i = 0; i < PLT_HEADER_INSNS; i++)
  2802. bfd_putl32 (plt_header[i], splt->contents + 4*i);
  2803. elf_section_data (splt->output_section)->this_hdr.sh_entsize
  2804. = PLT_ENTRY_SIZE;
  2805. }
  2806. }
  2807. if (htab->elf.sgotplt)
  2808. {
  2809. asection *output_section = htab->elf.sgotplt->output_section;
  2810. if (bfd_is_abs_section (output_section))
  2811. {
  2812. (*_bfd_error_handler)
  2813. (_("discarded output section: `%pA'"), htab->elf.sgotplt);
  2814. return false;
  2815. }
  2816. if (htab->elf.sgotplt->size > 0)
  2817. {
  2818. /* Write the first two entries in .got.plt, needed for the dynamic
  2819. linker. */
  2820. bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
  2821. bfd_put_NN (output_bfd, (bfd_vma) 0,
  2822. htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
  2823. }
  2824. elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
  2825. }
  2826. if (htab->elf.sgot)
  2827. {
  2828. asection *output_section = htab->elf.sgot->output_section;
  2829. if (htab->elf.sgot->size > 0)
  2830. {
  2831. /* Set the first entry in the global offset table to the address of
  2832. the dynamic section. */
  2833. bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
  2834. bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
  2835. }
  2836. elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
  2837. }
  2838. /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
  2839. htab_traverse (htab->loc_hash_table,
  2840. riscv_elf_finish_local_dynamic_symbol,
  2841. info);
  2842. return true;
  2843. }
  2844. /* Return address for Ith PLT stub in section PLT, for relocation REL
  2845. or (bfd_vma) -1 if it should not be included. */
  2846. static bfd_vma
  2847. riscv_elf_plt_sym_val (bfd_vma i, const asection *plt,
  2848. const arelent *rel ATTRIBUTE_UNUSED)
  2849. {
  2850. return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
  2851. }
  2852. static enum elf_reloc_type_class
  2853. riscv_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2854. const asection *rel_sec ATTRIBUTE_UNUSED,
  2855. const Elf_Internal_Rela *rela)
  2856. {
  2857. switch (ELFNN_R_TYPE (rela->r_info))
  2858. {
  2859. case R_RISCV_RELATIVE:
  2860. return reloc_class_relative;
  2861. case R_RISCV_JUMP_SLOT:
  2862. return reloc_class_plt;
  2863. case R_RISCV_COPY:
  2864. return reloc_class_copy;
  2865. default:
  2866. return reloc_class_normal;
  2867. }
  2868. }
  2869. /* Given the ELF header flags in FLAGS, it returns a string that describes the
  2870. float ABI. */
  2871. static const char *
  2872. riscv_float_abi_string (flagword flags)
  2873. {
  2874. switch (flags & EF_RISCV_FLOAT_ABI)
  2875. {
  2876. case EF_RISCV_FLOAT_ABI_SOFT:
  2877. return "soft-float";
  2878. break;
  2879. case EF_RISCV_FLOAT_ABI_SINGLE:
  2880. return "single-float";
  2881. break;
  2882. case EF_RISCV_FLOAT_ABI_DOUBLE:
  2883. return "double-float";
  2884. break;
  2885. case EF_RISCV_FLOAT_ABI_QUAD:
  2886. return "quad-float";
  2887. break;
  2888. default:
  2889. abort ();
  2890. }
  2891. }
  2892. /* The information of architecture elf attributes. */
  2893. static riscv_subset_list_t in_subsets;
  2894. static riscv_subset_list_t out_subsets;
  2895. static riscv_subset_list_t merged_subsets;
  2896. /* Predicator for standard extension. */
  2897. static bool
  2898. riscv_std_ext_p (const char *name)
  2899. {
  2900. return (strlen (name) == 1) && (name[0] != 'x') && (name[0] != 's');
  2901. }
  2902. /* Update the output subset's version to match the input when the input
  2903. subset's version is newer. */
  2904. static void
  2905. riscv_update_subset_version (struct riscv_subset_t *in,
  2906. struct riscv_subset_t *out)
  2907. {
  2908. if (in == NULL || out == NULL)
  2909. return;
  2910. /* Update the output ISA versions to the newest ones, but otherwise don't
  2911. provide any errors or warnings about mis-matched ISA versions as it's
  2912. generally too tricky to check for these at link time. */
  2913. if ((in->major_version > out->major_version)
  2914. || (in->major_version == out->major_version
  2915. && in->minor_version > out->minor_version)
  2916. || (out->major_version == RISCV_UNKNOWN_VERSION))
  2917. {
  2918. out->major_version = in->major_version;
  2919. out->minor_version = in->minor_version;
  2920. }
  2921. }
  2922. /* Return true if subset is 'i' or 'e'. */
  2923. static bool
  2924. riscv_i_or_e_p (bfd *ibfd,
  2925. const char *arch,
  2926. struct riscv_subset_t *subset)
  2927. {
  2928. if ((strcasecmp (subset->name, "e") != 0)
  2929. && (strcasecmp (subset->name, "i") != 0))
  2930. {
  2931. _bfd_error_handler
  2932. (_("error: %pB: corrupted ISA string '%s'. "
  2933. "First letter should be 'i' or 'e' but got '%s'"),
  2934. ibfd, arch, subset->name);
  2935. return false;
  2936. }
  2937. return true;
  2938. }
  2939. /* Merge standard extensions.
  2940. Return Value:
  2941. Return FALSE if failed to merge.
  2942. Arguments:
  2943. `bfd`: bfd handler.
  2944. `in_arch`: Raw ISA string for input object.
  2945. `out_arch`: Raw ISA string for output object.
  2946. `pin`: Subset list for input object.
  2947. `pout`: Subset list for output object. */
  2948. static bool
  2949. riscv_merge_std_ext (bfd *ibfd,
  2950. const char *in_arch,
  2951. const char *out_arch,
  2952. struct riscv_subset_t **pin,
  2953. struct riscv_subset_t **pout)
  2954. {
  2955. const char *standard_exts = "mafdqlcbjtpvn";
  2956. const char *p;
  2957. struct riscv_subset_t *in = *pin;
  2958. struct riscv_subset_t *out = *pout;
  2959. /* First letter should be 'i' or 'e'. */
  2960. if (!riscv_i_or_e_p (ibfd, in_arch, in))
  2961. return false;
  2962. if (!riscv_i_or_e_p (ibfd, out_arch, out))
  2963. return false;
  2964. if (strcasecmp (in->name, out->name) != 0)
  2965. {
  2966. /* TODO: We might allow merge 'i' with 'e'. */
  2967. _bfd_error_handler
  2968. (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
  2969. ibfd, in->name, out->name);
  2970. return false;
  2971. }
  2972. riscv_update_subset_version(in, out);
  2973. riscv_add_subset (&merged_subsets,
  2974. out->name, out->major_version, out->minor_version);
  2975. in = in->next;
  2976. out = out->next;
  2977. /* Handle standard extension first. */
  2978. for (p = standard_exts; *p; ++p)
  2979. {
  2980. struct riscv_subset_t *ext_in, *ext_out, *ext_merged;
  2981. char find_ext[2] = {*p, '\0'};
  2982. bool find_in, find_out;
  2983. find_in = riscv_lookup_subset (&in_subsets, find_ext, &ext_in);
  2984. find_out = riscv_lookup_subset (&out_subsets, find_ext, &ext_out);
  2985. if (!find_in && !find_out)
  2986. continue;
  2987. if (find_in && find_out)
  2988. riscv_update_subset_version(ext_in, ext_out);
  2989. ext_merged = find_out ? ext_out : ext_in;
  2990. riscv_add_subset (&merged_subsets, ext_merged->name,
  2991. ext_merged->major_version, ext_merged->minor_version);
  2992. }
  2993. /* Skip all standard extensions. */
  2994. while ((in != NULL) && riscv_std_ext_p (in->name)) in = in->next;
  2995. while ((out != NULL) && riscv_std_ext_p (out->name)) out = out->next;
  2996. *pin = in;
  2997. *pout = out;
  2998. return true;
  2999. }
  3000. /* Merge multi letter extensions. PIN is a pointer to the head of the input
  3001. object subset list. Likewise for POUT and the output object. Return TRUE
  3002. on success and FALSE when a conflict is found. */
  3003. static bool
  3004. riscv_merge_multi_letter_ext (riscv_subset_t **pin,
  3005. riscv_subset_t **pout)
  3006. {
  3007. riscv_subset_t *in = *pin;
  3008. riscv_subset_t *out = *pout;
  3009. riscv_subset_t *tail;
  3010. int cmp;
  3011. while (in && out)
  3012. {
  3013. cmp = riscv_compare_subsets (in->name, out->name);
  3014. if (cmp < 0)
  3015. {
  3016. /* `in' comes before `out', append `in' and increment. */
  3017. riscv_add_subset (&merged_subsets, in->name, in->major_version,
  3018. in->minor_version);
  3019. in = in->next;
  3020. }
  3021. else if (cmp > 0)
  3022. {
  3023. /* `out' comes before `in', append `out' and increment. */
  3024. riscv_add_subset (&merged_subsets, out->name, out->major_version,
  3025. out->minor_version);
  3026. out = out->next;
  3027. }
  3028. else
  3029. {
  3030. /* Both present, check version and increment both. */
  3031. riscv_update_subset_version (in, out);
  3032. riscv_add_subset (&merged_subsets, out->name, out->major_version,
  3033. out->minor_version);
  3034. out = out->next;
  3035. in = in->next;
  3036. }
  3037. }
  3038. if (in || out)
  3039. {
  3040. /* If we're here, either `in' or `out' is running longer than
  3041. the other. So, we need to append the corresponding tail. */
  3042. tail = in ? in : out;
  3043. while (tail)
  3044. {
  3045. riscv_add_subset (&merged_subsets, tail->name, tail->major_version,
  3046. tail->minor_version);
  3047. tail = tail->next;
  3048. }
  3049. }
  3050. return true;
  3051. }
  3052. /* Merge Tag_RISCV_arch attribute. */
  3053. static char *
  3054. riscv_merge_arch_attr_info (bfd *ibfd, char *in_arch, char *out_arch)
  3055. {
  3056. riscv_subset_t *in, *out;
  3057. char *merged_arch_str;
  3058. unsigned xlen_in, xlen_out;
  3059. merged_subsets.head = NULL;
  3060. merged_subsets.tail = NULL;
  3061. riscv_parse_subset_t riscv_rps_ld_in =
  3062. {&in_subsets, _bfd_error_handler, &xlen_in, NULL, false};
  3063. riscv_parse_subset_t riscv_rps_ld_out =
  3064. {&out_subsets, _bfd_error_handler, &xlen_out, NULL, false};
  3065. if (in_arch == NULL && out_arch == NULL)
  3066. return NULL;
  3067. if (in_arch == NULL && out_arch != NULL)
  3068. return out_arch;
  3069. if (in_arch != NULL && out_arch == NULL)
  3070. return in_arch;
  3071. /* Parse subset from ISA string. */
  3072. if (!riscv_parse_subset (&riscv_rps_ld_in, in_arch))
  3073. return NULL;
  3074. if (!riscv_parse_subset (&riscv_rps_ld_out, out_arch))
  3075. return NULL;
  3076. /* Checking XLEN. */
  3077. if (xlen_out != xlen_in)
  3078. {
  3079. _bfd_error_handler
  3080. (_("error: %pB: ISA string of input (%s) doesn't match "
  3081. "output (%s)"), ibfd, in_arch, out_arch);
  3082. return NULL;
  3083. }
  3084. /* Merge subset list. */
  3085. in = in_subsets.head;
  3086. out = out_subsets.head;
  3087. /* Merge standard extension. */
  3088. if (!riscv_merge_std_ext (ibfd, in_arch, out_arch, &in, &out))
  3089. return NULL;
  3090. /* Merge all non-single letter extensions with single call. */
  3091. if (!riscv_merge_multi_letter_ext (&in, &out))
  3092. return NULL;
  3093. if (xlen_in != xlen_out)
  3094. {
  3095. _bfd_error_handler
  3096. (_("error: %pB: XLEN of input (%u) doesn't match "
  3097. "output (%u)"), ibfd, xlen_in, xlen_out);
  3098. return NULL;
  3099. }
  3100. if (xlen_in != ARCH_SIZE)
  3101. {
  3102. _bfd_error_handler
  3103. (_("error: %pB: unsupported XLEN (%u), you might be "
  3104. "using wrong emulation"), ibfd, xlen_in);
  3105. return NULL;
  3106. }
  3107. merged_arch_str = riscv_arch_str (ARCH_SIZE, &merged_subsets);
  3108. /* Release the subset lists. */
  3109. riscv_release_subset_list (&in_subsets);
  3110. riscv_release_subset_list (&out_subsets);
  3111. riscv_release_subset_list (&merged_subsets);
  3112. return merged_arch_str;
  3113. }
  3114. /* Merge object attributes from IBFD into output_bfd of INFO.
  3115. Raise an error if there are conflicting attributes. */
  3116. static bool
  3117. riscv_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
  3118. {
  3119. bfd *obfd = info->output_bfd;
  3120. obj_attribute *in_attr;
  3121. obj_attribute *out_attr;
  3122. bool result = true;
  3123. bool priv_attrs_merged = false;
  3124. const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
  3125. unsigned int i;
  3126. /* Skip linker created files. */
  3127. if (ibfd->flags & BFD_LINKER_CREATED)
  3128. return true;
  3129. /* Skip any input that doesn't have an attribute section.
  3130. This enables to link object files without attribute section with
  3131. any others. */
  3132. if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
  3133. return true;
  3134. if (!elf_known_obj_attributes_proc (obfd)[0].i)
  3135. {
  3136. /* This is the first object. Copy the attributes. */
  3137. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  3138. out_attr = elf_known_obj_attributes_proc (obfd);
  3139. /* Use the Tag_null value to indicate the attributes have been
  3140. initialized. */
  3141. out_attr[0].i = 1;
  3142. return true;
  3143. }
  3144. in_attr = elf_known_obj_attributes_proc (ibfd);
  3145. out_attr = elf_known_obj_attributes_proc (obfd);
  3146. for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
  3147. {
  3148. switch (i)
  3149. {
  3150. case Tag_RISCV_arch:
  3151. if (!out_attr[Tag_RISCV_arch].s)
  3152. out_attr[Tag_RISCV_arch].s = in_attr[Tag_RISCV_arch].s;
  3153. else if (in_attr[Tag_RISCV_arch].s
  3154. && out_attr[Tag_RISCV_arch].s)
  3155. {
  3156. /* Check compatible. */
  3157. char *merged_arch =
  3158. riscv_merge_arch_attr_info (ibfd,
  3159. in_attr[Tag_RISCV_arch].s,
  3160. out_attr[Tag_RISCV_arch].s);
  3161. if (merged_arch == NULL)
  3162. {
  3163. result = false;
  3164. out_attr[Tag_RISCV_arch].s = "";
  3165. }
  3166. else
  3167. out_attr[Tag_RISCV_arch].s = merged_arch;
  3168. }
  3169. break;
  3170. case Tag_RISCV_priv_spec:
  3171. case Tag_RISCV_priv_spec_minor:
  3172. case Tag_RISCV_priv_spec_revision:
  3173. /* If we have handled the privileged elf attributes, then skip it. */
  3174. if (!priv_attrs_merged)
  3175. {
  3176. unsigned int Tag_a = Tag_RISCV_priv_spec;
  3177. unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
  3178. unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
  3179. enum riscv_spec_class in_priv_spec = PRIV_SPEC_CLASS_NONE;
  3180. enum riscv_spec_class out_priv_spec = PRIV_SPEC_CLASS_NONE;
  3181. /* Get the privileged spec class from elf attributes. */
  3182. riscv_get_priv_spec_class_from_numbers (in_attr[Tag_a].i,
  3183. in_attr[Tag_b].i,
  3184. in_attr[Tag_c].i,
  3185. &in_priv_spec);
  3186. riscv_get_priv_spec_class_from_numbers (out_attr[Tag_a].i,
  3187. out_attr[Tag_b].i,
  3188. out_attr[Tag_c].i,
  3189. &out_priv_spec);
  3190. /* Allow to link the object without the privileged specs. */
  3191. if (out_priv_spec == PRIV_SPEC_CLASS_NONE)
  3192. {
  3193. out_attr[Tag_a].i = in_attr[Tag_a].i;
  3194. out_attr[Tag_b].i = in_attr[Tag_b].i;
  3195. out_attr[Tag_c].i = in_attr[Tag_c].i;
  3196. }
  3197. else if (in_priv_spec != PRIV_SPEC_CLASS_NONE
  3198. && in_priv_spec != out_priv_spec)
  3199. {
  3200. _bfd_error_handler
  3201. (_("warning: %pB use privileged spec version %u.%u.%u but "
  3202. "the output use version %u.%u.%u"),
  3203. ibfd,
  3204. in_attr[Tag_a].i,
  3205. in_attr[Tag_b].i,
  3206. in_attr[Tag_c].i,
  3207. out_attr[Tag_a].i,
  3208. out_attr[Tag_b].i,
  3209. out_attr[Tag_c].i);
  3210. /* The privileged spec v1.9.1 can not be linked with others
  3211. since the conflicts, so we plan to drop it in a year or
  3212. two. */
  3213. if (in_priv_spec == PRIV_SPEC_CLASS_1P9P1
  3214. || out_priv_spec == PRIV_SPEC_CLASS_1P9P1)
  3215. {
  3216. _bfd_error_handler
  3217. (_("warning: privileged spec version 1.9.1 can not be "
  3218. "linked with other spec versions"));
  3219. }
  3220. /* Update the output privileged spec to the newest one. */
  3221. if (in_priv_spec > out_priv_spec)
  3222. {
  3223. out_attr[Tag_a].i = in_attr[Tag_a].i;
  3224. out_attr[Tag_b].i = in_attr[Tag_b].i;
  3225. out_attr[Tag_c].i = in_attr[Tag_c].i;
  3226. }
  3227. }
  3228. priv_attrs_merged = true;
  3229. }
  3230. break;
  3231. case Tag_RISCV_unaligned_access:
  3232. out_attr[i].i |= in_attr[i].i;
  3233. break;
  3234. case Tag_RISCV_stack_align:
  3235. if (out_attr[i].i == 0)
  3236. out_attr[i].i = in_attr[i].i;
  3237. else if (in_attr[i].i != 0
  3238. && out_attr[i].i != 0
  3239. && out_attr[i].i != in_attr[i].i)
  3240. {
  3241. _bfd_error_handler
  3242. (_("error: %pB use %u-byte stack aligned but the output "
  3243. "use %u-byte stack aligned"),
  3244. ibfd, in_attr[i].i, out_attr[i].i);
  3245. result = false;
  3246. }
  3247. break;
  3248. default:
  3249. result &= _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
  3250. }
  3251. /* If out_attr was copied from in_attr then it won't have a type yet. */
  3252. if (in_attr[i].type && !out_attr[i].type)
  3253. out_attr[i].type = in_attr[i].type;
  3254. }
  3255. /* Merge Tag_compatibility attributes and any common GNU ones. */
  3256. if (!_bfd_elf_merge_object_attributes (ibfd, info))
  3257. return false;
  3258. /* Check for any attributes not known on RISC-V. */
  3259. result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
  3260. return result;
  3261. }
  3262. /* Merge backend specific data from an object file to the output
  3263. object file when linking. */
  3264. static bool
  3265. _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  3266. {
  3267. bfd *obfd = info->output_bfd;
  3268. flagword new_flags, old_flags;
  3269. if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd))
  3270. return true;
  3271. if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
  3272. {
  3273. (*_bfd_error_handler)
  3274. (_("%pB: ABI is incompatible with that of the selected emulation:\n"
  3275. " target emulation `%s' does not match `%s'"),
  3276. ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
  3277. return false;
  3278. }
  3279. if (!_bfd_elf_merge_object_attributes (ibfd, info))
  3280. return false;
  3281. if (!riscv_merge_attributes (ibfd, info))
  3282. return false;
  3283. /* Check to see if the input BFD actually contains any sections. If not,
  3284. its flags may not have been initialized either, but it cannot actually
  3285. cause any incompatibility. Do not short-circuit dynamic objects; their
  3286. section list may be emptied by elf_link_add_object_symbols.
  3287. Also check to see if there are no code sections in the input. In this
  3288. case, there is no need to check for code specific flags. */
  3289. if (!(ibfd->flags & DYNAMIC))
  3290. {
  3291. bool null_input_bfd = true;
  3292. bool only_data_sections = true;
  3293. asection *sec;
  3294. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  3295. {
  3296. null_input_bfd = false;
  3297. if ((bfd_section_flags (sec)
  3298. & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
  3299. == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
  3300. {
  3301. only_data_sections = false;
  3302. break;
  3303. }
  3304. }
  3305. if (null_input_bfd || only_data_sections)
  3306. return true;
  3307. }
  3308. new_flags = elf_elfheader (ibfd)->e_flags;
  3309. old_flags = elf_elfheader (obfd)->e_flags;
  3310. if (!elf_flags_init (obfd))
  3311. {
  3312. elf_flags_init (obfd) = true;
  3313. elf_elfheader (obfd)->e_flags = new_flags;
  3314. return true;
  3315. }
  3316. /* Disallow linking different float ABIs. */
  3317. if ((old_flags ^ new_flags) & EF_RISCV_FLOAT_ABI)
  3318. {
  3319. (*_bfd_error_handler)
  3320. (_("%pB: can't link %s modules with %s modules"), ibfd,
  3321. riscv_float_abi_string (new_flags),
  3322. riscv_float_abi_string (old_flags));
  3323. goto fail;
  3324. }
  3325. /* Disallow linking RVE and non-RVE. */
  3326. if ((old_flags ^ new_flags) & EF_RISCV_RVE)
  3327. {
  3328. (*_bfd_error_handler)
  3329. (_("%pB: can't link RVE with other target"), ibfd);
  3330. goto fail;
  3331. }
  3332. /* Allow linking RVC and non-RVC, and keep the RVC flag. */
  3333. elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_RVC;
  3334. return true;
  3335. fail:
  3336. bfd_set_error (bfd_error_bad_value);
  3337. return false;
  3338. }
  3339. /* A second format for recording PC-relative hi relocations. This stores the
  3340. information required to relax them to GP-relative addresses. */
  3341. typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc;
  3342. struct riscv_pcgp_hi_reloc
  3343. {
  3344. bfd_vma hi_sec_off;
  3345. bfd_vma hi_addend;
  3346. bfd_vma hi_addr;
  3347. unsigned hi_sym;
  3348. asection *sym_sec;
  3349. bool undefined_weak;
  3350. riscv_pcgp_hi_reloc *next;
  3351. };
  3352. typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc;
  3353. struct riscv_pcgp_lo_reloc
  3354. {
  3355. bfd_vma hi_sec_off;
  3356. riscv_pcgp_lo_reloc *next;
  3357. };
  3358. typedef struct
  3359. {
  3360. riscv_pcgp_hi_reloc *hi;
  3361. riscv_pcgp_lo_reloc *lo;
  3362. } riscv_pcgp_relocs;
  3363. /* Initialize the pcgp reloc info in P. */
  3364. static bool
  3365. riscv_init_pcgp_relocs (riscv_pcgp_relocs *p)
  3366. {
  3367. p->hi = NULL;
  3368. p->lo = NULL;
  3369. return true;
  3370. }
  3371. /* Free the pcgp reloc info in P. */
  3372. static void
  3373. riscv_free_pcgp_relocs (riscv_pcgp_relocs *p,
  3374. bfd *abfd ATTRIBUTE_UNUSED,
  3375. asection *sec ATTRIBUTE_UNUSED)
  3376. {
  3377. riscv_pcgp_hi_reloc *c;
  3378. riscv_pcgp_lo_reloc *l;
  3379. for (c = p->hi; c != NULL; )
  3380. {
  3381. riscv_pcgp_hi_reloc *next = c->next;
  3382. free (c);
  3383. c = next;
  3384. }
  3385. for (l = p->lo; l != NULL; )
  3386. {
  3387. riscv_pcgp_lo_reloc *next = l->next;
  3388. free (l);
  3389. l = next;
  3390. }
  3391. }
  3392. /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
  3393. The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
  3394. relax the corresponding lo part reloc. */
  3395. static bool
  3396. riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off,
  3397. bfd_vma hi_addend, bfd_vma hi_addr,
  3398. unsigned hi_sym, asection *sym_sec,
  3399. bool undefined_weak)
  3400. {
  3401. riscv_pcgp_hi_reloc *new = bfd_malloc (sizeof (*new));
  3402. if (!new)
  3403. return false;
  3404. new->hi_sec_off = hi_sec_off;
  3405. new->hi_addend = hi_addend;
  3406. new->hi_addr = hi_addr;
  3407. new->hi_sym = hi_sym;
  3408. new->sym_sec = sym_sec;
  3409. new->undefined_weak = undefined_weak;
  3410. new->next = p->hi;
  3411. p->hi = new;
  3412. return true;
  3413. }
  3414. /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
  3415. This is used by a lo part reloc to find the corresponding hi part reloc. */
  3416. static riscv_pcgp_hi_reloc *
  3417. riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
  3418. {
  3419. riscv_pcgp_hi_reloc *c;
  3420. for (c = p->hi; c != NULL; c = c->next)
  3421. if (c->hi_sec_off == hi_sec_off)
  3422. return c;
  3423. return NULL;
  3424. }
  3425. /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
  3426. This is used to record relocs that can't be relaxed. */
  3427. static bool
  3428. riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
  3429. {
  3430. riscv_pcgp_lo_reloc *new = bfd_malloc (sizeof (*new));
  3431. if (!new)
  3432. return false;
  3433. new->hi_sec_off = hi_sec_off;
  3434. new->next = p->lo;
  3435. p->lo = new;
  3436. return true;
  3437. }
  3438. /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
  3439. This is used by a hi part reloc to find the corresponding lo part reloc. */
  3440. static bool
  3441. riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
  3442. {
  3443. riscv_pcgp_lo_reloc *c;
  3444. for (c = p->lo; c != NULL; c = c->next)
  3445. if (c->hi_sec_off == hi_sec_off)
  3446. return true;
  3447. return false;
  3448. }
  3449. static void
  3450. riscv_update_pcgp_relocs (riscv_pcgp_relocs *p, asection *deleted_sec,
  3451. bfd_vma deleted_addr, size_t deleted_count)
  3452. {
  3453. /* Bytes have already been deleted and toaddr should match the old section
  3454. size for our checks, so adjust it here. */
  3455. bfd_vma toaddr = deleted_sec->size + deleted_count;
  3456. riscv_pcgp_lo_reloc *l;
  3457. riscv_pcgp_hi_reloc *h;
  3458. /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
  3459. entries where they occur after the deleted bytes. */
  3460. for (l = p->lo; l != NULL; l = l->next)
  3461. if (l->hi_sec_off > deleted_addr
  3462. && l->hi_sec_off < toaddr)
  3463. l->hi_sec_off -= deleted_count;
  3464. /* Update both section offsets, and symbol values of pcrel_hi relocs where
  3465. these values occur after the deleted bytes. */
  3466. for (h = p->hi; h != NULL; h = h->next)
  3467. {
  3468. if (h->hi_sec_off > deleted_addr
  3469. && h->hi_sec_off < toaddr)
  3470. h->hi_sec_off -= deleted_count;
  3471. if (h->sym_sec == deleted_sec
  3472. && h->hi_addr > deleted_addr
  3473. && h->hi_addr < toaddr)
  3474. h->hi_addr -= deleted_count;
  3475. }
  3476. }
  3477. /* Delete some bytes from a section while relaxing. */
  3478. static bool
  3479. riscv_relax_delete_bytes (bfd *abfd,
  3480. asection *sec,
  3481. bfd_vma addr,
  3482. size_t count,
  3483. struct bfd_link_info *link_info,
  3484. riscv_pcgp_relocs *p)
  3485. {
  3486. unsigned int i, symcount;
  3487. bfd_vma toaddr = sec->size;
  3488. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
  3489. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  3490. unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  3491. struct bfd_elf_section_data *data = elf_section_data (sec);
  3492. bfd_byte *contents = data->this_hdr.contents;
  3493. /* Actually delete the bytes. */
  3494. sec->size -= count;
  3495. memmove (contents + addr, contents + addr + count, toaddr - addr - count);
  3496. /* Adjust the location of all of the relocs. Note that we need not
  3497. adjust the addends, since all PC-relative references must be against
  3498. symbols, which we will adjust below. */
  3499. for (i = 0; i < sec->reloc_count; i++)
  3500. if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr)
  3501. data->relocs[i].r_offset -= count;
  3502. /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
  3503. table for which these values occur after the deleted bytes. */
  3504. if (p)
  3505. riscv_update_pcgp_relocs (p, sec, addr, count);
  3506. /* Adjust the local symbols defined in this section. */
  3507. for (i = 0; i < symtab_hdr->sh_info; i++)
  3508. {
  3509. Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
  3510. if (sym->st_shndx == sec_shndx)
  3511. {
  3512. /* If the symbol is in the range of memory we just moved, we
  3513. have to adjust its value. */
  3514. if (sym->st_value > addr && sym->st_value <= toaddr)
  3515. sym->st_value -= count;
  3516. /* If the symbol *spans* the bytes we just deleted (i.e. its
  3517. *end* is in the moved bytes but its *start* isn't), then we
  3518. must adjust its size.
  3519. This test needs to use the original value of st_value, otherwise
  3520. we might accidentally decrease size when deleting bytes right
  3521. before the symbol. But since deleted relocs can't span across
  3522. symbols, we can't have both a st_value and a st_size decrease,
  3523. so it is simpler to just use an else. */
  3524. else if (sym->st_value <= addr
  3525. && sym->st_value + sym->st_size > addr
  3526. && sym->st_value + sym->st_size <= toaddr)
  3527. sym->st_size -= count;
  3528. }
  3529. }
  3530. /* Now adjust the global symbols defined in this section. */
  3531. symcount = ((symtab_hdr->sh_size / sizeof (ElfNN_External_Sym))
  3532. - symtab_hdr->sh_info);
  3533. for (i = 0; i < symcount; i++)
  3534. {
  3535. struct elf_link_hash_entry *sym_hash = sym_hashes[i];
  3536. /* The '--wrap SYMBOL' option is causing a pain when the object file,
  3537. containing the definition of __wrap_SYMBOL, includes a direct
  3538. call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
  3539. the same symbol (which is __wrap_SYMBOL), but still exist as two
  3540. different symbols in 'sym_hashes', we don't want to adjust
  3541. the global symbol __wrap_SYMBOL twice.
  3542. The same problem occurs with symbols that are versioned_hidden, as
  3543. foo becomes an alias for foo@BAR, and hence they need the same
  3544. treatment. */
  3545. if (link_info->wrap_hash != NULL
  3546. || sym_hash->versioned != unversioned)
  3547. {
  3548. struct elf_link_hash_entry **cur_sym_hashes;
  3549. /* Loop only over the symbols which have already been checked. */
  3550. for (cur_sym_hashes = sym_hashes; cur_sym_hashes < &sym_hashes[i];
  3551. cur_sym_hashes++)
  3552. {
  3553. /* If the current symbol is identical to 'sym_hash', that means
  3554. the symbol was already adjusted (or at least checked). */
  3555. if (*cur_sym_hashes == sym_hash)
  3556. break;
  3557. }
  3558. /* Don't adjust the symbol again. */
  3559. if (cur_sym_hashes < &sym_hashes[i])
  3560. continue;
  3561. }
  3562. if ((sym_hash->root.type == bfd_link_hash_defined
  3563. || sym_hash->root.type == bfd_link_hash_defweak)
  3564. && sym_hash->root.u.def.section == sec)
  3565. {
  3566. /* As above, adjust the value if needed. */
  3567. if (sym_hash->root.u.def.value > addr
  3568. && sym_hash->root.u.def.value <= toaddr)
  3569. sym_hash->root.u.def.value -= count;
  3570. /* As above, adjust the size if needed. */
  3571. else if (sym_hash->root.u.def.value <= addr
  3572. && sym_hash->root.u.def.value + sym_hash->size > addr
  3573. && sym_hash->root.u.def.value + sym_hash->size <= toaddr)
  3574. sym_hash->size -= count;
  3575. }
  3576. }
  3577. return true;
  3578. }
  3579. typedef bool (*relax_func_t) (bfd *, asection *, asection *,
  3580. struct bfd_link_info *,
  3581. Elf_Internal_Rela *,
  3582. bfd_vma, bfd_vma, bfd_vma, bool *,
  3583. riscv_pcgp_relocs *,
  3584. bool undefined_weak);
  3585. /* Relax AUIPC + JALR into JAL. */
  3586. static bool
  3587. _bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
  3588. struct bfd_link_info *link_info,
  3589. Elf_Internal_Rela *rel,
  3590. bfd_vma symval,
  3591. bfd_vma max_alignment,
  3592. bfd_vma reserve_size ATTRIBUTE_UNUSED,
  3593. bool *again,
  3594. riscv_pcgp_relocs *pcgp_relocs,
  3595. bool undefined_weak ATTRIBUTE_UNUSED)
  3596. {
  3597. bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
  3598. bfd_vma foff = symval - (sec_addr (sec) + rel->r_offset);
  3599. bool near_zero = (symval + RISCV_IMM_REACH / 2) < RISCV_IMM_REACH;
  3600. bfd_vma auipc, jalr;
  3601. int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
  3602. /* If the call crosses section boundaries, an alignment directive could
  3603. cause the PC-relative offset to later increase, so we need to add in the
  3604. max alignment of any section inclusive from the call to the target.
  3605. Otherwise, we only need to use the alignment of the current section. */
  3606. if (VALID_JTYPE_IMM (foff))
  3607. {
  3608. if (sym_sec->output_section == sec->output_section
  3609. && sym_sec->output_section != bfd_abs_section_ptr)
  3610. max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
  3611. foff += ((bfd_signed_vma) foff < 0 ? -max_alignment : max_alignment);
  3612. }
  3613. /* See if this function call can be shortened. */
  3614. if (!VALID_JTYPE_IMM (foff) && !(!bfd_link_pic (link_info) && near_zero))
  3615. return true;
  3616. /* Shorten the function call. */
  3617. BFD_ASSERT (rel->r_offset + 8 <= sec->size);
  3618. auipc = bfd_getl32 (contents + rel->r_offset);
  3619. jalr = bfd_getl32 (contents + rel->r_offset + 4);
  3620. rd = (jalr >> OP_SH_RD) & OP_MASK_RD;
  3621. rvc = rvc && VALID_CJTYPE_IMM (foff);
  3622. /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
  3623. rvc = rvc && (rd == 0 || (rd == X_RA && ARCH_SIZE == 32));
  3624. if (rvc)
  3625. {
  3626. /* Relax to C.J[AL] rd, addr. */
  3627. r_type = R_RISCV_RVC_JUMP;
  3628. auipc = rd == 0 ? MATCH_C_J : MATCH_C_JAL;
  3629. len = 2;
  3630. }
  3631. else if (VALID_JTYPE_IMM (foff))
  3632. {
  3633. /* Relax to JAL rd, addr. */
  3634. r_type = R_RISCV_JAL;
  3635. auipc = MATCH_JAL | (rd << OP_SH_RD);
  3636. }
  3637. else
  3638. {
  3639. /* Near zero, relax to JALR rd, x0, addr. */
  3640. r_type = R_RISCV_LO12_I;
  3641. auipc = MATCH_JALR | (rd << OP_SH_RD);
  3642. }
  3643. /* Replace the R_RISCV_CALL reloc. */
  3644. rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), r_type);
  3645. /* Replace the AUIPC. */
  3646. riscv_put_insn (8 * len, auipc, contents + rel->r_offset);
  3647. /* Delete unnecessary JALR. */
  3648. *again = true;
  3649. return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len,
  3650. link_info, pcgp_relocs);
  3651. }
  3652. /* Traverse all output sections and return the max alignment. */
  3653. static bfd_vma
  3654. _bfd_riscv_get_max_alignment (asection *sec)
  3655. {
  3656. unsigned int max_alignment_power = 0;
  3657. asection *o;
  3658. for (o = sec->output_section->owner->sections; o != NULL; o = o->next)
  3659. {
  3660. if (o->alignment_power > max_alignment_power)
  3661. max_alignment_power = o->alignment_power;
  3662. }
  3663. return (bfd_vma) 1 << max_alignment_power;
  3664. }
  3665. /* Relax non-PIC global variable references to GP-relative references. */
  3666. static bool
  3667. _bfd_riscv_relax_lui (bfd *abfd,
  3668. asection *sec,
  3669. asection *sym_sec,
  3670. struct bfd_link_info *link_info,
  3671. Elf_Internal_Rela *rel,
  3672. bfd_vma symval,
  3673. bfd_vma max_alignment,
  3674. bfd_vma reserve_size,
  3675. bool *again,
  3676. riscv_pcgp_relocs *pcgp_relocs,
  3677. bool undefined_weak)
  3678. {
  3679. bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
  3680. bfd_vma gp = riscv_global_pointer_value (link_info);
  3681. int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
  3682. BFD_ASSERT (rel->r_offset + 4 <= sec->size);
  3683. if (gp)
  3684. {
  3685. /* If gp and the symbol are in the same output section, which is not the
  3686. abs section, then consider only that output section's alignment. */
  3687. struct bfd_link_hash_entry *h =
  3688. bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, false, false,
  3689. true);
  3690. if (h->u.def.section->output_section == sym_sec->output_section
  3691. && sym_sec->output_section != bfd_abs_section_ptr)
  3692. max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
  3693. }
  3694. /* Is the reference in range of x0 or gp?
  3695. Valid gp range conservatively because of alignment issue. */
  3696. if (undefined_weak
  3697. || (VALID_ITYPE_IMM (symval)
  3698. || (symval >= gp
  3699. && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
  3700. || (symval < gp
  3701. && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
  3702. {
  3703. unsigned sym = ELFNN_R_SYM (rel->r_info);
  3704. switch (ELFNN_R_TYPE (rel->r_info))
  3705. {
  3706. case R_RISCV_LO12_I:
  3707. if (undefined_weak)
  3708. {
  3709. /* Change the RS1 to zero. */
  3710. bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
  3711. insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
  3712. bfd_putl32 (insn, contents + rel->r_offset);
  3713. }
  3714. else
  3715. rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
  3716. return true;
  3717. case R_RISCV_LO12_S:
  3718. if (undefined_weak)
  3719. {
  3720. /* Change the RS1 to zero. */
  3721. bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
  3722. insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
  3723. bfd_putl32 (insn, contents + rel->r_offset);
  3724. }
  3725. else
  3726. rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
  3727. return true;
  3728. case R_RISCV_HI20:
  3729. /* We can delete the unnecessary LUI and reloc. */
  3730. rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
  3731. *again = true;
  3732. return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4,
  3733. link_info, pcgp_relocs);
  3734. default:
  3735. abort ();
  3736. }
  3737. }
  3738. /* Can we relax LUI to C.LUI? Alignment might move the section forward;
  3739. account for this assuming page alignment at worst. In the presence of
  3740. RELRO segment the linker aligns it by one page size, therefore sections
  3741. after the segment can be moved more than one page. */
  3742. if (use_rvc
  3743. && ELFNN_R_TYPE (rel->r_info) == R_RISCV_HI20
  3744. && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval))
  3745. && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval)
  3746. + (link_info->relro ? 2 * ELF_MAXPAGESIZE
  3747. : ELF_MAXPAGESIZE)))
  3748. {
  3749. /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
  3750. bfd_vma lui = bfd_getl32 (contents + rel->r_offset);
  3751. unsigned rd = ((unsigned)lui >> OP_SH_RD) & OP_MASK_RD;
  3752. if (rd == 0 || rd == X_SP)
  3753. return true;
  3754. lui = (lui & (OP_MASK_RD << OP_SH_RD)) | MATCH_C_LUI;
  3755. bfd_putl32 (lui, contents + rel->r_offset);
  3756. /* Replace the R_RISCV_HI20 reloc. */
  3757. rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_RVC_LUI);
  3758. *again = true;
  3759. return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2,
  3760. link_info, pcgp_relocs);
  3761. }
  3762. return true;
  3763. }
  3764. /* Relax non-PIC TLS references to TP-relative references. */
  3765. static bool
  3766. _bfd_riscv_relax_tls_le (bfd *abfd,
  3767. asection *sec,
  3768. asection *sym_sec ATTRIBUTE_UNUSED,
  3769. struct bfd_link_info *link_info,
  3770. Elf_Internal_Rela *rel,
  3771. bfd_vma symval,
  3772. bfd_vma max_alignment ATTRIBUTE_UNUSED,
  3773. bfd_vma reserve_size ATTRIBUTE_UNUSED,
  3774. bool *again,
  3775. riscv_pcgp_relocs *pcgp_relocs,
  3776. bool undefined_weak ATTRIBUTE_UNUSED)
  3777. {
  3778. /* See if this symbol is in range of tp. */
  3779. if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
  3780. return true;
  3781. BFD_ASSERT (rel->r_offset + 4 <= sec->size);
  3782. switch (ELFNN_R_TYPE (rel->r_info))
  3783. {
  3784. case R_RISCV_TPREL_LO12_I:
  3785. rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_I);
  3786. return true;
  3787. case R_RISCV_TPREL_LO12_S:
  3788. rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_S);
  3789. return true;
  3790. case R_RISCV_TPREL_HI20:
  3791. case R_RISCV_TPREL_ADD:
  3792. /* We can delete the unnecessary instruction and reloc. */
  3793. rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
  3794. *again = true;
  3795. return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info,
  3796. pcgp_relocs);
  3797. default:
  3798. abort ();
  3799. }
  3800. }
  3801. /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
  3802. Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
  3803. static bool
  3804. _bfd_riscv_relax_align (bfd *abfd, asection *sec,
  3805. asection *sym_sec,
  3806. struct bfd_link_info *link_info,
  3807. Elf_Internal_Rela *rel,
  3808. bfd_vma symval,
  3809. bfd_vma max_alignment ATTRIBUTE_UNUSED,
  3810. bfd_vma reserve_size ATTRIBUTE_UNUSED,
  3811. bool *again ATTRIBUTE_UNUSED,
  3812. riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
  3813. bool undefined_weak ATTRIBUTE_UNUSED)
  3814. {
  3815. bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
  3816. bfd_vma alignment = 1, pos;
  3817. while (alignment <= rel->r_addend)
  3818. alignment *= 2;
  3819. symval -= rel->r_addend;
  3820. bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
  3821. bfd_vma nop_bytes = aligned_addr - symval;
  3822. /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
  3823. sec->sec_flg0 = true;
  3824. /* Make sure there are enough NOPs to actually achieve the alignment. */
  3825. if (rel->r_addend < nop_bytes)
  3826. {
  3827. _bfd_error_handler
  3828. (_("%pB(%pA+%#" PRIx64 "): %" PRId64 " bytes required for alignment "
  3829. "to %" PRId64 "-byte boundary, but only %" PRId64 " present"),
  3830. abfd, sym_sec, (uint64_t) rel->r_offset,
  3831. (int64_t) nop_bytes, (int64_t) alignment, (int64_t) rel->r_addend);
  3832. bfd_set_error (bfd_error_bad_value);
  3833. return false;
  3834. }
  3835. /* Delete the reloc. */
  3836. rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
  3837. /* If the number of NOPs is already correct, there's nothing to do. */
  3838. if (nop_bytes == rel->r_addend)
  3839. return true;
  3840. /* Write as many RISC-V NOPs as we need. */
  3841. for (pos = 0; pos < (nop_bytes & -4); pos += 4)
  3842. bfd_putl32 (RISCV_NOP, contents + rel->r_offset + pos);
  3843. /* Write a final RVC NOP if need be. */
  3844. if (nop_bytes % 4 != 0)
  3845. bfd_putl16 (RVC_NOP, contents + rel->r_offset + pos);
  3846. /* Delete the excess bytes. */
  3847. return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes,
  3848. rel->r_addend - nop_bytes, link_info,
  3849. NULL);
  3850. }
  3851. /* Relax PC-relative references to GP-relative references. */
  3852. static bool
  3853. _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
  3854. asection *sec,
  3855. asection *sym_sec,
  3856. struct bfd_link_info *link_info,
  3857. Elf_Internal_Rela *rel,
  3858. bfd_vma symval,
  3859. bfd_vma max_alignment,
  3860. bfd_vma reserve_size,
  3861. bool *again ATTRIBUTE_UNUSED,
  3862. riscv_pcgp_relocs *pcgp_relocs,
  3863. bool undefined_weak)
  3864. {
  3865. bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
  3866. bfd_vma gp = riscv_global_pointer_value (link_info);
  3867. BFD_ASSERT (rel->r_offset + 4 <= sec->size);
  3868. /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
  3869. actual target address. */
  3870. riscv_pcgp_hi_reloc hi_reloc;
  3871. memset (&hi_reloc, 0, sizeof (hi_reloc));
  3872. switch (ELFNN_R_TYPE (rel->r_info))
  3873. {
  3874. case R_RISCV_PCREL_LO12_I:
  3875. case R_RISCV_PCREL_LO12_S:
  3876. {
  3877. /* If the %lo has an addend, it isn't for the label pointing at the
  3878. hi part instruction, but rather for the symbol pointed at by the
  3879. hi part instruction. So we must subtract it here for the lookup.
  3880. It is still used below in the final symbol address. */
  3881. bfd_vma hi_sec_off = symval - sec_addr (sym_sec) - rel->r_addend;
  3882. riscv_pcgp_hi_reloc *hi = riscv_find_pcgp_hi_reloc (pcgp_relocs,
  3883. hi_sec_off);
  3884. if (hi == NULL)
  3885. {
  3886. riscv_record_pcgp_lo_reloc (pcgp_relocs, hi_sec_off);
  3887. return true;
  3888. }
  3889. hi_reloc = *hi;
  3890. symval = hi_reloc.hi_addr;
  3891. sym_sec = hi_reloc.sym_sec;
  3892. /* We can not know whether the undefined weak symbol is referenced
  3893. according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
  3894. we have to record the 'undefined_weak' flag when handling the
  3895. corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
  3896. undefined_weak = hi_reloc.undefined_weak;
  3897. }
  3898. break;
  3899. case R_RISCV_PCREL_HI20:
  3900. /* Mergeable symbols and code might later move out of range. */
  3901. if (! undefined_weak
  3902. && sym_sec->flags & (SEC_MERGE | SEC_CODE))
  3903. return true;
  3904. /* If the cooresponding lo relocation has already been seen then it's not
  3905. safe to relax this relocation. */
  3906. if (riscv_find_pcgp_lo_reloc (pcgp_relocs, rel->r_offset))
  3907. return true;
  3908. break;
  3909. default:
  3910. abort ();
  3911. }
  3912. if (gp)
  3913. {
  3914. /* If gp and the symbol are in the same output section, which is not the
  3915. abs section, then consider only that output section's alignment. */
  3916. struct bfd_link_hash_entry *h =
  3917. bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, false, false,
  3918. true);
  3919. if (h->u.def.section->output_section == sym_sec->output_section
  3920. && sym_sec->output_section != bfd_abs_section_ptr)
  3921. max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
  3922. }
  3923. /* Is the reference in range of x0 or gp?
  3924. Valid gp range conservatively because of alignment issue. */
  3925. if (undefined_weak
  3926. || (VALID_ITYPE_IMM (symval)
  3927. || (symval >= gp
  3928. && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
  3929. || (symval < gp
  3930. && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
  3931. {
  3932. unsigned sym = hi_reloc.hi_sym;
  3933. switch (ELFNN_R_TYPE (rel->r_info))
  3934. {
  3935. case R_RISCV_PCREL_LO12_I:
  3936. if (undefined_weak)
  3937. {
  3938. /* Change the RS1 to zero, and then modify the relocation
  3939. type to R_RISCV_LO12_I. */
  3940. bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
  3941. insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
  3942. bfd_putl32 (insn, contents + rel->r_offset);
  3943. rel->r_info = ELFNN_R_INFO (sym, R_RISCV_LO12_I);
  3944. rel->r_addend = hi_reloc.hi_addend;
  3945. }
  3946. else
  3947. {
  3948. rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
  3949. rel->r_addend += hi_reloc.hi_addend;
  3950. }
  3951. return true;
  3952. case R_RISCV_PCREL_LO12_S:
  3953. if (undefined_weak)
  3954. {
  3955. /* Change the RS1 to zero, and then modify the relocation
  3956. type to R_RISCV_LO12_S. */
  3957. bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
  3958. insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
  3959. bfd_putl32 (insn, contents + rel->r_offset);
  3960. rel->r_info = ELFNN_R_INFO (sym, R_RISCV_LO12_S);
  3961. rel->r_addend = hi_reloc.hi_addend;
  3962. }
  3963. else
  3964. {
  3965. rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
  3966. rel->r_addend += hi_reloc.hi_addend;
  3967. }
  3968. return true;
  3969. case R_RISCV_PCREL_HI20:
  3970. riscv_record_pcgp_hi_reloc (pcgp_relocs,
  3971. rel->r_offset,
  3972. rel->r_addend,
  3973. symval,
  3974. ELFNN_R_SYM(rel->r_info),
  3975. sym_sec,
  3976. undefined_weak);
  3977. /* We can delete the unnecessary AUIPC and reloc. */
  3978. rel->r_info = ELFNN_R_INFO (0, R_RISCV_DELETE);
  3979. rel->r_addend = 4;
  3980. return true;
  3981. default:
  3982. abort ();
  3983. }
  3984. }
  3985. return true;
  3986. }
  3987. /* Delete the bytes for R_RISCV_DELETE. */
  3988. static bool
  3989. _bfd_riscv_relax_delete (bfd *abfd,
  3990. asection *sec,
  3991. asection *sym_sec ATTRIBUTE_UNUSED,
  3992. struct bfd_link_info *link_info,
  3993. Elf_Internal_Rela *rel,
  3994. bfd_vma symval ATTRIBUTE_UNUSED,
  3995. bfd_vma max_alignment ATTRIBUTE_UNUSED,
  3996. bfd_vma reserve_size ATTRIBUTE_UNUSED,
  3997. bool *again ATTRIBUTE_UNUSED,
  3998. riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
  3999. bool undefined_weak ATTRIBUTE_UNUSED)
  4000. {
  4001. if (!riscv_relax_delete_bytes (abfd, sec, rel->r_offset, rel->r_addend,
  4002. link_info, NULL))
  4003. return false;
  4004. rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
  4005. return true;
  4006. }
  4007. /* Called by after_allocation to set the information of data segment
  4008. before relaxing. */
  4009. void
  4010. bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info *info,
  4011. int *data_segment_phase)
  4012. {
  4013. struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
  4014. htab->data_segment_phase = data_segment_phase;
  4015. }
  4016. /* Relax a section.
  4017. Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs.
  4018. Pass 1: Deletes the bytes that PCREL relaxation in pass 0 made obsolete.
  4019. Pass 2: Which cannot be disabled, handles code alignment directives. */
  4020. static bool
  4021. _bfd_riscv_relax_section (bfd *abfd, asection *sec,
  4022. struct bfd_link_info *info,
  4023. bool *again)
  4024. {
  4025. Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
  4026. struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
  4027. struct bfd_elf_section_data *data = elf_section_data (sec);
  4028. Elf_Internal_Rela *relocs;
  4029. bool ret = false;
  4030. unsigned int i;
  4031. bfd_vma max_alignment, reserve_size = 0;
  4032. riscv_pcgp_relocs pcgp_relocs;
  4033. *again = false;
  4034. if (bfd_link_relocatable (info)
  4035. || sec->sec_flg0
  4036. || (sec->flags & SEC_RELOC) == 0
  4037. || sec->reloc_count == 0
  4038. || (info->disable_target_specific_optimizations
  4039. && info->relax_pass == 0)
  4040. /* The exp_seg_relro_adjust is enum phase_enum (0x4),
  4041. and defined in ld/ldexp.h. */
  4042. || *(htab->data_segment_phase) == 4)
  4043. return true;
  4044. riscv_init_pcgp_relocs (&pcgp_relocs);
  4045. /* Read this BFD's relocs if we haven't done so already. */
  4046. if (data->relocs)
  4047. relocs = data->relocs;
  4048. else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  4049. info->keep_memory)))
  4050. goto fail;
  4051. if (htab)
  4052. {
  4053. max_alignment = htab->max_alignment;
  4054. if (max_alignment == (bfd_vma) -1)
  4055. {
  4056. max_alignment = _bfd_riscv_get_max_alignment (sec);
  4057. htab->max_alignment = max_alignment;
  4058. }
  4059. }
  4060. else
  4061. max_alignment = _bfd_riscv_get_max_alignment (sec);
  4062. /* Examine and consider relaxing each reloc. */
  4063. for (i = 0; i < sec->reloc_count; i++)
  4064. {
  4065. asection *sym_sec;
  4066. Elf_Internal_Rela *rel = relocs + i;
  4067. relax_func_t relax_func;
  4068. int type = ELFNN_R_TYPE (rel->r_info);
  4069. bfd_vma symval;
  4070. char symtype;
  4071. bool undefined_weak = false;
  4072. relax_func = NULL;
  4073. if (info->relax_pass == 0)
  4074. {
  4075. if (type == R_RISCV_CALL
  4076. || type == R_RISCV_CALL_PLT)
  4077. relax_func = _bfd_riscv_relax_call;
  4078. else if (type == R_RISCV_HI20
  4079. || type == R_RISCV_LO12_I
  4080. || type == R_RISCV_LO12_S)
  4081. relax_func = _bfd_riscv_relax_lui;
  4082. else if (type == R_RISCV_TPREL_HI20
  4083. || type == R_RISCV_TPREL_ADD
  4084. || type == R_RISCV_TPREL_LO12_I
  4085. || type == R_RISCV_TPREL_LO12_S)
  4086. relax_func = _bfd_riscv_relax_tls_le;
  4087. else if (!bfd_link_pic (info)
  4088. && (type == R_RISCV_PCREL_HI20
  4089. || type == R_RISCV_PCREL_LO12_I
  4090. || type == R_RISCV_PCREL_LO12_S))
  4091. relax_func = _bfd_riscv_relax_pc;
  4092. else
  4093. continue;
  4094. /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
  4095. if (i == sec->reloc_count - 1
  4096. || ELFNN_R_TYPE ((rel + 1)->r_info) != R_RISCV_RELAX
  4097. || rel->r_offset != (rel + 1)->r_offset)
  4098. continue;
  4099. /* Skip over the R_RISCV_RELAX. */
  4100. i++;
  4101. }
  4102. else if (info->relax_pass == 1 && type == R_RISCV_DELETE)
  4103. relax_func = _bfd_riscv_relax_delete;
  4104. else if (info->relax_pass == 2 && type == R_RISCV_ALIGN)
  4105. relax_func = _bfd_riscv_relax_align;
  4106. else
  4107. continue;
  4108. data->relocs = relocs;
  4109. /* Read this BFD's contents if we haven't done so already. */
  4110. if (!data->this_hdr.contents
  4111. && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
  4112. goto fail;
  4113. /* Read this BFD's symbols if we haven't done so already. */
  4114. if (symtab_hdr->sh_info != 0
  4115. && !symtab_hdr->contents
  4116. && !(symtab_hdr->contents =
  4117. (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
  4118. symtab_hdr->sh_info,
  4119. 0, NULL, NULL, NULL)))
  4120. goto fail;
  4121. /* Get the value of the symbol referred to by the reloc. */
  4122. if (ELFNN_R_SYM (rel->r_info) < symtab_hdr->sh_info)
  4123. {
  4124. /* A local symbol. */
  4125. Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents
  4126. + ELFNN_R_SYM (rel->r_info));
  4127. reserve_size = (isym->st_size - rel->r_addend) > isym->st_size
  4128. ? 0 : isym->st_size - rel->r_addend;
  4129. /* Relocate against local STT_GNU_IFUNC symbol. we have created
  4130. a fake global symbol entry for this, so deal with the local ifunc
  4131. as a global. */
  4132. if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  4133. continue;
  4134. if (isym->st_shndx == SHN_UNDEF)
  4135. sym_sec = sec, symval = rel->r_offset;
  4136. else
  4137. {
  4138. BFD_ASSERT (isym->st_shndx < elf_numsections (abfd));
  4139. sym_sec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
  4140. #if 0
  4141. /* The purpose of this code is unknown. It breaks linker scripts
  4142. for embedded development that place sections at address zero.
  4143. This code is believed to be unnecessary. Disabling it but not
  4144. yet removing it, in case something breaks. */
  4145. if (sec_addr (sym_sec) == 0)
  4146. continue;
  4147. #endif
  4148. symval = isym->st_value;
  4149. }
  4150. symtype = ELF_ST_TYPE (isym->st_info);
  4151. }
  4152. else
  4153. {
  4154. unsigned long indx;
  4155. struct elf_link_hash_entry *h;
  4156. indx = ELFNN_R_SYM (rel->r_info) - symtab_hdr->sh_info;
  4157. h = elf_sym_hashes (abfd)[indx];
  4158. while (h->root.type == bfd_link_hash_indirect
  4159. || h->root.type == bfd_link_hash_warning)
  4160. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4161. /* Disable the relaxation for ifunc. */
  4162. if (h != NULL && h->type == STT_GNU_IFUNC)
  4163. continue;
  4164. if (h->root.type == bfd_link_hash_undefweak
  4165. && (relax_func == _bfd_riscv_relax_lui
  4166. || relax_func == _bfd_riscv_relax_pc))
  4167. {
  4168. /* For the lui and auipc relaxations, since the symbol
  4169. value of an undefined weak symbol is always be zero,
  4170. we can optimize the patterns into a single LI/MV/ADDI
  4171. instruction.
  4172. Note that, creating shared libraries and pie output may
  4173. break the rule above. Fortunately, since we do not relax
  4174. pc relocs when creating shared libraries and pie output,
  4175. and the absolute address access for R_RISCV_HI20 isn't
  4176. allowed when "-fPIC" is set, the problem of creating shared
  4177. libraries can not happen currently. Once we support the
  4178. auipc relaxations when creating shared libraries, then we will
  4179. need the more rigorous checking for this optimization. */
  4180. undefined_weak = true;
  4181. }
  4182. /* This line has to match the check in riscv_elf_relocate_section
  4183. in the R_RISCV_CALL[_PLT] case. */
  4184. if (bfd_link_pic (info) && h->plt.offset != MINUS_ONE)
  4185. {
  4186. sym_sec = htab->elf.splt;
  4187. symval = h->plt.offset;
  4188. }
  4189. else if (undefined_weak)
  4190. {
  4191. symval = 0;
  4192. sym_sec = bfd_und_section_ptr;
  4193. }
  4194. else if ((h->root.type == bfd_link_hash_defined
  4195. || h->root.type == bfd_link_hash_defweak)
  4196. && h->root.u.def.section != NULL
  4197. && h->root.u.def.section->output_section != NULL)
  4198. {
  4199. symval = h->root.u.def.value;
  4200. sym_sec = h->root.u.def.section;
  4201. }
  4202. else
  4203. continue;
  4204. if (h->type != STT_FUNC)
  4205. reserve_size =
  4206. (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_addend;
  4207. symtype = h->type;
  4208. }
  4209. if (sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE
  4210. && (sym_sec->flags & SEC_MERGE))
  4211. {
  4212. /* At this stage in linking, no SEC_MERGE symbol has been
  4213. adjusted, so all references to such symbols need to be
  4214. passed through _bfd_merged_section_offset. (Later, in
  4215. relocate_section, all SEC_MERGE symbols *except* for
  4216. section symbols have been adjusted.)
  4217. gas may reduce relocations against symbols in SEC_MERGE
  4218. sections to a relocation against the section symbol when
  4219. the original addend was zero. When the reloc is against
  4220. a section symbol we should include the addend in the
  4221. offset passed to _bfd_merged_section_offset, since the
  4222. location of interest is the original symbol. On the
  4223. other hand, an access to "sym+addend" where "sym" is not
  4224. a section symbol should not include the addend; Such an
  4225. access is presumed to be an offset from "sym"; The
  4226. location of interest is just "sym". */
  4227. if (symtype == STT_SECTION)
  4228. symval += rel->r_addend;
  4229. symval = _bfd_merged_section_offset (abfd, &sym_sec,
  4230. elf_section_data (sym_sec)->sec_info,
  4231. symval);
  4232. if (symtype != STT_SECTION)
  4233. symval += rel->r_addend;
  4234. }
  4235. else
  4236. symval += rel->r_addend;
  4237. symval += sec_addr (sym_sec);
  4238. if (!relax_func (abfd, sec, sym_sec, info, rel, symval,
  4239. max_alignment, reserve_size, again,
  4240. &pcgp_relocs, undefined_weak))
  4241. goto fail;
  4242. }
  4243. ret = true;
  4244. fail:
  4245. if (relocs != data->relocs)
  4246. free (relocs);
  4247. riscv_free_pcgp_relocs (&pcgp_relocs, abfd, sec);
  4248. return ret;
  4249. }
  4250. #if ARCH_SIZE == 32
  4251. # define PRSTATUS_SIZE 204
  4252. # define PRSTATUS_OFFSET_PR_CURSIG 12
  4253. # define PRSTATUS_OFFSET_PR_PID 24
  4254. # define PRSTATUS_OFFSET_PR_REG 72
  4255. # define ELF_GREGSET_T_SIZE 128
  4256. # define PRPSINFO_SIZE 128
  4257. # define PRPSINFO_OFFSET_PR_PID 16
  4258. # define PRPSINFO_OFFSET_PR_FNAME 32
  4259. # define PRPSINFO_OFFSET_PR_PSARGS 48
  4260. # define PRPSINFO_PR_FNAME_LENGTH 16
  4261. # define PRPSINFO_PR_PSARGS_LENGTH 80
  4262. #else
  4263. # define PRSTATUS_SIZE 376
  4264. # define PRSTATUS_OFFSET_PR_CURSIG 12
  4265. # define PRSTATUS_OFFSET_PR_PID 32
  4266. # define PRSTATUS_OFFSET_PR_REG 112
  4267. # define ELF_GREGSET_T_SIZE 256
  4268. # define PRPSINFO_SIZE 136
  4269. # define PRPSINFO_OFFSET_PR_PID 24
  4270. # define PRPSINFO_OFFSET_PR_FNAME 40
  4271. # define PRPSINFO_OFFSET_PR_PSARGS 56
  4272. # define PRPSINFO_PR_FNAME_LENGTH 16
  4273. # define PRPSINFO_PR_PSARGS_LENGTH 80
  4274. #endif
  4275. /* Write PRSTATUS and PRPSINFO note into core file. This will be called
  4276. before the generic code in elf.c. By checking the compiler defines we
  4277. only perform any action here if the generic code would otherwise not be
  4278. able to help us. The intention is that bare metal core dumps (where the
  4279. prstatus_t and/or prpsinfo_t might not be available) will use this code,
  4280. while non bare metal tools will use the generic elf code. */
  4281. static char *
  4282. riscv_write_core_note (bfd *abfd ATTRIBUTE_UNUSED,
  4283. char *buf ATTRIBUTE_UNUSED,
  4284. int *bufsiz ATTRIBUTE_UNUSED,
  4285. int note_type ATTRIBUTE_UNUSED, ...)
  4286. {
  4287. switch (note_type)
  4288. {
  4289. default:
  4290. return NULL;
  4291. #if !defined (HAVE_PRPSINFO_T)
  4292. case NT_PRPSINFO:
  4293. {
  4294. char data[PRPSINFO_SIZE] ATTRIBUTE_NONSTRING;
  4295. va_list ap;
  4296. va_start (ap, note_type);
  4297. memset (data, 0, sizeof (data));
  4298. strncpy (data + PRPSINFO_OFFSET_PR_FNAME, va_arg (ap, const char *),
  4299. PRPSINFO_PR_FNAME_LENGTH);
  4300. #if GCC_VERSION == 8000 || GCC_VERSION == 8001
  4301. DIAGNOSTIC_PUSH;
  4302. /* GCC 8.0 and 8.1 warn about 80 equals destination size with
  4303. -Wstringop-truncation:
  4304. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
  4305. */
  4306. DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
  4307. #endif
  4308. strncpy (data + PRPSINFO_OFFSET_PR_PSARGS, va_arg (ap, const char *),
  4309. PRPSINFO_PR_PSARGS_LENGTH);
  4310. #if GCC_VERSION == 8000 || GCC_VERSION == 8001
  4311. DIAGNOSTIC_POP;
  4312. #endif
  4313. va_end (ap);
  4314. return elfcore_write_note (abfd, buf, bufsiz,
  4315. "CORE", note_type, data, sizeof (data));
  4316. }
  4317. #endif /* !HAVE_PRPSINFO_T */
  4318. #if !defined (HAVE_PRSTATUS_T)
  4319. case NT_PRSTATUS:
  4320. {
  4321. char data[PRSTATUS_SIZE];
  4322. va_list ap;
  4323. long pid;
  4324. int cursig;
  4325. const void *greg;
  4326. va_start (ap, note_type);
  4327. memset (data, 0, sizeof(data));
  4328. pid = va_arg (ap, long);
  4329. bfd_put_32 (abfd, pid, data + PRSTATUS_OFFSET_PR_PID);
  4330. cursig = va_arg (ap, int);
  4331. bfd_put_16 (abfd, cursig, data + PRSTATUS_OFFSET_PR_CURSIG);
  4332. greg = va_arg (ap, const void *);
  4333. memcpy (data + PRSTATUS_OFFSET_PR_REG, greg,
  4334. PRSTATUS_SIZE - PRSTATUS_OFFSET_PR_REG - ARCH_SIZE / 8);
  4335. va_end (ap);
  4336. return elfcore_write_note (abfd, buf, bufsiz,
  4337. "CORE", note_type, data, sizeof (data));
  4338. }
  4339. #endif /* !HAVE_PRSTATUS_T */
  4340. }
  4341. }
  4342. /* Support for core dump NOTE sections. */
  4343. static bool
  4344. riscv_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  4345. {
  4346. switch (note->descsz)
  4347. {
  4348. default:
  4349. return false;
  4350. case PRSTATUS_SIZE: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
  4351. /* pr_cursig */
  4352. elf_tdata (abfd)->core->signal
  4353. = bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
  4354. /* pr_pid */
  4355. elf_tdata (abfd)->core->lwpid
  4356. = bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
  4357. break;
  4358. }
  4359. /* Make a ".reg/999" section. */
  4360. return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
  4361. note->descpos + PRSTATUS_OFFSET_PR_REG);
  4362. }
  4363. static bool
  4364. riscv_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  4365. {
  4366. switch (note->descsz)
  4367. {
  4368. default:
  4369. return false;
  4370. case PRPSINFO_SIZE: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
  4371. /* pr_pid */
  4372. elf_tdata (abfd)->core->pid
  4373. = bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
  4374. /* pr_fname */
  4375. elf_tdata (abfd)->core->program = _bfd_elfcore_strndup
  4376. (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME,
  4377. PRPSINFO_PR_FNAME_LENGTH);
  4378. /* pr_psargs */
  4379. elf_tdata (abfd)->core->command = _bfd_elfcore_strndup
  4380. (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS,
  4381. PRPSINFO_PR_PSARGS_LENGTH);
  4382. break;
  4383. }
  4384. /* Note that for some reason, a spurious space is tacked
  4385. onto the end of the args in some (at least one anyway)
  4386. implementations, so strip it off if it exists. */
  4387. {
  4388. char *command = elf_tdata (abfd)->core->command;
  4389. int n = strlen (command);
  4390. if (0 < n && command[n - 1] == ' ')
  4391. command[n - 1] = '\0';
  4392. }
  4393. return true;
  4394. }
  4395. /* Set the right mach type. */
  4396. static bool
  4397. riscv_elf_object_p (bfd *abfd)
  4398. {
  4399. /* There are only two mach types in RISCV currently. */
  4400. if (strcmp (abfd->xvec->name, "elf32-littleriscv") == 0
  4401. || strcmp (abfd->xvec->name, "elf32-bigriscv") == 0)
  4402. bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv32);
  4403. else
  4404. bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv64);
  4405. return true;
  4406. }
  4407. /* Determine whether an object attribute tag takes an integer, a
  4408. string or both. */
  4409. static int
  4410. riscv_elf_obj_attrs_arg_type (int tag)
  4411. {
  4412. return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
  4413. }
  4414. /* Do not choose mapping symbols as a function name. */
  4415. static bfd_size_type
  4416. riscv_maybe_function_sym (const asymbol *sym,
  4417. asection *sec,
  4418. bfd_vma *code_off)
  4419. {
  4420. if (sym->flags & BSF_LOCAL
  4421. && riscv_elf_is_mapping_symbols (sym->name))
  4422. return 0;
  4423. return _bfd_elf_maybe_function_sym (sym, sec, code_off);
  4424. }
  4425. /* Treat the following cases as target special symbols, they are
  4426. usually omitted. */
  4427. static bool
  4428. riscv_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
  4429. {
  4430. /* PR27584, local and empty symbols. Since they are usually
  4431. generated for pcrel relocations. */
  4432. return (!strcmp (sym->name, "")
  4433. || _bfd_elf_is_local_label_name (abfd, sym->name)
  4434. /* PR27916, mapping symbols. */
  4435. || riscv_elf_is_mapping_symbols (sym->name));
  4436. }
  4437. static int
  4438. riscv_elf_additional_program_headers (bfd *abfd,
  4439. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  4440. {
  4441. int ret = 0;
  4442. /* See if we need a PT_RISCV_ATTRIBUTES segment. */
  4443. if (bfd_get_section_by_name (abfd, RISCV_ATTRIBUTES_SECTION_NAME))
  4444. ++ret;
  4445. return ret;
  4446. }
  4447. static bool
  4448. riscv_elf_modify_segment_map (bfd *abfd,
  4449. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  4450. {
  4451. asection *s;
  4452. struct elf_segment_map *m, **pm;
  4453. size_t amt;
  4454. /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
  4455. segment. */
  4456. s = bfd_get_section_by_name (abfd, RISCV_ATTRIBUTES_SECTION_NAME);
  4457. if (s != NULL)
  4458. {
  4459. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  4460. if (m->p_type == PT_RISCV_ATTRIBUTES)
  4461. break;
  4462. /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
  4463. another. */
  4464. if (m == NULL)
  4465. {
  4466. amt = sizeof (*m);
  4467. m = bfd_zalloc (abfd, amt);
  4468. if (m == NULL)
  4469. return false;
  4470. m->p_type = PT_RISCV_ATTRIBUTES;
  4471. m->count = 1;
  4472. m->sections[0] = s;
  4473. /* We want to put it after the PHDR and INTERP segments. */
  4474. pm = &elf_seg_map (abfd);
  4475. while (*pm != NULL
  4476. && ((*pm)->p_type == PT_PHDR
  4477. || (*pm)->p_type == PT_INTERP))
  4478. pm = &(*pm)->next;
  4479. m->next = *pm;
  4480. *pm = m;
  4481. }
  4482. }
  4483. return true;
  4484. }
  4485. /* Merge non-visibility st_other attributes. */
  4486. static void
  4487. riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
  4488. unsigned int st_other,
  4489. bool definition ATTRIBUTE_UNUSED,
  4490. bool dynamic ATTRIBUTE_UNUSED)
  4491. {
  4492. unsigned int isym_sto = st_other & ~ELF_ST_VISIBILITY (-1);
  4493. unsigned int h_sto = h->other & ~ELF_ST_VISIBILITY (-1);
  4494. if (isym_sto == h_sto)
  4495. return;
  4496. if (isym_sto & ~STO_RISCV_VARIANT_CC)
  4497. _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
  4498. h->root.root.string, isym_sto);
  4499. if (isym_sto & STO_RISCV_VARIANT_CC)
  4500. h->other |= STO_RISCV_VARIANT_CC;
  4501. }
  4502. #define TARGET_LITTLE_SYM riscv_elfNN_vec
  4503. #define TARGET_LITTLE_NAME "elfNN-littleriscv"
  4504. #define TARGET_BIG_SYM riscv_elfNN_be_vec
  4505. #define TARGET_BIG_NAME "elfNN-bigriscv"
  4506. #define elf_backend_reloc_type_class riscv_reloc_type_class
  4507. #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
  4508. #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
  4509. #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
  4510. #define bfd_elfNN_bfd_merge_private_bfd_data \
  4511. _bfd_riscv_elf_merge_private_bfd_data
  4512. #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
  4513. #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
  4514. #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
  4515. #define elf_backend_check_relocs riscv_elf_check_relocs
  4516. #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
  4517. #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
  4518. #define elf_backend_relocate_section riscv_elf_relocate_section
  4519. #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
  4520. #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
  4521. #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
  4522. #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
  4523. #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
  4524. #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
  4525. #define elf_backend_object_p riscv_elf_object_p
  4526. #define elf_backend_write_core_note riscv_write_core_note
  4527. #define elf_backend_maybe_function_sym riscv_maybe_function_sym
  4528. #define elf_info_to_howto_rel NULL
  4529. #define elf_info_to_howto riscv_info_to_howto_rela
  4530. #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
  4531. #define bfd_elfNN_mkobject elfNN_riscv_mkobject
  4532. #define elf_backend_additional_program_headers \
  4533. riscv_elf_additional_program_headers
  4534. #define elf_backend_modify_segment_map riscv_elf_modify_segment_map
  4535. #define elf_backend_merge_symbol_attribute riscv_elf_merge_symbol_attribute
  4536. #define elf_backend_init_index_section _bfd_elf_init_1_index_section
  4537. #define elf_backend_can_gc_sections 1
  4538. #define elf_backend_can_refcount 1
  4539. #define elf_backend_want_got_plt 1
  4540. #define elf_backend_plt_readonly 1
  4541. #define elf_backend_plt_alignment 4
  4542. #define elf_backend_want_plt_sym 1
  4543. #define elf_backend_got_header_size (ARCH_SIZE / 8)
  4544. #define elf_backend_want_dynrelro 1
  4545. #define elf_backend_rela_normal 1
  4546. #define elf_backend_default_execstack 0
  4547. #undef elf_backend_obj_attrs_vendor
  4548. #define elf_backend_obj_attrs_vendor "riscv"
  4549. #undef elf_backend_obj_attrs_arg_type
  4550. #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
  4551. #undef elf_backend_obj_attrs_section_type
  4552. #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
  4553. #undef elf_backend_obj_attrs_section
  4554. #define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
  4555. #include "elfNN-target.h"