elf32-csky.c 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373
  1. /* 32-bit ELF support for C-SKY.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by C-SKY Microsystems and Mentor Graphics.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "elf/csky.h"
  23. #include "opcode/csky.h"
  24. #include <assert.h>
  25. #include "libiberty.h"
  26. #include "elf32-csky.h"
  27. /* Data structures used for merging different arch variants.
  28. V1 (510/610) and V2 (8xx) processors are incompatible, but
  29. we can merge wthin each family. */
  30. enum merge_class
  31. {
  32. CSKY_V1,
  33. CSKY_V2
  34. };
  35. typedef const struct csky_arch_for_merge
  36. {
  37. const char *name;
  38. const unsigned long arch_eflag;
  39. /* The files can merge only if they are in same class. */
  40. enum merge_class class;
  41. /* When input files have different levels,
  42. the target sets arch_eflag to the largest level file's arch_eflag. */
  43. unsigned int class_level;
  44. /* Control whether to print warning when merging with different arch. */
  45. unsigned int do_warning;
  46. } csky_arch_for_merge;
  47. static csky_arch_for_merge csky_archs[] =
  48. {
  49. /* 510 and 610 merge to 610 without warning. */
  50. { "ck510", CSKY_ARCH_510, CSKY_V1, 0, 0},
  51. { "ck610", CSKY_ARCH_610, CSKY_V1, 1, 0},
  52. /* 801, 802, 803, 807, 810 merge to largest one. */
  53. { "ck801", CSKY_ARCH_801, CSKY_V2, 0, 1},
  54. { "ck802", CSKY_ARCH_802, CSKY_V2, 1, 1},
  55. { "ck803", CSKY_ARCH_803, CSKY_V2, 2, 1},
  56. { "ck807", CSKY_ARCH_807, CSKY_V2, 3, 1},
  57. { "ck810", CSKY_ARCH_810, CSKY_V2, 4, 1},
  58. { "ck860", CSKY_ARCH_860, CSKY_V2, 5, 1},
  59. { NULL, 0, 0, 0, 0}
  60. };
  61. /* Return the ARCH bits out of ABFD. */
  62. #define bfd_csky_arch(abfd) \
  63. (elf_elfheader (abfd)->e_flags & CSKY_ARCH_MASK)
  64. /* Return the ABI bits out of ABFD. */
  65. #define bfd_csky_abi(abfd) \
  66. (elf_elfheader (abfd)->e_flags & CSKY_ABI_MASK)
  67. /* The index of a howto-item is implicitly equal to
  68. the corresponding Relocation Type Encoding. */
  69. static reloc_howto_type csky_elf_howto_table[] =
  70. {
  71. /* 0 */
  72. HOWTO (R_CKCORE_NONE, /* type */
  73. 0, /* rightshift */
  74. 0, /* size */
  75. 0, /* bitsize */
  76. false, /* pc_relative */
  77. 0, /* bitpos */
  78. complain_overflow_dont, /* complain_on_overflow */
  79. NULL, /* special_function */
  80. "R_CKCORE_NONE", /* name */
  81. false, /* partial_inplace */
  82. 0, /* src_mask */
  83. 0, /* dst_mask */
  84. false), /* pcrel_offset */
  85. /* 1. */
  86. HOWTO (R_CKCORE_ADDR32, /* type */
  87. 0, /* rightshift */
  88. 2, /* size */
  89. 32, /* bitsize */
  90. false, /* pc_relative */
  91. 0, /* bitpos */
  92. complain_overflow_dont, /* complain_on_overflow */
  93. bfd_elf_generic_reloc, /* special_function */
  94. "R_CKCORE_ADDR32", /* name */
  95. false, /* partial_inplace */
  96. 0, /* src_mask */
  97. 0xffffffff, /* dst_mask */
  98. false), /* pcrel_offset */
  99. /* 2: Only for csky v1. */
  100. HOWTO (R_CKCORE_PCREL_IMM8BY4, /* type */
  101. 2, /* rightshift */
  102. 1, /* size */
  103. 8, /* bitsize */
  104. true, /* pc_relative */
  105. 0, /* bitpos */
  106. complain_overflow_bitfield, /* complain_on_overflow */
  107. NULL, /* special_function */
  108. "R_CKCORE_PCREL_IMM8BY4", /* name */
  109. false, /* partial_inplace */
  110. 0xff, /* src_mask */
  111. 0xff, /* dst_mask */
  112. true), /* pcrel_offset */
  113. /* 3: Only for csky v1. */
  114. HOWTO (R_CKCORE_PCREL_IMM11BY2, /* type */
  115. 1, /* rightshift */
  116. 1, /* size */
  117. 11, /* bitsize */
  118. true, /* pc_relative */
  119. 0, /* bitpos */
  120. complain_overflow_signed, /* complain_on_overflow */
  121. bfd_elf_generic_reloc, /* special_function */
  122. "R_CKCORE_PCREL_IMM11BY2", /* name */
  123. false, /* partial_inplace */
  124. 0x7ff, /* src_mask */
  125. 0x7ff, /* dst_mask */
  126. true), /* pcrel_offset */
  127. /* 4: DELETED. */
  128. HOWTO (R_CKCORE_PCREL_IMM4BY2,0,0,0,0,0,0,0,"R_CKCORE_PCREL_IMM4BY2",0,0,0,0),
  129. /* 5. */
  130. HOWTO (R_CKCORE_PCREL32, /* type */
  131. 0, /* rightshift */
  132. 2, /* size */
  133. 32, /* bitsize */
  134. true, /* pc_relative */
  135. 0, /* bitpos */
  136. complain_overflow_dont, /* complain_on_overflow */
  137. bfd_elf_generic_reloc, /* special_function */
  138. "R_CKCORE_PCREL32", /* name */
  139. false, /* partial_inplace */
  140. 0x0, /* src_mask */
  141. 0xffffffff, /* dst_mask */
  142. true), /* pcrel_offset */
  143. /* 6: Only for csky v1. */
  144. HOWTO (R_CKCORE_PCREL_JSR_IMM11BY2, /* type */
  145. 1, /* rightshift */
  146. 1, /* size */
  147. 11, /* bitsize */
  148. true, /* pc_relative */
  149. 0, /* bitpos */
  150. complain_overflow_signed, /* complain_on_overflow */
  151. bfd_elf_generic_reloc, /* special_function */
  152. "R_CKCORE_PCREL_JSR_IMM11BY2", /* name */
  153. false, /* partial_inplace */
  154. 0x7ff, /* src_mask */
  155. 0x7ff, /* dst_mask */
  156. true), /* pcrel_offset */
  157. /* 7: GNU extension to record C++ vtable member usage. */
  158. HOWTO (R_CKCORE_GNU_VTENTRY, /* type */
  159. 0, /* rightshift */
  160. 2, /* size */
  161. 0, /* bitsize */
  162. false, /* pc_relative */
  163. 0, /* bitpos */
  164. complain_overflow_dont, /* complain_on_overflow */
  165. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  166. "R_CKCORE_GNU_VTENTRY", /* name */
  167. false, /* partial_inplace */
  168. 0x0, /* src_mask */
  169. 0x0, /* dst_mask */
  170. false), /* pcrel_offset */
  171. /* 8: GNU extension to record C++ vtable hierarchy. */
  172. HOWTO (R_CKCORE_GNU_VTINHERIT, /* type */
  173. 0, /* rightshift */
  174. 2, /* size */
  175. 0, /* bitsize */
  176. false, /* pc_relative */
  177. 0, /* bitpos */
  178. complain_overflow_dont, /* complain_on_overflow */
  179. NULL, /* special_function */
  180. "R_CKCORE_GNU_VTINHERIT", /* name */
  181. false, /* partial_inplace */
  182. 0x0, /* src_mask */
  183. 0x0, /* dst_mask */
  184. false), /* pcrel_offset */
  185. /* 9. */
  186. HOWTO (R_CKCORE_RELATIVE, /* type */
  187. 0, /* rightshift */
  188. 2, /* size */
  189. 32, /* bitsize */
  190. false, /* pc_relative */
  191. 0, /* bitpos */
  192. complain_overflow_signed, /* complain_on_overflow */
  193. bfd_elf_generic_reloc, /* special_function */
  194. "R_CKCORE_RELATIVE", /* name */
  195. true, /* partial_inplace */
  196. 0x0, /* src_mask */
  197. 0xffffffff, /* dst_mask */
  198. false), /* pcrel_offset */
  199. /* 10: None. */
  200. /* FIXME: It is a bug that copy relocations are not implemented. */
  201. HOWTO (R_CKCORE_COPY, /* type */
  202. 0, /* rightshift */
  203. 2, /* size */
  204. 32, /* bitsize */
  205. false, /* pc_relative */
  206. 0, /* bitpos */
  207. complain_overflow_bitfield, /* complain_on_overflow */
  208. bfd_elf_generic_reloc, /* special_function */
  209. "R_CKCORE_COPY", /* name */
  210. true, /* partial_inplace */
  211. 0xffffffff, /* src_mask */
  212. 0xffffffff, /* dst_mask */
  213. false), /* pcrel_offset */
  214. /* 11: None. */
  215. HOWTO (R_CKCORE_GLOB_DAT,0,0,0,0,0,0,0,"R_CKCORE_GLOB_DAT",0,0,0,0),
  216. /* 12: None. */
  217. HOWTO (R_CKCORE_JUMP_SLOT,0,0,0,0,0,0,0,"R_CKCORE_JUMP_SLOT",0,0,0,0),
  218. /* 13. */
  219. HOWTO (R_CKCORE_GOTOFF, /* type */
  220. 0, /* rightshift */
  221. 2, /* size */
  222. 32, /* bitsize */
  223. false, /* pc_relative */
  224. 0, /* bitpos */
  225. complain_overflow_dont, /* complain_on_overflow */
  226. bfd_elf_generic_reloc, /* special_function */
  227. "R_CKCORE_GOTOFF", /* name */
  228. true, /* partial_inplace */
  229. 0x0, /* src_mask */
  230. 0xffffffffl, /* dst_mask */
  231. false), /* pcrel_offset */
  232. /* 14. */
  233. HOWTO (R_CKCORE_GOTPC, /* type */
  234. 0, /* rightshift */
  235. 2, /* size */
  236. 32, /* bitsize */
  237. true, /* pc_relative */
  238. 0, /* bitpos */
  239. complain_overflow_dont, /* complain_on_overflow */
  240. bfd_elf_generic_reloc, /* special_function */
  241. "R_CKCORE_GOTPC", /* name */
  242. true, /* partial_inplace */
  243. 0x0, /* src_mask */
  244. 0xffffffff, /* dst_mask */
  245. false), /* pcrel_offset */
  246. /* 15. */
  247. HOWTO (R_CKCORE_GOT32, /* type */
  248. 0, /* rightshift */
  249. 2, /* size */
  250. 32, /* bitsize */
  251. false, /* pc_relative */
  252. 0, /* bitpos */
  253. complain_overflow_dont, /* complain_on_overflow */
  254. bfd_elf_generic_reloc, /* special_function */
  255. "R_CKCORE_GOT32", /* name */
  256. true, /* partial_inplace */
  257. 0x0, /* src_mask */
  258. 0xffffffff, /* dst_mask */
  259. true), /* pcrel_offset */
  260. /* 16. */
  261. HOWTO (R_CKCORE_PLT32, /* type */
  262. 0, /* rightshift */
  263. 2, /* size */
  264. 32, /* bitsize */
  265. false, /* pc_relative */
  266. 0, /* bitpos */
  267. complain_overflow_dont, /* complain_on_overflow */
  268. bfd_elf_generic_reloc, /* special_function */
  269. "R_CKCORE_PLT32", /* name */
  270. true, /* partial_inplace */
  271. 0x0, /* src_mask */
  272. 0xffffffff, /* dst_mask */
  273. true), /* pcrel_offset */
  274. /* 17: None. */
  275. HOWTO (R_CKCORE_ADDRGOT,0,0,0,0,0,0,0,"R_CKCORE_ADDRGOT",0,0,0,0),
  276. /* 18: None. */
  277. HOWTO (R_CKCORE_ADDRPLT,0,0,0,0,0,0,0,"R_CKCORE_ADDRPLT",0,0,0,0),
  278. /* 19: Only for csky v2. */
  279. HOWTO (R_CKCORE_PCREL_IMM26BY2, /* type */
  280. 1, /* rightshift */
  281. 2, /* size */
  282. 26, /* bitsize */
  283. true, /* pc_relative */
  284. 0, /* bitpos */
  285. complain_overflow_signed, /* complain_on_overflow */
  286. bfd_elf_generic_reloc, /* special_function */
  287. "R_CKCORE_PCREL_IMM26BY2", /* name */
  288. false, /* partial_inplace */
  289. 0x0, /* src_mask */
  290. 0x3ffffff, /* dst_mask */
  291. true), /* pcrel_offset */
  292. /* 20: Only for csky v2. */
  293. HOWTO (R_CKCORE_PCREL_IMM16BY2, /* type */
  294. 1, /* rightshift */
  295. 2, /* size */
  296. 16, /* bitsize */
  297. true, /* pc_relative */
  298. 0, /* bitpos */
  299. complain_overflow_signed, /* complain_on_overflow */
  300. bfd_elf_generic_reloc, /* special_function */
  301. "R_CKCORE_PCREL_IMM16BY2", /* name */
  302. false, /* partial_inplace */
  303. 0x0, /* src_mask */
  304. 0xffff, /* dst_mask */
  305. true), /* pcrel_offset */
  306. /* 21: Only for csky v2. */
  307. HOWTO (R_CKCORE_PCREL_IMM16BY4, /* type */
  308. 2, /* rightshift */
  309. 2, /* size */
  310. 16, /* bitsize */
  311. true, /* pc_relative */
  312. 0, /* bitpos */
  313. complain_overflow_bitfield, /* complain_on_overflow */
  314. bfd_elf_generic_reloc, /* special_function */
  315. "R_CKCORE_PCREL_IMM16BY4", /* name */
  316. false, /* partial_inplace */
  317. 0xffff0000, /* src_mask */
  318. 0xffff, /* dst_mask */
  319. true), /* pcrel_offset */
  320. /* 22: Only for csky v2. */
  321. HOWTO (R_CKCORE_PCREL_IMM10BY2, /* type */
  322. 1, /* rightshift */
  323. 1, /* size */
  324. 10, /* bitsize */
  325. true, /* pc_relative */
  326. 0, /* bitpos */
  327. complain_overflow_signed, /* complain_on_overflow */
  328. bfd_elf_generic_reloc, /* special_function */
  329. "R_CKCORE_PCREL_IMM10BY2", /* name */
  330. false, /* partial_inplace */
  331. 0x0, /* src_mask */
  332. 0x3ff, /* dst_mask */
  333. true), /* pcrel_offset */
  334. /* 23: Only for csky v2. */
  335. HOWTO (R_CKCORE_PCREL_IMM10BY4, /* type */
  336. 2, /* rightshift */
  337. 2, /* size */
  338. 10, /* bitsize */
  339. true, /* pc_relative */
  340. 0, /* bitpos */
  341. complain_overflow_bitfield, /* complain_on_overflow */
  342. bfd_elf_generic_reloc, /* special_function */
  343. "R_CKCORE_PCREL_IMM10BY4", /* name */
  344. false, /* partial_inplace */
  345. 0x0, /* src_mask */
  346. 0x3ff, /* dst_mask */
  347. true), /* pcrel_offset */
  348. /* 24: Only for csky v2. */
  349. HOWTO (R_CKCORE_ADDR_HI16, /* type */
  350. 16, /* rightshift */
  351. 2, /* size */
  352. 16, /* bitsize */
  353. false, /* pc_relative */
  354. 0, /* bitpos */
  355. complain_overflow_dont, /* complain_on_overflow */
  356. bfd_elf_generic_reloc, /* special_function */
  357. "R_CKCORE_ADDR_HI16", /* name */
  358. false, /* partial_inplace */
  359. 0x0, /* src_mask */
  360. 0xffff, /* dst_mask */
  361. false), /* pcrel_offset */
  362. /* 25. */
  363. HOWTO (R_CKCORE_ADDR_LO16, /* type */
  364. 0, /* rightshift */
  365. 2, /* size */
  366. 16, /* bitsize */
  367. false, /* pc_relative */
  368. 0, /* bitpos */
  369. complain_overflow_dont, /* complain_on_overflow */
  370. bfd_elf_generic_reloc, /* special_function */
  371. "R_CKCORE_ADDR_LO16", /* name */
  372. false, /* partial_inplace */
  373. 0x0, /* src_mask */
  374. 0xffff, /* dst_mask */
  375. false), /* pcrel_offset */
  376. /* 26. */
  377. HOWTO (R_CKCORE_GOTPC_HI16, /* type */
  378. 16, /* rightshift */
  379. 2, /* size */
  380. 16, /* bitsize */
  381. true, /* pc_relative */
  382. 0, /* bitpos */
  383. complain_overflow_dont, /* complain_on_overflow */
  384. bfd_elf_generic_reloc, /* special_function */
  385. "R_CKCORE_GOTPC_HI16", /* name */
  386. false, /* partial_inplace */
  387. 0x0, /* src_mask */
  388. 0xffff, /* dst_mask */
  389. false), /* pcrel_offset */
  390. /* 27. */
  391. HOWTO (R_CKCORE_GOTPC_LO16, /* type */
  392. 0, /* rightshift */
  393. 2, /* size */
  394. 16, /* bitsize */
  395. true, /* pc_relative */
  396. 0, /* bitpos */
  397. complain_overflow_dont, /* complain_on_overflow */
  398. bfd_elf_generic_reloc, /* special_function */
  399. "R_CKCORE_GOTPC_LO16", /* name */
  400. false, /* partial_inplace */
  401. 0x0, /* src_mask */
  402. 0xffff, /* dst_mask */
  403. false), /* pcrel_offset */
  404. /* 28. */
  405. HOWTO (R_CKCORE_GOTOFF_HI16, /* type */
  406. 16, /* rightshift */
  407. 2, /* size */
  408. 16, /* bitsize */
  409. false, /* pc_relative */
  410. 0, /* bitpos */
  411. complain_overflow_dont, /* complain_on_overflow */
  412. bfd_elf_generic_reloc, /* special_function */
  413. "R_CKCORE_GOTOFF_HI16", /* name */
  414. false, /* partial_inplace */
  415. 0x0, /* src_mask */
  416. 0xffff, /* dst_mask */
  417. false), /* pcrel_offset */
  418. /* 29. */
  419. HOWTO (R_CKCORE_GOTOFF_LO16, /* type */
  420. 0, /* rightshift */
  421. 2, /* size */
  422. 16, /* bitsize */
  423. false, /* pc_relative */
  424. 0, /* bitpos */
  425. complain_overflow_dont, /* complain_on_overflow */
  426. bfd_elf_generic_reloc, /* special_function */
  427. "R_CKCORE_GOTOFF_LO16", /* name */
  428. false, /* partial_inplace */
  429. 0x0, /* src_mask */
  430. 0xffff, /* dst_mask */
  431. false), /* pcrel_offset */
  432. /* 30. */
  433. HOWTO (R_CKCORE_GOT12, /* type */
  434. 2, /* rightshift */
  435. 2, /* size */
  436. 12, /* bitsize */
  437. false, /* pc_relative */
  438. 0, /* bitpos */
  439. complain_overflow_bitfield, /* complain_on_overflow */
  440. bfd_elf_generic_reloc, /* special_function */
  441. "R_CKCORE_GOT12", /* name */
  442. true, /* partial_inplace */
  443. 0x0, /* src_mask */
  444. 0xfff, /* dst_mask */
  445. false), /* pcrel_offset */
  446. /* 31. */
  447. HOWTO (R_CKCORE_GOT_HI16, /* type */
  448. 16, /* rightshift */
  449. 2, /* size */
  450. 16, /* bitsize */
  451. false, /* pc_relative */
  452. 0, /* bitpos */
  453. complain_overflow_dont, /* complain_on_overflow */
  454. bfd_elf_generic_reloc, /* special_function */
  455. "R_CKCORE_GOT_HI16", /* name */
  456. true, /* partial_inplace */
  457. 0x0, /* src_mask */
  458. 0xffff, /* dst_mask */
  459. false), /* pcrel_offset */
  460. /* 32. */
  461. HOWTO (R_CKCORE_GOT_LO16, /* type */
  462. 0, /* rightshift */
  463. 2, /* size */
  464. 16, /* bitsize */
  465. false, /* pc_relative */
  466. 0, /* bitpos */
  467. complain_overflow_dont, /* complain_on_overflow */
  468. bfd_elf_generic_reloc, /* special_function */
  469. "R_CKCORE_GOT_LO16", /* name */
  470. true, /* partial_inplace */
  471. 0x0, /* src_mask */
  472. 0xffff, /* dst_mask */
  473. false), /* pcrel_offset */
  474. /* 33. */
  475. HOWTO (R_CKCORE_PLT12, /* type */
  476. 2, /* rightshift */
  477. 2, /* size */
  478. 12, /* bitsize */
  479. false, /* pc_relative */
  480. 0, /* bitpos */
  481. complain_overflow_bitfield, /* complain_on_overflow */
  482. bfd_elf_generic_reloc, /* special_function */
  483. "R_CKCORE_PLT12", /* name */
  484. true, /* partial_inplace */
  485. 0x0, /* src_mask */
  486. 0xfff, /* dst_mask */
  487. false), /* pcrel_offset */
  488. /* 34. */
  489. HOWTO (R_CKCORE_PLT_HI16, /* type */
  490. 16, /* rightshift */
  491. 2, /* size */
  492. 16, /* bitsize */
  493. false, /* pc_relative */
  494. 0, /* bitpos */
  495. complain_overflow_dont, /* complain_on_overflow */
  496. bfd_elf_generic_reloc, /* special_function */
  497. "R_CKCORE_PLT_HI16", /* name */
  498. true, /* partial_inplace */
  499. 0x0, /* src_mask */
  500. 0xffff, /* dst_mask */
  501. false), /* pcrel_offset */
  502. /* 35. */
  503. HOWTO (R_CKCORE_PLT_LO16, /* type */
  504. 0, /* rightshift */
  505. 2, /* size */
  506. 16, /* bitsize */
  507. false, /* pc_relative */
  508. 0, /* bitpos */
  509. complain_overflow_dont, /* complain_on_overflow */
  510. bfd_elf_generic_reloc, /* special_function */
  511. "R_CKCORE_PLT_LO16", /* name */
  512. true, /* partial_inplace */
  513. 0x0, /* src_mask */
  514. 0xffff, /* dst_mask */
  515. false), /* pcrel_offset */
  516. /* 36: None. */
  517. HOWTO (R_CKCORE_ADDRGOT_HI16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
  518. /* 37: None. */
  519. HOWTO (R_CKCORE_ADDRGOT_LO16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
  520. /* 38: None. */
  521. HOWTO (R_CKCORE_ADDRPLT_HI16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
  522. /* 39: None. */
  523. HOWTO (R_CKCORE_ADDRPLT_LO16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
  524. /* 40. */
  525. HOWTO (R_CKCORE_PCREL_JSR_IMM26BY2, /* type */
  526. 1, /* rightshift */
  527. 2, /* size */
  528. 26, /* bitsize */
  529. true, /* pc_relative */
  530. 0, /* bitpos */
  531. complain_overflow_signed, /* complain_on_overflow */
  532. bfd_elf_generic_reloc, /* special_function */
  533. "R_CKCORE_PCREL_JSR_IMM26BY2", /* name */
  534. false, /* partial_inplace */
  535. 0x0, /* src_mask */
  536. 0x3ffffff, /* dst_mask */
  537. true), /* pcrel_offset */
  538. /* 41. */
  539. HOWTO (R_CKCORE_TOFFSET_LO16, /* type */
  540. 0, /* rightshift */
  541. 2, /* size */
  542. 16, /* bitsize */
  543. false, /* pc_relative */
  544. 0, /* bitpos */
  545. complain_overflow_unsigned, /* complain_on_overflow */
  546. NULL, /* special_function */
  547. "R_CKCORE_TOFFSET_LO16", /* name */
  548. false, /* partial_inplace */
  549. 0x0, /* src_mask */
  550. 0xffff, /* dst_mask */
  551. false), /* pcrel_offset */
  552. /* 42. */
  553. HOWTO (R_CKCORE_DOFFSET_LO16, /* type */
  554. 0, /* rightshift */
  555. 2, /* size */
  556. 16, /* bitsize */
  557. false, /* pc_relative */
  558. 0, /* bitpos */
  559. complain_overflow_unsigned, /* complain_on_overflow */
  560. NULL, /* special_function */
  561. "R_CKCORE_DOFFSET_LO16", /* name */
  562. false, /* partial_inplace */
  563. 0x0, /* src_mask */
  564. 0xffff, /* dst_mask */
  565. false), /* pcrel_offset */
  566. /* 43. */
  567. HOWTO (R_CKCORE_PCREL_IMM18BY2, /* type */
  568. 1, /* rightshift */
  569. 2, /* size */
  570. 18, /* bitsize */
  571. true, /* pc_relative */
  572. 0, /* bitpos */
  573. complain_overflow_signed, /* complain_on_overflow */
  574. bfd_elf_generic_reloc, /* special_function */
  575. "R_CKCORE_PCREL_IMM18BY2", /* name */
  576. false, /* partial_inplace */
  577. 0x0, /* src_mask */
  578. 0x3ffff, /* dst_mask */
  579. true), /* pcrel_offset */
  580. /* 44. */
  581. HOWTO (R_CKCORE_DOFFSET_IMM18, /* type */
  582. 0, /* rightshift */
  583. 2, /* size */
  584. 18, /* bitsize */
  585. false, /* pc_relative */
  586. 0, /* bitpos */
  587. complain_overflow_unsigned, /* complain_on_overflow */
  588. NULL, /* special_function */
  589. "R_CKCORE_DOFFSET_IMM18", /* name */
  590. false, /* partial_inplace */
  591. 0x0, /* src_mask */
  592. 0x3ffff, /* dst_mask */
  593. false), /* pcrel_offset */
  594. /* 45. */
  595. HOWTO (R_CKCORE_DOFFSET_IMM18BY2, /* type */
  596. 1, /* rightshift */
  597. 2, /* size */
  598. 18, /* bitsize */
  599. false, /* pc_relative */
  600. 0, /* bitpos */
  601. complain_overflow_unsigned, /* complain_on_overflow */
  602. NULL, /* special_function */
  603. "R_CKCORE_DOFFSET_IMM18BY2", /* name */
  604. false, /* partial_inplace */
  605. 0x0, /* src_mask */
  606. 0x3ffff, /* dst_mask */
  607. false), /* pcrel_offset */
  608. /* 46. */
  609. HOWTO (R_CKCORE_DOFFSET_IMM18BY4, /* type */
  610. 2, /* rightshift */
  611. 2, /* size */
  612. 18, /* bitsize */
  613. false, /* pc_relative */
  614. 0, /* bitpos */
  615. complain_overflow_unsigned, /* complain_on_overflow */
  616. NULL, /* special_function */
  617. "R_CKCORE_DOFFSET_IMM18BY4", /* name */
  618. false, /* partial_inplace */
  619. 0x0, /* src_mask */
  620. 0x3ffff, /* dst_mask */
  621. false), /* pcrel_offset */
  622. /* 47. */
  623. HOWTO (R_CKCORE_GOTOFF_IMM18, /* type */
  624. 0, /* rightshift */
  625. 2, /* size */
  626. 18, /* bitsize */
  627. false, /* pc_relative */
  628. 0, /* bitpos */
  629. complain_overflow_bitfield, /* complain_on_overflow */
  630. bfd_elf_generic_reloc, /* special_function */
  631. "R_CKCORE_GOTOFF_IMM18", /* name */
  632. true, /* partial_inplace */
  633. 0xfffc, /* src_mask */
  634. 0x3ffff, /* dst_mask */
  635. false), /* pcrel_offset */
  636. /* 48. */
  637. HOWTO (R_CKCORE_GOT_IMM18BY4, /* type */
  638. 2, /* rightshift */
  639. 2, /* size */
  640. 18, /* bitsize */
  641. false, /* pc_relative */
  642. 0, /* bitpos */
  643. complain_overflow_bitfield, /* complain_on_overflow */
  644. bfd_elf_generic_reloc, /* special_function */
  645. "R_CKCORE_GOT_IMM18BY4", /* name */
  646. true, /* partial_inplace */
  647. 0xfffc, /* src_mask */
  648. 0x3ffff, /* dst_mask */
  649. false), /* pcrel_offset */
  650. /* 49. */
  651. HOWTO (R_CKCORE_PLT_IMM18BY4, /* type */
  652. 2, /* rightshift */
  653. 2, /* size */
  654. 18, /* bitsize */
  655. false, /* pc_relative */
  656. 0, /* bitpos */
  657. complain_overflow_bitfield, /* complain_on_overflow */
  658. bfd_elf_generic_reloc, /* special_function */
  659. "R_CKCORE_PLT_IMM18BY4", /* name */
  660. true, /* partial_inplace */
  661. 0xfffc, /* src_mask */
  662. 0x3ffff, /* dst_mask */
  663. true), /* pcrel_offset */
  664. /* 50: for lrw16. */
  665. HOWTO (R_CKCORE_PCREL_IMM7BY4, /* type */
  666. 2, /* rightshift */
  667. 1, /* size */
  668. 7, /* bitsize */
  669. true, /* pc_relative */
  670. 0, /* bitpos */
  671. complain_overflow_bitfield, /* complain_on_overflow */
  672. bfd_elf_generic_reloc, /* special_function */
  673. "R_CKCORE_PCREL_IMM7BY4", /* name */
  674. false, /* partial_inplace */
  675. 0xec1f, /* src_mask */
  676. 0x31f, /* dst_mask */
  677. true), /* pcrel_offset */
  678. /* 51: for static nptl. */
  679. HOWTO (R_CKCORE_TLS_LE32, /* type */
  680. 0, /* rightshift */
  681. 2, /* size */
  682. 32, /* bitsize */
  683. false, /* pc_relative */
  684. 0, /* bitpos */
  685. complain_overflow_dont, /* complain_on_overflow */
  686. bfd_elf_generic_reloc, /* special_function */
  687. "R_CKCORE_TLS_LE32", /* name */
  688. false, /* partial_inplace */
  689. 0x0, /* src_mask */
  690. 0xffffffff, /* dst_mask */
  691. true), /* pcrel_offset */
  692. /* 52: for static nptl. */
  693. HOWTO (R_CKCORE_TLS_IE32, /* type */
  694. 0, /* rightshift */
  695. 2, /* size */
  696. 32, /* bitsize */
  697. false, /* pc_relative */
  698. 0, /* bitpos */
  699. complain_overflow_dont, /* complain_on_overflow */
  700. bfd_elf_generic_reloc, /* special_function */
  701. "R_CKCORE_TLS_IE32", /* name */
  702. false, /* partial_inplace */
  703. 0x0, /* src_mask */
  704. 0xffffffff, /* dst_mask */
  705. true), /* pcrel_offset */
  706. /* 53: for pic nptl. */
  707. HOWTO (R_CKCORE_TLS_GD32, /* type */
  708. 0, /* rightshift */
  709. 2, /* size */
  710. 32, /* bitsize */
  711. false, /* pc_relative */
  712. 0, /* bitpos */
  713. complain_overflow_dont, /* complain_on_overflow */
  714. bfd_elf_generic_reloc, /* special_function */
  715. "R_CKCORE_TLS_GD32", /* name */
  716. false, /* partial_inplace */
  717. 0x0, /* src_mask */
  718. 0xffffffff, /* dst_mask */
  719. true), /* pcrel_offset */
  720. /* 54: for pic nptl. */
  721. HOWTO (R_CKCORE_TLS_LDM32, /* type */
  722. 0, /* rightshift */
  723. 2, /* size */
  724. 32, /* bitsize */
  725. false, /* pc_relative */
  726. 0, /* bitpos */
  727. complain_overflow_dont, /* complain_on_overflow */
  728. bfd_elf_generic_reloc, /* special_function */
  729. "R_CKCORE_TLS_LDM32", /* name */
  730. false, /* partial_inplace */
  731. 0x0, /* src_mask */
  732. 0xffffffff, /* dst_mask */
  733. true), /* pcrel_offset */
  734. /* 55: for pic nptl. */
  735. HOWTO (R_CKCORE_TLS_LDO32, /* type */
  736. 0, /* rightshift */
  737. 2, /* size */
  738. 32, /* bitsize */
  739. false, /* pc_relative */
  740. 0, /* bitpos */
  741. complain_overflow_dont, /* complain_on_overflow */
  742. bfd_elf_generic_reloc, /* special_function */
  743. "R_CKCORE_TLS_LDO32", /* name */
  744. false, /* partial_inplace */
  745. 0x0, /* src_mask */
  746. 0xffffffff, /* dst_mask */
  747. true), /* pcrel_offset */
  748. /* 56: for linker. */
  749. HOWTO (R_CKCORE_TLS_DTPMOD32,0,0,0,0,0,0,0,"R_CKCORE_TLS_DTPMOD32",0,0,0,0),
  750. /* 57: for linker. */
  751. HOWTO (R_CKCORE_TLS_DTPOFF32,0,0,0,0,0,0,0,"R_CKCORE_TLS_DTPOFF32",0,0,0,0),
  752. /* 58: for linker. */
  753. HOWTO (R_CKCORE_TLS_TPOFF32,0,0,0,0,0,0,0,"R_CKCORE_TLS_TPOFF32",0,0,0,0),
  754. /* 59: for ck807f. */
  755. HOWTO (R_CKCORE_PCREL_FLRW_IMM8BY4, /* type */
  756. 2, /* rightshift */
  757. 2, /* size */
  758. 8, /* bitsize */
  759. true, /* pc_relative */
  760. 0, /* bitpos */
  761. complain_overflow_bitfield, /* complain_on_overflow */
  762. bfd_elf_generic_reloc, /* special_function */
  763. "R_CKCORE_PCREL_FLRW_IMM8BY4",/* name */
  764. false, /* partial_inplace */
  765. 0xfe1fff0f, /* src_mask */
  766. 0x1e000f0, /* dst_mask */
  767. true), /* pcrel_offset */
  768. /* 60: for 810 not to generate jsri. */
  769. HOWTO (R_CKCORE_NOJSRI, /* type */
  770. 0, /* rightshift */
  771. 2, /* size */
  772. 32, /* bitsize */
  773. false, /* pc_relative */
  774. 0, /* bitpos */
  775. complain_overflow_dont, /* complain_on_overflow */
  776. bfd_elf_generic_reloc, /* special_function */
  777. "R_CKCORE_NOJSRI", /* name */
  778. false, /* partial_inplace */
  779. 0xffff, /* src_mask */
  780. 0xffff, /* dst_mask */
  781. false), /* pcrel_offset */
  782. /* 61: for callgraph. */
  783. HOWTO (R_CKCORE_CALLGRAPH, /* type */
  784. 0, /* rightshift */
  785. 0, /* size */
  786. 0, /* bitsize */
  787. false, /* pc_relative */
  788. 0, /* bitpos */
  789. complain_overflow_dont, /* complain_on_overflow */
  790. NULL, /* special_function */
  791. "R_CKCORE_CALLGRAPH", /* name */
  792. false, /* partial_inplace */
  793. 0x0, /* src_mask */
  794. 0x0, /* dst_mask */
  795. true), /* pcrel_offset */
  796. /* 62: IRELATIVE*/
  797. HOWTO (R_CKCORE_IRELATIVE,0,0,0,0,0,0,0,"R_CKCORE_IRELATIVE",0,0,0,0),
  798. /* 63: for bloop instruction */
  799. HOWTO (R_CKCORE_PCREL_BLOOP_IMM4BY4, /* type */
  800. 1, /* rightshift */
  801. 2, /* size */
  802. 4, /* bitsize */
  803. 1, /* pc_relative */
  804. 0, /* bitpos */
  805. complain_overflow_signed, /* complain_on_overflow */
  806. bfd_elf_generic_reloc, /* special_function */
  807. "R_CKCORE_PCREL_BLOOP_IMM4BY4", /* name */
  808. false, /* partial_inplace */
  809. 0x0, /* src_mask */
  810. 0xf, /* dst_mask */
  811. true), /* pcrel_offset */
  812. /* 64: for bloop instruction */
  813. HOWTO (R_CKCORE_PCREL_BLOOP_IMM12BY4, /* type */
  814. 1, /* rightshift */
  815. 2, /* size */
  816. 12, /* bitsize */
  817. 1, /* pc_relative */
  818. 0, /* bitpos */
  819. complain_overflow_signed, /* complain_on_overflow */
  820. bfd_elf_generic_reloc, /* special_function */
  821. "R_CKCORE_PCREL_BLOOP_IMM12BY4", /* name */
  822. false, /* partial_inplace */
  823. 0x0, /* src_mask */
  824. 0xfff, /* dst_mask */
  825. true), /* pcrel_offset */
  826. };
  827. /* Whether GOT overflow checking is needed. */
  828. static int check_got_overflow = 0;
  829. /* Whether the target 32 bits is forced so that the high
  830. 16 bits is at the low address. */
  831. static int need_reverse_bits;
  832. /* Used for relaxation. See csky_relocate_contents. */
  833. static bfd_vma read_content_substitute;
  834. /* NOTICE!
  835. The way the following two look-up functions work demands
  836. that BFD_RELOC_CKCORE_xxx are defined contiguously. */
  837. static reloc_howto_type *
  838. csky_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  839. bfd_reloc_code_real_type code)
  840. {
  841. int csky_code = code - BFD_RELOC_CKCORE_NONE;
  842. if (csky_code < 0 || csky_code >= R_CKCORE_MAX)
  843. {
  844. switch (code)
  845. {
  846. case BFD_RELOC_NONE:
  847. csky_code = R_CKCORE_NONE;
  848. break;
  849. case BFD_RELOC_32:
  850. csky_code = R_CKCORE_ADDR32;
  851. break;
  852. case BFD_RELOC_32_PCREL:
  853. csky_code = R_CKCORE_PCREL32;
  854. break;
  855. case BFD_RELOC_VTABLE_INHERIT:
  856. csky_code = R_CKCORE_GNU_VTINHERIT;
  857. break;
  858. case BFD_RELOC_VTABLE_ENTRY:
  859. csky_code = R_CKCORE_GNU_VTENTRY;
  860. break;
  861. case BFD_RELOC_RVA:
  862. csky_code = R_CKCORE_RELATIVE;
  863. break;
  864. default:
  865. return (reloc_howto_type *)NULL;
  866. }
  867. }
  868. /* Note: when adding csky bfd reloc types in bfd-in2.h
  869. and csky elf reloc types in elf/csky.h,
  870. the order of the two reloc type tables should be consistent. */
  871. return &csky_elf_howto_table[csky_code];
  872. }
  873. static reloc_howto_type *
  874. csky_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  875. const char *r_name)
  876. {
  877. unsigned int i;
  878. for (i = 0; i < R_CKCORE_MAX; i++)
  879. if (strcasecmp (csky_elf_howto_table[i].name, r_name) == 0)
  880. return &csky_elf_howto_table[i];
  881. return NULL;
  882. }
  883. static reloc_howto_type *
  884. elf32_csky_howto_from_type (unsigned int r_type)
  885. {
  886. if (r_type < R_CKCORE_MAX)
  887. return &csky_elf_howto_table[r_type];
  888. else
  889. return NULL;
  890. }
  891. static bool
  892. csky_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  893. arelent *cache_ptr,
  894. Elf_Internal_Rela *dst)
  895. {
  896. unsigned int r_type;
  897. r_type = ELF32_R_TYPE (dst->r_info);
  898. cache_ptr->howto = elf32_csky_howto_from_type (r_type);
  899. if (cache_ptr->howto == NULL)
  900. {
  901. /* xgettext:c-format */
  902. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  903. abfd, r_type);
  904. bfd_set_error (bfd_error_bad_value);
  905. return false;
  906. }
  907. return true;
  908. }
  909. /* The Global Offset Table max size. */
  910. #define GOT_MAX_SIZE 0xFFFF8
  911. /* The name of the dynamic interpreter. This is put in the .interp
  912. section. */
  913. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  914. /* The size in bytes of an entry in the procedure linkage table. */
  915. #define PLT_ENTRY_SIZE 12
  916. #define PLT_ENTRY_SIZE_P 16
  917. /* The first entry in a procedure linkage table looks like
  918. this. It is set up so that any shared library function that is
  919. called before the relocation has been set up calls the dynamic
  920. linker first. */
  921. static const bfd_vma csky_elf_plt_entry_v2[PLT_ENTRY_SIZE / 4] =
  922. {
  923. 0xd99c2002, /* ldw r12, (gb, 8) */
  924. 0xea0d0000, /* movi r13,offset */
  925. 0xe8cc0000 /* jmp r12 */
  926. };
  927. static const bfd_vma csky_elf_plt_entry_v1[PLT_ENTRY_SIZE / 2 ] =
  928. {
  929. 0x25f0, /* subi r0, 32 */
  930. 0x9200, /* stw r2, (r0, 0) */
  931. 0x9310, /* stw r3, (r0, 4) */
  932. 0x822e, /* ldw r2, (gb, 8) */
  933. 0x7301, /* lrw r3, #offset */
  934. 0x00c2, /* jmp r2 */
  935. };
  936. /* Branch stub support. */
  937. enum stub_insn_type
  938. {
  939. INSN16,
  940. INSN32,
  941. DATA_TYPE
  942. };
  943. bool use_branch_stub = true;
  944. typedef struct
  945. {
  946. bfd_vma data;
  947. enum stub_insn_type type;
  948. unsigned int r_type;
  949. int reloc_addend;
  950. } insn_sequence;
  951. static const insn_sequence elf32_csky_stub_long_branch[] =
  952. {
  953. {0xea8d0002, INSN32, R_CKCORE_NONE, 0x0}, /* lrw t1,[pc+8] */
  954. {0x7834, INSN16, R_CKCORE_NONE, 0x0}, /* jmp t1 */
  955. {0x6c03, INSN16, R_CKCORE_NONE, 0x0}, /* nop */
  956. {0x0, DATA_TYPE, R_CKCORE_ADDR32, 0x0} /* .long addr */
  957. };
  958. static const insn_sequence elf32_csky_stub_long_branch_jmpi[] =
  959. {
  960. {0xeac00001, INSN32, R_CKCORE_NONE, 0x0}, /* jmpi [pc+4] */
  961. {0x0, DATA_TYPE, R_CKCORE_ADDR32, 0x0} /* .long addr */
  962. };
  963. /* The bsr instruction offset limit. */
  964. #define BSR_MAX_FWD_BRANCH_OFFSET (((1 << 25) - 1) << 1)
  965. #define BSR_MAX_BWD_BRANCH_OFFSET (-(1 << 26))
  966. #define STUB_SUFFIX ".stub"
  967. #define STUB_ENTRY_NAME "__%s_veneer"
  968. /* One entry per long/short branch stub defined above. */
  969. #define DEF_STUBS \
  970. DEF_STUB(long_branch) \
  971. DEF_STUB(long_branch_jmpi)
  972. #define DEF_STUB(x) csky_stub_##x,
  973. enum elf32_csky_stub_type
  974. {
  975. csky_stub_none,
  976. DEF_STUBS
  977. };
  978. #undef DEF_STUB
  979. typedef struct
  980. {
  981. const insn_sequence* template_sequence;
  982. int template_size;
  983. } stub_def;
  984. #define DEF_STUB(x) {elf32_csky_stub_##x, ARRAY_SIZE(elf32_csky_stub_##x)},
  985. static const stub_def stub_definitions[] = {
  986. {NULL, 0},
  987. DEF_STUBS
  988. };
  989. /* The size of the thread control block. */
  990. #define TCB_SIZE 8
  991. struct csky_elf_obj_tdata
  992. {
  993. struct elf_obj_tdata root;
  994. /* tls_type for each local got entry. */
  995. char *local_got_tls_type;
  996. };
  997. #define csky_elf_local_got_tls_type(bfd) \
  998. (csky_elf_tdata (bfd)->local_got_tls_type)
  999. #define csky_elf_tdata(bfd) \
  1000. ((struct csky_elf_obj_tdata *) (bfd)->tdata.any)
  1001. struct elf32_csky_stub_hash_entry
  1002. {
  1003. /* Base hash table entry structure. */
  1004. struct bfd_hash_entry root;
  1005. /* The stub section. */
  1006. asection *stub_sec;
  1007. /* Offset within stub_sec of the beginning of this stub. */
  1008. bfd_vma stub_offset;
  1009. /* Given the symbol's value and its section we can determine its final
  1010. value when building the stubs (so the stub knows where to jump). */
  1011. bfd_vma target_value;
  1012. asection *target_section;
  1013. /* Offset to apply to relocation referencing target_value. */
  1014. bfd_vma target_addend;
  1015. /* The stub type. */
  1016. enum elf32_csky_stub_type stub_type;
  1017. /* Its encoding size in bytes. */
  1018. int stub_size;
  1019. /* Its template. */
  1020. const insn_sequence *stub_template;
  1021. /* The size of the template (number of entries). */
  1022. int stub_template_size;
  1023. /* The symbol table entry, if any, that this was derived from. */
  1024. struct csky_elf_link_hash_entry *h;
  1025. /* Destination symbol type. */
  1026. unsigned char st_type;
  1027. /* Where this stub is being called from, or, in the case of combined
  1028. stub sections, the first input section in the group. */
  1029. asection *id_sec;
  1030. /* The name for the local symbol at the start of this stub. The
  1031. stub name in the hash table has to be unique; this does not, so
  1032. it can be friendlier. */
  1033. char *output_name;
  1034. };
  1035. #define csky_stub_hash_lookup(table, string, create, copy) \
  1036. ((struct elf32_csky_stub_hash_entry *) \
  1037. bfd_hash_lookup ((table), (string), (create), (copy)))
  1038. /* C-SKY ELF linker hash entry. */
  1039. struct csky_elf_link_hash_entry
  1040. {
  1041. struct elf_link_hash_entry elf;
  1042. int plt_refcount;
  1043. /* For sub jsri2bsr relocs count. */
  1044. int jsri2bsr_refcount;
  1045. #define GOT_UNKNOWN 0
  1046. #define GOT_NORMAL 1
  1047. #define GOT_TLS_GD 2
  1048. #define GOT_TLS_IE 4
  1049. unsigned char tls_type;
  1050. /* A pointer to the most recently used stub hash entry against this
  1051. symbol. */
  1052. struct elf32_csky_stub_hash_entry *stub_cache;
  1053. };
  1054. /* Traverse an C-SKY ELF linker hash table. */
  1055. #define csky_elf_link_hash_traverse(table, func, info) \
  1056. (elf_link_hash_traverse \
  1057. (&(table)->root, \
  1058. (bool (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
  1059. (info)))
  1060. /* Get the C-SKY ELF linker hash table from a link_info structure. */
  1061. #define csky_elf_hash_table(p) \
  1062. ((is_elf_hash_table ((p)->hash) \
  1063. && elf_hash_table_id (elf_hash_table (p)) == CSKY_ELF_DATA) \
  1064. ? (struct csky_elf_link_hash_table *) (p)->hash : NULL)
  1065. #define csky_elf_hash_entry(ent) ((struct csky_elf_link_hash_entry*)(ent))
  1066. /* Array to keep track of which stub sections have been created, and
  1067. information on stub grouping. */
  1068. struct map_stub
  1069. {
  1070. /* This is the section to which stubs in the group will be
  1071. attached. */
  1072. asection *link_sec;
  1073. /* The stub section. */
  1074. asection *stub_sec;
  1075. };
  1076. /* C-SKY ELF linker hash table. */
  1077. struct csky_elf_link_hash_table
  1078. {
  1079. struct elf_link_hash_table elf;
  1080. /* Data for R_CKCORE_TLS_LDM32 relocations. */
  1081. union
  1082. {
  1083. bfd_signed_vma refcount;
  1084. bfd_vma offset;
  1085. } tls_ldm_got;
  1086. /* The stub hash table. */
  1087. struct bfd_hash_table stub_hash_table;
  1088. /* Linker stub bfd. */
  1089. bfd *stub_bfd;
  1090. /* Linker call-backs. */
  1091. asection * (*add_stub_section) (const char *, asection *);
  1092. void (*layout_sections_again) (void);
  1093. /* Array to keep track of which stub sections have been created, and
  1094. * information on stub grouping. */
  1095. struct map_stub *stub_group;
  1096. /* Number of elements in stub_group. */
  1097. unsigned int top_id;
  1098. /* Assorted information used by elf32_csky_size_stubs. */
  1099. unsigned int bfd_count;
  1100. unsigned int top_index;
  1101. asection **input_list;
  1102. };
  1103. /* We can't change vectors in the bfd target which will apply to
  1104. data sections, however we only do this to the text sections. */
  1105. static bfd_vma
  1106. csky_get_insn_32 (bfd *input_bfd,
  1107. bfd_byte *location)
  1108. {
  1109. if (bfd_big_endian (input_bfd))
  1110. return bfd_get_32 (input_bfd, location);
  1111. else
  1112. return (bfd_get_16 (input_bfd, location) << 16
  1113. | bfd_get_16 (input_bfd, location + 2));
  1114. }
  1115. static void
  1116. csky_put_insn_32 (bfd *input_bfd,
  1117. bfd_vma x,
  1118. bfd_byte *location)
  1119. {
  1120. if (bfd_big_endian (input_bfd))
  1121. bfd_put_32 (input_bfd, x, location);
  1122. else
  1123. {
  1124. bfd_put_16 (input_bfd, x >> 16, location);
  1125. bfd_put_16 (input_bfd, x & 0xffff, location + 2);
  1126. }
  1127. }
  1128. /* Find or create a stub section. Returns a pointer to the stub section, and
  1129. the section to which the stub section will be attached (in *LINK_SEC_P).
  1130. LINK_SEC_P may be NULL. */
  1131. static asection *
  1132. elf32_csky_create_or_find_stub_sec (asection **link_sec_p, asection *section,
  1133. struct csky_elf_link_hash_table *htab)
  1134. {
  1135. asection *link_sec;
  1136. asection *stub_sec;
  1137. link_sec = htab->stub_group[section->id].link_sec;
  1138. stub_sec = htab->stub_group[section->id].stub_sec;
  1139. if (stub_sec == NULL)
  1140. {
  1141. stub_sec = htab->stub_group[link_sec->id].stub_sec;
  1142. if (stub_sec == NULL)
  1143. {
  1144. size_t namelen;
  1145. bfd_size_type len;
  1146. char *s_name;
  1147. namelen = strlen (link_sec->name);
  1148. len = namelen + sizeof (STUB_SUFFIX);
  1149. s_name = bfd_alloc (htab->stub_bfd, len);
  1150. if (s_name == NULL)
  1151. return NULL;
  1152. memcpy (s_name, link_sec->name, namelen);
  1153. memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
  1154. stub_sec = (*htab->add_stub_section) (s_name, link_sec);
  1155. if (stub_sec == NULL)
  1156. return NULL;
  1157. htab->stub_group[link_sec->id].stub_sec = stub_sec;
  1158. }
  1159. htab->stub_group[section->id].stub_sec = stub_sec;
  1160. }
  1161. if (link_sec_p)
  1162. *link_sec_p = link_sec;
  1163. return stub_sec;
  1164. }
  1165. /* Build a name for an entry in the stub hash table. */
  1166. static char *
  1167. elf32_csky_stub_name (const asection *input_section,
  1168. const asection *sym_sec,
  1169. const struct csky_elf_link_hash_entry *hash,
  1170. const Elf_Internal_Rela *rel)
  1171. {
  1172. char *stub_name;
  1173. bfd_size_type len;
  1174. if (hash)
  1175. {
  1176. len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
  1177. stub_name = bfd_malloc (len);
  1178. if (stub_name != NULL)
  1179. sprintf (stub_name, "%08x_%s+%x",
  1180. input_section->id & 0xffffffff,
  1181. hash->elf.root.root.string,
  1182. (int) rel->r_addend & 0xffffffff);
  1183. }
  1184. else
  1185. {
  1186. len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
  1187. stub_name = bfd_malloc (len);
  1188. if (stub_name != NULL)
  1189. sprintf (stub_name, "%08x_%x:%x+%x",
  1190. input_section->id & 0xffffffff,
  1191. sym_sec->id & 0xffffffff,
  1192. (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
  1193. (int) rel->r_addend & 0xffffffff);
  1194. }
  1195. return stub_name;
  1196. }
  1197. /* Determine the type of stub needed, if any, for a call. */
  1198. static enum elf32_csky_stub_type
  1199. csky_type_of_stub (struct bfd_link_info *info,
  1200. asection *input_sec,
  1201. const Elf_Internal_Rela *rel,
  1202. unsigned char st_type,
  1203. struct csky_elf_link_hash_entry *hash,
  1204. bfd_vma destination,
  1205. asection *sym_sec ATTRIBUTE_UNUSED,
  1206. bfd *input_bfd ATTRIBUTE_UNUSED,
  1207. const char *name ATTRIBUTE_UNUSED)
  1208. {
  1209. bfd_vma location;
  1210. bfd_signed_vma branch_offset;
  1211. unsigned int r_type;
  1212. enum elf32_csky_stub_type stub_type = csky_stub_none;
  1213. struct elf_link_hash_entry * h = &hash->elf;
  1214. /* We don't know the actual type of destination in case it is of
  1215. type STT_SECTION: give up. */
  1216. if (st_type == STT_SECTION)
  1217. return stub_type;
  1218. location = (input_sec->output_offset
  1219. + input_sec->output_section->vma
  1220. + rel->r_offset);
  1221. branch_offset = (bfd_signed_vma)(destination - location);
  1222. r_type = ELF32_R_TYPE (rel->r_info);
  1223. if (r_type == R_CKCORE_PCREL_IMM26BY2
  1224. && ((h != NULL
  1225. && ((h->def_dynamic && !h->def_regular)
  1226. || (bfd_link_pic (info)
  1227. && h->root.type == bfd_link_hash_defweak)))
  1228. || branch_offset > BSR_MAX_FWD_BRANCH_OFFSET
  1229. || branch_offset < BSR_MAX_BWD_BRANCH_OFFSET))
  1230. {
  1231. if (bfd_csky_arch (info->output_bfd) == CSKY_ARCH_810
  1232. || bfd_csky_arch (info->output_bfd) == CSKY_ARCH_807)
  1233. stub_type = csky_stub_long_branch_jmpi;
  1234. else
  1235. stub_type = csky_stub_long_branch;
  1236. }
  1237. return stub_type;
  1238. }
  1239. /* Create an entry in an C-SKY ELF linker hash table. */
  1240. static struct bfd_hash_entry *
  1241. csky_elf_link_hash_newfunc (struct bfd_hash_entry * entry,
  1242. struct bfd_hash_table * table,
  1243. const char * string)
  1244. {
  1245. struct csky_elf_link_hash_entry * ret =
  1246. (struct csky_elf_link_hash_entry *) entry;
  1247. /* Allocate the structure if it has not already been allocated by a
  1248. subclass. */
  1249. if (ret == NULL)
  1250. {
  1251. ret = (struct csky_elf_link_hash_entry *)
  1252. bfd_hash_allocate (table,
  1253. sizeof (struct csky_elf_link_hash_entry));
  1254. if (ret == NULL)
  1255. return (struct bfd_hash_entry *) ret;
  1256. }
  1257. /* Call the allocation method of the superclass. */
  1258. ret = ((struct csky_elf_link_hash_entry *)
  1259. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *)ret,
  1260. table, string));
  1261. if (ret != NULL)
  1262. {
  1263. struct csky_elf_link_hash_entry *eh;
  1264. eh = (struct csky_elf_link_hash_entry *) ret;
  1265. eh->plt_refcount = 0;
  1266. eh->jsri2bsr_refcount = 0;
  1267. eh->tls_type = GOT_NORMAL;
  1268. ret->stub_cache = NULL;
  1269. }
  1270. return (struct bfd_hash_entry *) ret;
  1271. }
  1272. /* Initialize an entry in the stub hash table. */
  1273. static struct bfd_hash_entry *
  1274. stub_hash_newfunc (struct bfd_hash_entry *entry,
  1275. struct bfd_hash_table *table,
  1276. const char *string)
  1277. {
  1278. /* Allocate the structure if it has not already been allocated by a
  1279. subclass. */
  1280. if (entry == NULL)
  1281. {
  1282. entry = ((struct bfd_hash_entry *)
  1283. bfd_hash_allocate (table,
  1284. sizeof (struct elf32_csky_stub_hash_entry)));
  1285. if (entry == NULL)
  1286. return entry;
  1287. }
  1288. /* Call the allocation method of the superclass. */
  1289. entry = bfd_hash_newfunc (entry, table, string);
  1290. if (entry != NULL)
  1291. {
  1292. struct elf32_csky_stub_hash_entry *eh;
  1293. /* Initialize the local fields. */
  1294. eh = (struct elf32_csky_stub_hash_entry *) entry;
  1295. eh->stub_sec = NULL;
  1296. eh->stub_offset = 0;
  1297. eh->target_value = 0;
  1298. eh->target_section = NULL;
  1299. eh->target_addend = 0;
  1300. eh->stub_type = csky_stub_none;
  1301. eh->stub_size = 0;
  1302. eh->stub_template = NULL;
  1303. eh->stub_template_size = -1;
  1304. eh->h = NULL;
  1305. eh->id_sec = NULL;
  1306. eh->output_name = NULL;
  1307. }
  1308. return entry;
  1309. }
  1310. /* Free the derived linker hash table. */
  1311. static void
  1312. csky_elf_link_hash_table_free (bfd *obfd)
  1313. {
  1314. struct csky_elf_link_hash_table *ret
  1315. = (struct csky_elf_link_hash_table *) obfd->link.hash;
  1316. bfd_hash_table_free (&ret->stub_hash_table);
  1317. _bfd_elf_link_hash_table_free (obfd);
  1318. }
  1319. /* Create an CSKY elf linker hash table. */
  1320. static struct bfd_link_hash_table *
  1321. csky_elf_link_hash_table_create (bfd *abfd)
  1322. {
  1323. struct csky_elf_link_hash_table *ret;
  1324. size_t amt = sizeof (struct csky_elf_link_hash_table);
  1325. ret = (struct csky_elf_link_hash_table*) bfd_zmalloc (amt);
  1326. if (ret == NULL)
  1327. return NULL;
  1328. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
  1329. csky_elf_link_hash_newfunc,
  1330. sizeof (struct csky_elf_link_hash_entry),
  1331. CSKY_ELF_DATA))
  1332. {
  1333. free (ret);
  1334. return NULL;
  1335. }
  1336. if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
  1337. sizeof (struct elf32_csky_stub_hash_entry)))
  1338. {
  1339. free (ret);
  1340. return NULL;
  1341. }
  1342. ret->elf.root.hash_table_free = csky_elf_link_hash_table_free;
  1343. return &ret->elf.root;
  1344. }
  1345. static bool
  1346. csky_elf_mkobject (bfd *abfd)
  1347. {
  1348. return bfd_elf_allocate_object (abfd, sizeof (struct csky_elf_obj_tdata),
  1349. CSKY_ELF_DATA);
  1350. }
  1351. /* Adjust a symbol defined by a dynamic object and referenced by a
  1352. regular object. The current definition is in some section of the
  1353. dynamic object, but we're not including those sections. We have to
  1354. change the definition to something the rest of the link can
  1355. understand. */
  1356. static bool
  1357. csky_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  1358. struct elf_link_hash_entry *h)
  1359. {
  1360. struct csky_elf_link_hash_entry *eh;
  1361. struct csky_elf_link_hash_table *htab;
  1362. asection *srel;
  1363. asection *s;
  1364. eh = (struct csky_elf_link_hash_entry *)h;
  1365. if (eh == NULL)
  1366. return false;
  1367. htab = csky_elf_hash_table (info);
  1368. if (htab == NULL)
  1369. return false;
  1370. /* Clear jsri2bsr_refcount, if creating shared library files. */
  1371. if (bfd_link_pic (info) && eh->jsri2bsr_refcount > 0)
  1372. eh->jsri2bsr_refcount = 0;
  1373. /* If there is a function, put it in the procedure linkage table. We
  1374. will fill in the contents of the procedure linkage table later. */
  1375. if (h->needs_plt)
  1376. {
  1377. /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
  1378. symbol binds locally. */
  1379. if (h->plt.refcount <= 0
  1380. || (h->type != STT_GNU_IFUNC
  1381. && (SYMBOL_CALLS_LOCAL (info, h)
  1382. || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1383. && h->root.type == bfd_link_hash_undefweak))))
  1384. {
  1385. /* This case can occur if we saw a PLT32 reloc in an input
  1386. file, but the symbol was never referred to by a dynamic
  1387. object, or if all references were garbage collected. In
  1388. such a case, we don't actually need to build a procedure
  1389. linkage table, and we can just do a PC32 reloc instead. */
  1390. h->plt.offset = (bfd_vma) -1;
  1391. h->needs_plt = 0;
  1392. if (h->got.refcount == 0)
  1393. h->got.refcount += 1;
  1394. }
  1395. else if (h->got.refcount != 0)
  1396. {
  1397. h->got.refcount -= eh->plt_refcount;
  1398. eh->plt_refcount = 0;
  1399. }
  1400. return true;
  1401. }
  1402. else
  1403. /* It's possible that we incorrectly decided a .plt reloc was
  1404. needed for an R_CKCORE_PC32 or similar reloc to a non-function
  1405. sym in check_relocs. We can't decide accurately between function
  1406. and non-function syms in check_relocs; objects loaded later in
  1407. the link may change h->type. So fix it now. */
  1408. h->plt.offset = (bfd_vma) -1;
  1409. /* If this is a weak symbol, and there is a real definition, the
  1410. processor independent code will have arranged for us to see the
  1411. real definition first, and we can just use the same value. */
  1412. if (h->is_weakalias)
  1413. {
  1414. struct elf_link_hash_entry *def = weakdef (h);
  1415. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  1416. h->root.u.def.section = def->root.u.def.section;
  1417. h->root.u.def.value = def->root.u.def.value;
  1418. return true;
  1419. }
  1420. /* If there are no non-GOT references, we do not need a copy
  1421. relocation. */
  1422. if (!h->non_got_ref)
  1423. return true;
  1424. /* This is a reference to a symbol defined by a dynamic object which
  1425. is not a function. */
  1426. /* If we are creating a shared library, we must presume that the
  1427. only references to the symbol are via the global offset table.
  1428. For such cases we need not do anything here; the relocations will
  1429. be handled correctly by relocate_section. */
  1430. if (bfd_link_pic (info) || htab->elf.is_relocatable_executable)
  1431. return true;
  1432. /* We must allocate the symbol in our .dynbss section, which will
  1433. become part of the .bss section of the executable. There will be
  1434. an entry for this symbol in the .dynsym section. The dynamic
  1435. object will contain position independent code, so all references
  1436. from the dynamic object to this symbol will go through the global
  1437. offset table. The dynamic linker will use the .dynsym entry to
  1438. determine the address it must put in the global offset table, so
  1439. both the dynamic object and the regular object will refer to the
  1440. same memory location for the variable. */
  1441. /* We must generate a R_CKCORE_COPY reloc to tell the dynamic linker to
  1442. copy the initial value out of the dynamic object and into the
  1443. runtime process image. We need to remember the offset into the
  1444. .rela.bss section we are going to use. */
  1445. if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  1446. {
  1447. s = htab->elf.sdynrelro;
  1448. srel = htab->elf.sreldynrelro;
  1449. }
  1450. else
  1451. {
  1452. s = htab->elf.sdynbss;
  1453. srel = htab->elf.srelbss;
  1454. }
  1455. if (info->nocopyreloc == 0
  1456. && (h->root.u.def.section->flags & SEC_ALLOC) != 0
  1457. && h->size != 0
  1458. && srel != NULL
  1459. && s != NULL)
  1460. {
  1461. srel->size += sizeof (Elf32_External_Rela);
  1462. h->needs_copy = 1;
  1463. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  1464. }
  1465. h->non_got_ref = 0;
  1466. return true;
  1467. }
  1468. /* Allocate space in .plt, .got and associated reloc sections for
  1469. dynamic relocs. */
  1470. static bool
  1471. csky_allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
  1472. {
  1473. struct bfd_link_info *info;
  1474. struct csky_elf_link_hash_table *htab;
  1475. struct csky_elf_link_hash_entry *eh;
  1476. struct elf_dyn_relocs *p;
  1477. /* For indirect case, such as _ZdlPv to _ZdlPv@@GLIBCXX_3.4. */
  1478. if (h->root.type == bfd_link_hash_indirect)
  1479. return true;
  1480. if (h->root.type == bfd_link_hash_warning)
  1481. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1482. info = (struct bfd_link_info *) inf;
  1483. htab = csky_elf_hash_table (info);
  1484. if (htab == NULL)
  1485. return false;
  1486. /*TODO: how to deal with weak symbol relocs. */
  1487. if ((htab->elf.dynamic_sections_created || h->type == STT_GNU_IFUNC)
  1488. && h->plt.refcount > 0)
  1489. {
  1490. /* Make sure this symbol is output as a dynamic symbol.
  1491. Undefined weak syms won't yet be marked as dynamic. */
  1492. if (h->dynindx == -1 && !h->forced_local
  1493. && h->root.type == bfd_link_hash_undefweak
  1494. && ! bfd_elf_link_record_dynamic_symbol (info, h))
  1495. return false;
  1496. if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
  1497. {
  1498. asection *splt = htab->elf.splt;
  1499. /* If this is the first .plt entry, make room for the special
  1500. first entry. */
  1501. if (splt->size == 0)
  1502. {
  1503. if (bfd_csky_abi (info->output_bfd) == CSKY_ABI_V1)
  1504. splt->size += PLT_ENTRY_SIZE_P;
  1505. else
  1506. splt->size += PLT_ENTRY_SIZE;
  1507. }
  1508. h->plt.offset = splt->size;
  1509. /* If this symbol is not defined in a regular file, and we are
  1510. not generating a shared library, then set the symbol to this
  1511. location in the .plt. This is required to make function
  1512. pointers compare as equal between the normal executable and
  1513. the shared library. */
  1514. if (!bfd_link_pic (info) && !h->def_regular)
  1515. {
  1516. h->root.u.def.section = splt;
  1517. h->root.u.def.value = h->plt.offset;
  1518. }
  1519. /* Make room for this entry. */
  1520. if (bfd_csky_abi (info->output_bfd) == CSKY_ABI_V1)
  1521. splt->size += PLT_ENTRY_SIZE_P;
  1522. else
  1523. splt->size += PLT_ENTRY_SIZE;
  1524. /* We also need to make an entry in the .rela.plt section. */
  1525. htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
  1526. /* We also need to make an entry in the .got.plt section, which
  1527. will be placed in the .got section by the linker script. */
  1528. htab->elf.sgotplt->size += 4;
  1529. }
  1530. else
  1531. {
  1532. h->plt.offset = (bfd_vma) -1;
  1533. h->needs_plt = 0;
  1534. }
  1535. }
  1536. else
  1537. {
  1538. h->plt.offset = (bfd_vma) -1;
  1539. h->needs_plt = 0;
  1540. }
  1541. if (h->got.refcount > 0)
  1542. {
  1543. asection *sgot;
  1544. bool dyn;
  1545. int indx;
  1546. int tls_type = csky_elf_hash_entry (h)->tls_type;
  1547. /* Make sure this symbol is output as a dynamic symbol.
  1548. Undefined weak syms won't yet be marked as dynamic. */
  1549. if (h->dynindx == -1 && !h->forced_local
  1550. && h->root.type == bfd_link_hash_undefweak
  1551. && ! bfd_elf_link_record_dynamic_symbol (info, h))
  1552. return false;
  1553. sgot = htab->elf.sgot;
  1554. h->got.offset = sgot->size;
  1555. BFD_ASSERT (tls_type != GOT_UNKNOWN);
  1556. if (tls_type == GOT_NORMAL)
  1557. /* Non-TLS symbols need one GOT slot. */
  1558. sgot->size += 4;
  1559. else
  1560. {
  1561. if (tls_type & GOT_TLS_GD)
  1562. /* R_CKCORE_TLS_GD32 needs 2 consecutive GOT slots. */
  1563. sgot->size += 8;
  1564. if (tls_type & GOT_TLS_IE)
  1565. /* R_CKCORE_TLS_IE32 needs one GOT slot. */
  1566. sgot->size += 4;
  1567. }
  1568. dyn = htab->elf.dynamic_sections_created;
  1569. indx = 0;
  1570. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
  1571. && (! bfd_link_pic (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
  1572. indx = h->dynindx;
  1573. if (tls_type != GOT_NORMAL
  1574. && (bfd_link_pic (info) || indx != 0)
  1575. && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  1576. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  1577. || h->root.type != bfd_link_hash_undefweak))
  1578. {
  1579. if (tls_type & GOT_TLS_IE)
  1580. htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
  1581. if (tls_type & GOT_TLS_GD)
  1582. htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
  1583. if ((tls_type & GOT_TLS_GD) && indx != 0)
  1584. htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
  1585. }
  1586. else if (((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  1587. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  1588. || h->root.type != bfd_link_hash_undefweak)
  1589. && (bfd_link_pic (info)
  1590. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)
  1591. || h->plt.offset == (bfd_vma) -1))
  1592. htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
  1593. }
  1594. else
  1595. h->got.offset = (bfd_vma) -1;
  1596. eh = (struct csky_elf_link_hash_entry *) h;
  1597. if (h->dyn_relocs == NULL)
  1598. return true;
  1599. /* In the shared -Bsymbolic case, discard space allocated for
  1600. dynamic pc-relative relocs against symbols which turn out to be
  1601. defined in regular objects. For the normal shared case, discard
  1602. space for pc-relative relocs that have become local due to symbol
  1603. visibility changes. */
  1604. if (bfd_link_pic (info))
  1605. {
  1606. if (SYMBOL_CALLS_LOCAL (info, h))
  1607. {
  1608. struct elf_dyn_relocs **pp;
  1609. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  1610. {
  1611. p->count -= p->pc_count;
  1612. p->pc_count = 0;
  1613. if (p->count == 0)
  1614. *pp = p->next;
  1615. else
  1616. pp = &p->next;
  1617. }
  1618. }
  1619. if (eh->jsri2bsr_refcount
  1620. && h->root.type == bfd_link_hash_defined
  1621. && h->dyn_relocs != NULL)
  1622. h->dyn_relocs->count -= eh->jsri2bsr_refcount;
  1623. /* Also discard relocs on undefined weak syms with non-default
  1624. visibility. */
  1625. if (h->dyn_relocs != NULL
  1626. && h->root.type == bfd_link_hash_undefweak)
  1627. {
  1628. if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1629. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  1630. h->dyn_relocs = NULL;
  1631. /* Make sure undefined weak symbols are output as a dynamic
  1632. symbol in PIEs. */
  1633. else if (h->dynindx == -1
  1634. && !h->forced_local
  1635. && !bfd_elf_link_record_dynamic_symbol (info, h))
  1636. return false;
  1637. }
  1638. }
  1639. else
  1640. {
  1641. /* For the non-shared case, discard space for relocs against
  1642. symbols which turn out to need copy relocs or are not
  1643. dynamic. */
  1644. if (!h->non_got_ref
  1645. && ((h->def_dynamic && !h->def_regular)
  1646. || (htab->elf.dynamic_sections_created
  1647. && (h->root.type == bfd_link_hash_undefweak
  1648. || h->root.type == bfd_link_hash_indirect
  1649. || h->root.type == bfd_link_hash_undefined))))
  1650. {
  1651. /* Make sure this symbol is output as a dynamic symbol.
  1652. Undefined weak syms won't yet be marked as dynamic. */
  1653. if (h->dynindx == -1 && !h->forced_local
  1654. && h->root.type == bfd_link_hash_undefweak)
  1655. {
  1656. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1657. return false;
  1658. }
  1659. /* If that succeeded, we know we'll be keeping all the
  1660. relocs. */
  1661. if (h->dynindx != -1)
  1662. goto keep;
  1663. }
  1664. h->dyn_relocs = NULL;
  1665. keep: ;
  1666. }
  1667. /* Finally, allocate space. */
  1668. for (p = h->dyn_relocs; p != NULL; p = p->next)
  1669. {
  1670. asection *srelgot = htab->elf.srelgot;
  1671. srelgot->size += p->count * sizeof (Elf32_External_Rela);
  1672. }
  1673. return true;
  1674. }
  1675. /* Set the sizes of the dynamic sections. */
  1676. static bool
  1677. csky_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  1678. struct bfd_link_info *info)
  1679. {
  1680. struct csky_elf_link_hash_table *htab;
  1681. bfd *dynobj;
  1682. asection *s;
  1683. bool relocs;
  1684. bfd *ibfd;
  1685. htab = csky_elf_hash_table (info);
  1686. if (htab == NULL)
  1687. return false;
  1688. dynobj = htab->elf.dynobj;
  1689. if (dynobj == NULL)
  1690. return false;
  1691. if (htab->elf.dynamic_sections_created)
  1692. {
  1693. /* Set the contents of the .interp section to the interpreter. */
  1694. if (!bfd_link_pic (info) && !info->nointerp)
  1695. {
  1696. s = bfd_get_section_by_name (dynobj, ".interp");
  1697. BFD_ASSERT (s != NULL);
  1698. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  1699. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  1700. }
  1701. }
  1702. /* Set up .got offsets for local syms, and space for local dynamic
  1703. relocs. */
  1704. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  1705. {
  1706. bfd_signed_vma *local_got_refcounts;
  1707. bfd_signed_vma *end_local_got;
  1708. bfd_size_type locsymcount;
  1709. Elf_Internal_Shdr *symtab_hdr;
  1710. asection *srelgot, *sgot;
  1711. char *local_tls_type;
  1712. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  1713. continue;
  1714. sgot = htab->elf.sgot;
  1715. srelgot = htab->elf.srelgot;
  1716. for (s = ibfd->sections; s != NULL; s = s->next)
  1717. {
  1718. struct elf_dyn_relocs *p;
  1719. for (p = *((struct elf_dyn_relocs **)
  1720. &elf_section_data (s)->local_dynrel);
  1721. p != NULL;
  1722. p = p->next)
  1723. {
  1724. if (!bfd_is_abs_section (p->sec)
  1725. && bfd_is_abs_section (p->sec->output_section))
  1726. /* Input section has been discarded, either because
  1727. it is a copy of a linkonce section or due to
  1728. linker script /DISCARD/, so we'll be discarding
  1729. the relocs too. */
  1730. ;
  1731. else if (p->count != 0)
  1732. {
  1733. srelgot->size += p->count * sizeof (Elf32_External_Rela);
  1734. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  1735. info->flags |= DF_TEXTREL;
  1736. }
  1737. }
  1738. }
  1739. local_got_refcounts = elf_local_got_refcounts (ibfd);
  1740. if (!local_got_refcounts)
  1741. continue;
  1742. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  1743. locsymcount = symtab_hdr->sh_info;
  1744. end_local_got = local_got_refcounts + locsymcount;
  1745. local_tls_type = csky_elf_local_got_tls_type (ibfd);
  1746. for (; local_got_refcounts < end_local_got;
  1747. ++local_got_refcounts, ++local_tls_type)
  1748. {
  1749. if (*local_got_refcounts > 0)
  1750. {
  1751. /* GOT_TLS_GD and GOT_TLS_IE type for TLS, GOT_NORMAL type
  1752. for GOT. If output file is shared library, we should output
  1753. GOT_TLS_GD type relocation in .rel.got. */
  1754. *local_got_refcounts = sgot->size;
  1755. if (*local_tls_type & GOT_TLS_GD)
  1756. /* TLS_GD relocs need an 8-byte structure in the GOT. */
  1757. sgot->size += 8;
  1758. if (*local_tls_type & GOT_TLS_IE)
  1759. sgot->size += 4;
  1760. if (*local_tls_type == GOT_NORMAL)
  1761. sgot->size += 4;
  1762. if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
  1763. srelgot->size += sizeof (Elf32_External_Rela);
  1764. }
  1765. else
  1766. *local_got_refcounts = (bfd_vma) -1;
  1767. }
  1768. }
  1769. if (htab->tls_ldm_got.refcount > 0)
  1770. {
  1771. /* Allocate two GOT entries and one dynamic relocation (if necessary)
  1772. for R_CSKY_TLS_LDM32 relocations. */
  1773. htab->tls_ldm_got.offset = htab->elf.sgot->size;
  1774. htab->elf.sgot->size += 8;
  1775. if (bfd_link_pic (info))
  1776. htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
  1777. }
  1778. else
  1779. htab->tls_ldm_got.offset = -1;
  1780. /* Allocate global sym .plt and .got entries, and space for global
  1781. sym dynamic relocs. */
  1782. elf_link_hash_traverse (&htab->elf, csky_allocate_dynrelocs, (PTR) info);
  1783. /* Check for GOT overflow. */
  1784. if (check_got_overflow == 1
  1785. && htab->elf.sgot->size + htab->elf.sgotplt->size > GOT_MAX_SIZE)
  1786. {
  1787. _bfd_error_handler (_("GOT table size out of range")); /* */
  1788. return false;
  1789. }
  1790. /* We now have determined the sizes of the various dynamic sections.
  1791. Allocate memory for them. */
  1792. relocs = false;
  1793. for (s = dynobj->sections; s != NULL; s = s->next)
  1794. {
  1795. bool strip_section = true;
  1796. if ((s->flags & SEC_LINKER_CREATED) == 0)
  1797. continue;
  1798. if (s == htab->elf.splt
  1799. || s == htab->elf.sgot
  1800. || s == htab->elf.sgotplt
  1801. || s == htab->elf.sdynrelro
  1802. || s == htab->elf.sreldynrelro)
  1803. {
  1804. /* Strip this section if we don't need it;
  1805. see the comment below. */
  1806. /* We'd like to strip these sections if they aren't needed, but if
  1807. we've exported dynamic symbols from them we must leave them.
  1808. It's too late to tell BFD to get rid of the symbols. */
  1809. if (htab->elf.hplt != NULL)
  1810. strip_section = false;
  1811. }
  1812. else if (startswith (bfd_section_name (s), ".rel") )
  1813. {
  1814. if (s->size != 0 )
  1815. relocs = true;
  1816. /* We use the reloc_count field as a counter if we need
  1817. to copy relocs into the output file. */
  1818. s->reloc_count = 0;
  1819. }
  1820. else
  1821. /* It's not one of our sections, so don't allocate space. */
  1822. continue;
  1823. /* Strip this section if we don't need it; see the
  1824. comment below. */
  1825. if (s->size == 0)
  1826. {
  1827. /* If we don't need this section, strip it from the
  1828. output file. This is mostly to handle .rel.bss and
  1829. .rel.plt. We must create both sections in
  1830. create_dynamic_sections, because they must be created
  1831. before the linker maps input sections to output
  1832. sections. The linker does that before
  1833. adjust_dynamic_symbol is called, and it is that
  1834. function which decides whether anything needs to go
  1835. into these sections. */
  1836. if (strip_section)
  1837. s->flags |= SEC_EXCLUDE;
  1838. continue;
  1839. }
  1840. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  1841. continue;
  1842. /* Allocate memory for the section contents. We use bfd_zalloc
  1843. here in case unused entries are not reclaimed before the
  1844. section's contents are written out. This should not happen,
  1845. but this way if it does, we get a R_CKCORE_NONE reloc instead
  1846. of garbage. */
  1847. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  1848. if (s->contents == NULL)
  1849. return false;
  1850. }
  1851. if (htab->elf.dynamic_sections_created)
  1852. htab->elf.dt_pltgot_required = htab->elf.sgot->size != 0;
  1853. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
  1854. }
  1855. /* Finish up dynamic symbol handling. We set the contents of various
  1856. dynamic sections here. */
  1857. static bool
  1858. csky_elf_finish_dynamic_symbol (bfd *output_bfd,
  1859. struct bfd_link_info *info,
  1860. struct elf_link_hash_entry *h,
  1861. Elf_Internal_Sym *sym)
  1862. {
  1863. struct csky_elf_link_hash_table *htab;
  1864. htab = csky_elf_hash_table (info);
  1865. if (htab == NULL)
  1866. return false;
  1867. /* Sanity check to make sure no unexpected symbol reaches here.
  1868. This matches the test in csky_elf_relocate_section handling
  1869. of GOT/PLT entries. */
  1870. BFD_ASSERT (! (h->dynindx == -1
  1871. && !h->forced_local
  1872. && h->root.type != bfd_link_hash_undefweak
  1873. && bfd_link_pic (info)));
  1874. if (h->plt.offset != (bfd_vma) -1)
  1875. {
  1876. bfd_vma plt_index;
  1877. bfd_vma got_offset;
  1878. Elf_Internal_Rela rel;
  1879. bfd_byte *loc;
  1880. asection *plt, *relplt, *gotplt;
  1881. plt = htab->elf.splt;
  1882. relplt = htab->elf.srelplt;
  1883. gotplt = htab->elf.sgotplt;
  1884. /* This symbol has an entry in the procedure linkage table. Set
  1885. it up. */
  1886. BFD_ASSERT (h->dynindx != -1
  1887. || ((h->forced_local || bfd_link_executable (info))
  1888. && h->def_regular));
  1889. BFD_ASSERT (plt != NULL && gotplt != NULL && relplt != NULL);
  1890. if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
  1891. plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
  1892. else
  1893. plt_index = h->plt.offset / PLT_ENTRY_SIZE_P - 1;
  1894. got_offset = (plt_index + 3) * 4;
  1895. /* Fill in the entry in the procedure linkage table. */
  1896. if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
  1897. {
  1898. csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[0],
  1899. plt->contents + h->plt.offset);
  1900. csky_put_insn_32 (output_bfd,
  1901. (csky_elf_plt_entry_v2[1] | plt_index),
  1902. plt->contents + h->plt.offset + 4);
  1903. csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[2],
  1904. plt->contents + h->plt.offset + 8);
  1905. }
  1906. else
  1907. {
  1908. int i;
  1909. for (i = 0; i < 6; i++)
  1910. bfd_put_16 (output_bfd, csky_elf_plt_entry_v1[i],
  1911. plt->contents + h->plt.offset + i * 2);
  1912. bfd_put_32 (output_bfd, plt_index,
  1913. plt->contents + h->plt.offset + i * 2);
  1914. }
  1915. /* Fill in the entry in the .rel.plt section. */
  1916. rel.r_offset = (htab->elf.sgotplt->output_section->vma
  1917. + htab->elf.sgotplt->output_offset
  1918. + got_offset);
  1919. rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_JUMP_SLOT);
  1920. rel.r_addend = (plt->output_section->vma
  1921. + plt->output_offset
  1922. + h->plt.offset);
  1923. loc = (htab->elf.srelplt->contents
  1924. + plt_index * sizeof (Elf32_External_Rela));
  1925. if (loc != NULL)
  1926. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  1927. if (! h->def_regular)
  1928. {
  1929. /* Mark the symbol as undefined, rather than as defined in
  1930. the .plt section. Leave the value alone. */
  1931. sym->st_shndx = SHN_UNDEF;
  1932. /* If the symbol is weak, we do need to clear the value.
  1933. Otherwise, the PLT entry would provide a definition for
  1934. the symbol even if the symbol wasn't defined anywhere,
  1935. and so the symbol would never be NULL. Leave the value if
  1936. there were any relocations where pointer equality matters
  1937. (this is a clue for the dynamic linker, to make function
  1938. pointer comparisons work between an application and shared
  1939. library). */
  1940. if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
  1941. sym->st_value = 0;
  1942. }
  1943. }
  1944. /* Fill in the entry in the .got section. */
  1945. if (h->got.offset != (bfd_vma) -1
  1946. && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_GD) == 0)
  1947. && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_IE) == 0))
  1948. {
  1949. Elf_Internal_Rela rel;
  1950. bfd_byte *loc;
  1951. /* This symbol has an entry in the global offset table.
  1952. Set it up. */
  1953. BFD_ASSERT (htab->elf.sgot != NULL && htab->elf.srelgot != NULL);
  1954. rel.r_offset = (htab->elf.sgot->output_section->vma
  1955. + htab->elf.sgot->output_offset
  1956. + (h->got.offset & ~(bfd_vma) 1));
  1957. /* If this is a static link, or it is a -Bsymbolic link and the
  1958. symbol is defined locally or was forced to be local because
  1959. of a version file, we just want to emit a RELATIVE reloc.
  1960. The entry in the global offset table will already have been
  1961. initialized in the relocate_section function. */
  1962. if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
  1963. {
  1964. BFD_ASSERT ((h->got.offset & 1) != 0);
  1965. rel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
  1966. rel.r_addend = (h->root.u.def.value
  1967. + h->root.u.def.section->output_offset
  1968. + h->root.u.def.section->output_section->vma);
  1969. }
  1970. else
  1971. {
  1972. BFD_ASSERT ((h->got.offset & 1) == 0);
  1973. bfd_put_32 (output_bfd, (bfd_vma) 0,
  1974. htab->elf.sgot->contents + h->got.offset);
  1975. rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_GLOB_DAT);
  1976. rel.r_addend = 0;
  1977. }
  1978. loc = htab->elf.srelgot->contents;
  1979. loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  1980. if (loc != NULL)
  1981. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  1982. }
  1983. if (h->needs_copy)
  1984. {
  1985. asection *s;
  1986. Elf_Internal_Rela rela;
  1987. bfd_byte *loc;
  1988. /* This symbol needs a copy reloc. Set it up. */
  1989. BFD_ASSERT (h->dynindx != -1
  1990. && (h->root.type == bfd_link_hash_defined
  1991. || h->root.type == bfd_link_hash_defweak));
  1992. rela.r_offset = (h->root.u.def.value
  1993. + h->root.u.def.section->output_section->vma
  1994. + h->root.u.def.section->output_offset);
  1995. rela.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_COPY);
  1996. rela.r_addend = 0;
  1997. if (h->root.u.def.section == htab->elf.sdynrelro)
  1998. s = htab->elf.sreldynrelro;
  1999. else
  2000. s = htab->elf.srelbss;
  2001. BFD_ASSERT (s != NULL);
  2002. loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
  2003. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  2004. }
  2005. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  2006. if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  2007. || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  2008. sym->st_shndx = SHN_ABS;
  2009. return true;
  2010. }
  2011. /* Finish up the dynamic sections. */
  2012. static bool
  2013. csky_elf_finish_dynamic_sections (bfd *output_bfd,
  2014. struct bfd_link_info *info)
  2015. {
  2016. struct csky_elf_link_hash_table *htab;
  2017. bfd *dynobj;
  2018. asection *sdyn;
  2019. asection *got_sec;
  2020. htab = csky_elf_hash_table (info);
  2021. if (htab == NULL)
  2022. return false;
  2023. dynobj = htab->elf.dynobj;
  2024. sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
  2025. if (htab->elf.dynamic_sections_created)
  2026. {
  2027. Elf32_External_Dyn *dyncon, *dynconend;
  2028. BFD_ASSERT (sdyn != NULL && htab->elf.sgot != NULL);
  2029. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  2030. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  2031. for (; dyncon < dynconend; dyncon++)
  2032. {
  2033. Elf_Internal_Dyn dyn;
  2034. bool size = false;
  2035. const char *name = NULL;
  2036. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  2037. switch (dyn.d_tag)
  2038. {
  2039. default:
  2040. continue;
  2041. case DT_RELA:
  2042. name = ".rela.dyn";
  2043. size = false;
  2044. break;
  2045. case DT_RELASZ:
  2046. name = ".rela.dyn";
  2047. size = true;
  2048. break;
  2049. case DT_PLTRELSZ:
  2050. name = ".rela.plt";
  2051. size = true;
  2052. break;
  2053. case DT_PLTGOT:
  2054. dyn.d_un.d_ptr = htab->elf.sgot->output_section->vma;
  2055. break;
  2056. case DT_JMPREL:
  2057. dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma
  2058. + htab->elf.srelplt->output_offset;
  2059. break;
  2060. }
  2061. if (name != NULL)
  2062. {
  2063. asection *s = bfd_get_section_by_name (output_bfd, name);
  2064. if (s == NULL)
  2065. dyn.d_un.d_val = 0;
  2066. else if (!size)
  2067. dyn.d_un.d_ptr = s->vma;
  2068. else
  2069. dyn.d_un.d_val = s->size;
  2070. }
  2071. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2072. }
  2073. }
  2074. /* Fill in the first three entries in the global offset table. */
  2075. if (htab->elf.sgotplt)
  2076. got_sec = htab->elf.sgotplt;
  2077. else
  2078. got_sec = htab->elf.sgot;
  2079. if (got_sec != NULL)
  2080. {
  2081. if (got_sec->size > 0)
  2082. {
  2083. bfd_put_32 (output_bfd,
  2084. (sdyn == NULL ? (bfd_vma) 0
  2085. : sdyn->output_section->vma + sdyn->output_offset),
  2086. got_sec->contents);
  2087. bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 4);
  2088. bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 8);
  2089. }
  2090. elf_section_data (got_sec->output_section)->this_hdr.sh_entsize = 4;
  2091. }
  2092. return true;
  2093. }
  2094. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  2095. static void
  2096. csky_elf_copy_indirect_symbol (struct bfd_link_info *info,
  2097. struct elf_link_hash_entry *dir,
  2098. struct elf_link_hash_entry *ind)
  2099. {
  2100. struct csky_elf_link_hash_entry *edir, *eind;
  2101. edir = (struct csky_elf_link_hash_entry *) dir;
  2102. eind = (struct csky_elf_link_hash_entry *) ind;
  2103. if (ind->root.type == bfd_link_hash_indirect
  2104. && dir->got.refcount <= 0)
  2105. {
  2106. edir->tls_type = eind->tls_type;
  2107. eind->tls_type = GOT_UNKNOWN;
  2108. }
  2109. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  2110. }
  2111. /* Used to decide how to sort relocs in an optimal manner for the
  2112. dynamic linker, before writing them out. */
  2113. static enum elf_reloc_type_class
  2114. csky_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2115. const asection *rel_sec ATTRIBUTE_UNUSED,
  2116. const Elf_Internal_Rela *rela)
  2117. {
  2118. switch ((int) ELF32_R_TYPE (rela->r_info))
  2119. {
  2120. case R_CKCORE_RELATIVE:
  2121. return reloc_class_relative;
  2122. case R_CKCORE_JUMP_SLOT:
  2123. return reloc_class_plt;
  2124. case R_CKCORE_COPY:
  2125. return reloc_class_copy;
  2126. case R_CKCORE_IRELATIVE:
  2127. return reloc_class_ifunc;
  2128. default:
  2129. return reloc_class_normal;
  2130. }
  2131. }
  2132. /* Return the section that should be marked against GC for a given
  2133. relocation. */
  2134. static asection *
  2135. csky_elf_gc_mark_hook (asection *sec,
  2136. struct bfd_link_info *info,
  2137. Elf_Internal_Rela *rel,
  2138. struct elf_link_hash_entry *h,
  2139. Elf_Internal_Sym *sym)
  2140. {
  2141. if (h != NULL)
  2142. {
  2143. switch (ELF32_R_TYPE (rel->r_info))
  2144. {
  2145. case R_CKCORE_GNU_VTINHERIT:
  2146. case R_CKCORE_GNU_VTENTRY:
  2147. return NULL;
  2148. }
  2149. }
  2150. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  2151. }
  2152. /* Match symbol names created by tc-csky.c:make_mapping_symbol. */
  2153. static bool
  2154. is_mapping_symbol_name (const char *name)
  2155. {
  2156. return (name && name[0] == '$'
  2157. && (name[1] == 't' || name[1] == 'd')
  2158. && name[2] == 0);
  2159. }
  2160. /* Treat mapping symbols as special target symbols. */
  2161. static bool
  2162. csky_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
  2163. {
  2164. return is_mapping_symbol_name (sym->name);
  2165. }
  2166. /* Exclude mapping symbols from being treated as function symbols by
  2167. objdump and nm. */
  2168. static bfd_size_type
  2169. csky_elf_maybe_function_sym (const asymbol *sym, asection *sec,
  2170. bfd_vma *code_off)
  2171. {
  2172. if ((sym->flags & BSF_LOCAL) != 0
  2173. && is_mapping_symbol_name (sym->name))
  2174. return 0;
  2175. return _bfd_elf_maybe_function_sym (sym, sec, code_off);
  2176. }
  2177. /* Look through the relocs for a section during the first phase.
  2178. Since we don't do .gots or .plts, we just need to consider the
  2179. virtual table relocs for gc. */
  2180. static bool
  2181. csky_elf_check_relocs (bfd * abfd,
  2182. struct bfd_link_info * info,
  2183. asection * sec,
  2184. const Elf_Internal_Rela * relocs)
  2185. {
  2186. Elf_Internal_Shdr * symtab_hdr;
  2187. struct elf_link_hash_entry ** sym_hashes;
  2188. const Elf_Internal_Rela * rel;
  2189. const Elf_Internal_Rela * rel_end;
  2190. struct csky_elf_link_hash_table *htab;
  2191. asection *sreloc;
  2192. /* if output type is relocatable, return. */
  2193. if (bfd_link_relocatable (info))
  2194. return true;
  2195. htab = csky_elf_hash_table (info);
  2196. if (htab == NULL)
  2197. return false;
  2198. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  2199. sym_hashes = elf_sym_hashes (abfd);
  2200. rel_end = relocs + sec->reloc_count;
  2201. sreloc = NULL;
  2202. for (rel = relocs; rel < rel_end; rel++)
  2203. {
  2204. struct elf_link_hash_entry *h;
  2205. unsigned long r_symndx;
  2206. Elf_Internal_Sym *isym;
  2207. int r_type;
  2208. r_symndx = ELF32_R_SYM (rel->r_info);
  2209. r_type = ELF32_R_TYPE (rel->r_info);
  2210. if (r_symndx < symtab_hdr->sh_info)
  2211. {
  2212. /* A local symbol. */
  2213. isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
  2214. abfd, r_symndx);
  2215. if (isym == NULL)
  2216. return false;
  2217. h = NULL;
  2218. }
  2219. else
  2220. {
  2221. isym = NULL;
  2222. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  2223. while (h->root.type == bfd_link_hash_indirect
  2224. || h->root.type == bfd_link_hash_warning)
  2225. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2226. }
  2227. switch (r_type)
  2228. {
  2229. case R_CKCORE_PCREL_IMM26BY2:
  2230. case R_CKCORE_PCREL_IMM11BY2:
  2231. case R_CKCORE_PCREL_JSR_IMM11BY2:
  2232. case R_CKCORE_PCREL_JSR_IMM26BY2:
  2233. /* If the symbol is '*UND*', means this reloc is used for
  2234. * callgraph, don't need to leave to shared object. */
  2235. if (r_symndx == 0)
  2236. break;
  2237. /* Else fall through. */
  2238. case R_CKCORE_ADDR32:
  2239. case R_CKCORE_ADDR_HI16:
  2240. case R_CKCORE_ADDR_LO16:
  2241. if (h != NULL
  2242. && bfd_link_executable (info)
  2243. && r_type == R_CKCORE_ADDR32
  2244. && h->type == STT_OBJECT
  2245. && (sec->flags & SEC_ALLOC) != 0
  2246. && (sec->flags & SEC_READONLY))
  2247. /* If this reloc is in a read-only section, we might
  2248. need a copy reloc. We can't check reliably at this
  2249. stage whether the section is read-only, as input
  2250. sections have not yet been mapped to output sections.
  2251. Tentatively set the flag for now, and correct in
  2252. adjust_dynamic_symbol. */
  2253. h->non_got_ref = 1;
  2254. /* If we are creating a shared library or relocatable executable,
  2255. and this is a reloc against a global symbol, then we need to
  2256. copy the reloc into the shared library. However, if we are
  2257. linking with -Bsymbolic, we do not need to copy a reloc
  2258. against a global symbol which is defined in an object we are
  2259. including in the link (i.e., DEF_REGULAR is set). At
  2260. this point we have not seen all the input files, so it is
  2261. possible that DEF_REGULAR is not set now but will be set
  2262. later (it is never cleared). We account for that possibility
  2263. below by storing information in the relocs_copied field of
  2264. the hash table entry. */
  2265. if ((bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
  2266. || (!bfd_link_pic (info)
  2267. && (sec->flags & SEC_ALLOC) != 0
  2268. && h != NULL
  2269. && (h->root.type == bfd_link_hash_defweak
  2270. || !h->def_regular)))
  2271. {
  2272. struct elf_dyn_relocs *p;
  2273. struct elf_dyn_relocs **head;
  2274. /* We must copy these reloc types into the output file.
  2275. Create a reloc section in dynobj and make room for
  2276. this reloc. */
  2277. if (sreloc == NULL)
  2278. {
  2279. if (htab->elf.dynobj == NULL)
  2280. htab->elf.dynobj = abfd;
  2281. sreloc = _bfd_elf_make_dynamic_reloc_section
  2282. (sec, htab->elf.dynobj, 2, abfd, true);
  2283. if (sreloc == NULL)
  2284. return false;
  2285. }
  2286. if (h == NULL && !use_branch_stub
  2287. && ((ELF32_R_TYPE (rel->r_info)
  2288. == R_CKCORE_PCREL_IMM26BY2)
  2289. || (ELF32_R_TYPE (rel->r_info)
  2290. == R_CKCORE_PCREL_IMM11BY2)))
  2291. break;
  2292. /* If this is a global symbol, we count the number of
  2293. relocations we need for this symbol. */
  2294. if (h != NULL)
  2295. {
  2296. struct csky_elf_link_hash_entry *eh;
  2297. eh = (struct csky_elf_link_hash_entry *)h;
  2298. if ((ELF32_R_TYPE (rel->r_info)
  2299. == R_CKCORE_PCREL_JSR_IMM26BY2)
  2300. || (ELF32_R_TYPE (rel->r_info)
  2301. == R_CKCORE_PCREL_JSR_IMM11BY2))
  2302. eh->jsri2bsr_refcount += 1;
  2303. head = &h->dyn_relocs;
  2304. }
  2305. else
  2306. {
  2307. /* Track dynamic relocs needed for local syms too.
  2308. We really need local syms available to do this
  2309. easily. Oh well. */
  2310. void **vpp;
  2311. asection *s;
  2312. Elf_Internal_Sym *loc_isym;
  2313. loc_isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
  2314. abfd, r_symndx);
  2315. if (loc_isym == NULL)
  2316. return false;
  2317. s = bfd_section_from_elf_index (abfd, loc_isym->st_shndx);
  2318. if (s == NULL)
  2319. s = sec;
  2320. vpp = &elf_section_data (s)->local_dynrel;
  2321. head = (struct elf_dyn_relocs **)vpp;
  2322. }
  2323. p = *head;
  2324. if (p == NULL || p->sec != sec)
  2325. {
  2326. size_t amt = sizeof *p;
  2327. p = ((struct elf_dyn_relocs *)
  2328. bfd_alloc (htab->elf.dynobj, amt));
  2329. if (p == NULL)
  2330. return false;
  2331. p->next = *head;
  2332. *head = p;
  2333. p->sec = sec;
  2334. p->count = 0;
  2335. p->pc_count = 0;
  2336. }
  2337. if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2
  2338. || ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM11BY2)
  2339. p->pc_count += 1;
  2340. p->count += 1;
  2341. }
  2342. break;
  2343. case R_CKCORE_PLT_IMM18BY4:
  2344. case R_CKCORE_PLT32:
  2345. /* This symbol requires a procedure linkage table entry. We
  2346. actually build the entry in adjust_dynamic_symbol,
  2347. because this might be a case of linking PIC code which is
  2348. never referenced by a dynamic object, in which case we
  2349. don't need to generate a procedure linkage table entry
  2350. after all. */
  2351. /* If this is a local symbol, we resolve it directly without
  2352. creating a procedure linkage table entry. */
  2353. if (h == NULL)
  2354. continue;
  2355. if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PLT_IMM18BY4)
  2356. check_got_overflow = 1;
  2357. h->needs_plt = 1;
  2358. h->plt.refcount += 1;
  2359. h->got.refcount += 1;
  2360. ((struct csky_elf_link_hash_entry *)h)->plt_refcount += 1;
  2361. break;
  2362. case R_CKCORE_GOT12:
  2363. case R_CKCORE_PLT12:
  2364. case R_CKCORE_GOT32:
  2365. case R_CKCORE_GOT_HI16:
  2366. case R_CKCORE_GOT_LO16:
  2367. case R_CKCORE_PLT_HI16:
  2368. case R_CKCORE_PLT_LO16:
  2369. case R_CKCORE_GOT_IMM18BY4:
  2370. case R_CKCORE_TLS_IE32:
  2371. case R_CKCORE_TLS_GD32:
  2372. {
  2373. int tls_type, old_tls_type;
  2374. if (h != NULL
  2375. && bfd_link_executable (info)
  2376. && r_type == R_CKCORE_GOT_IMM18BY4
  2377. && (sec->flags & SEC_ALLOC) != 0
  2378. && (sec->flags & SEC_READONLY))
  2379. /* If this reloc is in a read-only section, we might
  2380. need a copy reloc. We can't check reliably at this
  2381. stage whether the section is read-only, as input
  2382. sections have not yet been mapped to output sections.
  2383. Tentatively set the flag for now, and correct in
  2384. adjust_dynamic_symbol. */
  2385. h->non_got_ref = 1;
  2386. switch (ELF32_R_TYPE (rel->r_info))
  2387. {
  2388. case R_CKCORE_TLS_IE32:
  2389. tls_type = GOT_TLS_IE;
  2390. break;
  2391. case R_CKCORE_TLS_GD32:
  2392. tls_type = GOT_TLS_GD;
  2393. break;
  2394. default:
  2395. tls_type = GOT_NORMAL;
  2396. break;
  2397. }
  2398. if (h != NULL)
  2399. {
  2400. if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_GOT_IMM18BY4)
  2401. check_got_overflow = 1;
  2402. h->got.refcount += 1;
  2403. old_tls_type = csky_elf_hash_entry (h)->tls_type;
  2404. }
  2405. else
  2406. {
  2407. bfd_signed_vma *local_got_refcounts;
  2408. /* This is a global offset table entry for a local symbol. */
  2409. /* we can write a new function named
  2410. elf32_csky_allocate_local_sym_info() to replace
  2411. following code. */
  2412. local_got_refcounts = elf_local_got_refcounts (abfd);
  2413. if (local_got_refcounts == NULL)
  2414. {
  2415. bfd_size_type size;
  2416. size = symtab_hdr->sh_info;
  2417. size *= (sizeof (bfd_signed_vma) + sizeof (char));
  2418. local_got_refcounts = ((bfd_signed_vma *)
  2419. bfd_zalloc (abfd, size));
  2420. if (local_got_refcounts == NULL)
  2421. return false;
  2422. elf_local_got_refcounts (abfd) = local_got_refcounts;
  2423. csky_elf_local_got_tls_type (abfd)
  2424. = (char *) (local_got_refcounts + symtab_hdr->sh_info);
  2425. }
  2426. local_got_refcounts[r_symndx] += 1;
  2427. old_tls_type = csky_elf_local_got_tls_type (abfd)[r_symndx];
  2428. }
  2429. /* We will already have issued an error message if there is a
  2430. TLS / non-TLS mismatch, based on the symbol type. We don't
  2431. support any linker relaxations. So just combine any TLS
  2432. types needed. */
  2433. if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
  2434. && tls_type != GOT_NORMAL)
  2435. tls_type |= old_tls_type;
  2436. if (old_tls_type != tls_type)
  2437. {
  2438. if (h != NULL)
  2439. csky_elf_hash_entry (h)->tls_type = tls_type;
  2440. else
  2441. csky_elf_local_got_tls_type (abfd)[r_symndx] = tls_type;
  2442. }
  2443. }
  2444. /* Fall through. */
  2445. case R_CKCORE_TLS_LDM32:
  2446. if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_TLS_LDM32)
  2447. htab->tls_ldm_got.refcount++;
  2448. /* Fall through. */
  2449. case R_CKCORE_GOTOFF:
  2450. case R_CKCORE_GOTPC:
  2451. case R_CKCORE_GOTOFF_HI16:
  2452. case R_CKCORE_GOTOFF_LO16:
  2453. case R_CKCORE_GOTPC_HI16:
  2454. case R_CKCORE_GOTPC_LO16:
  2455. case R_CKCORE_GOTOFF_IMM18:
  2456. if (htab->elf.sgot == NULL)
  2457. {
  2458. if (htab->elf.dynobj == NULL)
  2459. htab->elf.dynobj = abfd;
  2460. if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
  2461. return false;
  2462. }
  2463. break;
  2464. /* This relocation describes the C++ object vtable hierarchy.
  2465. Reconstruct it for later use during GC. */
  2466. case R_CKCORE_GNU_VTINHERIT:
  2467. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  2468. return false;
  2469. break;
  2470. /* This relocation describes which C++ vtable entries are actually
  2471. used. Record for later use during GC. */
  2472. case R_CKCORE_GNU_VTENTRY:
  2473. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  2474. return false;
  2475. break;
  2476. }
  2477. }
  2478. return true;
  2479. }
  2480. static const struct bfd_elf_special_section csky_elf_special_sections[]=
  2481. {
  2482. { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  2483. { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  2484. { NULL, 0, 0, 0, 0 }
  2485. };
  2486. /* Function to keep CSKY specific flags in the ELF header. */
  2487. static bool
  2488. csky_elf_set_private_flags (bfd * abfd, flagword flags)
  2489. {
  2490. BFD_ASSERT (! elf_flags_init (abfd)
  2491. || elf_elfheader (abfd)->e_flags == flags);
  2492. elf_elfheader (abfd)->e_flags = flags;
  2493. elf_flags_init (abfd) = true;
  2494. return true;
  2495. }
  2496. static csky_arch_for_merge *
  2497. csky_find_arch_with_eflag (const unsigned long arch_eflag)
  2498. {
  2499. csky_arch_for_merge *csky_arch = NULL;
  2500. for (csky_arch = csky_archs; csky_arch->name != NULL; csky_arch++)
  2501. if (csky_arch->arch_eflag == arch_eflag)
  2502. break;
  2503. if (csky_arch == NULL)
  2504. {
  2505. _bfd_error_handler (_("warning: unrecognized arch eflag '%#lx'"),
  2506. arch_eflag);
  2507. bfd_set_error (bfd_error_wrong_format);
  2508. }
  2509. return csky_arch;
  2510. }
  2511. static csky_arch_for_merge *
  2512. csky_find_arch_with_name (const char *name)
  2513. {
  2514. csky_arch_for_merge *csky_arch = NULL;
  2515. const char *msg;
  2516. if (name == NULL)
  2517. return NULL;
  2518. for (csky_arch = csky_archs; csky_arch->name != NULL; csky_arch++)
  2519. {
  2520. if (strncmp (csky_arch->name, name, strlen (csky_arch->name)) == 0)
  2521. break;
  2522. }
  2523. if (csky_arch == NULL)
  2524. {
  2525. msg = _("warning: unrecognised arch name '%#x'");
  2526. (*_bfd_error_handler) (msg, name);
  2527. bfd_set_error (bfd_error_wrong_format);
  2528. }
  2529. return csky_arch;
  2530. }
  2531. static bool
  2532. elf32_csky_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
  2533. {
  2534. bfd *obfd = info->output_bfd;
  2535. obj_attribute *in_attr;
  2536. obj_attribute *out_attr;
  2537. obj_attribute tattr;
  2538. csky_arch_for_merge *old_arch = NULL;
  2539. csky_arch_for_merge *new_arch = NULL;
  2540. int i;
  2541. bool result = true;
  2542. const char *msg = NULL;
  2543. const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
  2544. /* Skip the linker stubs file. This preserves previous behavior
  2545. of accepting unknown attributes in the first input file - but
  2546. is that a bug? */
  2547. if (ibfd->flags & BFD_LINKER_CREATED)
  2548. return true;
  2549. /* Skip any input that hasn't attribute section.
  2550. This enables to link object files without attribute section with
  2551. any others. */
  2552. if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
  2553. {
  2554. return true;
  2555. }
  2556. if (!elf_known_obj_attributes_proc (obfd)[0].i)
  2557. {
  2558. /* This is the first object. Copy the attributes. */
  2559. out_attr = elf_known_obj_attributes_proc (obfd);
  2560. /* If Tag_CSKY_CPU_NAME is already set, save it. */
  2561. memcpy (&tattr, &out_attr[Tag_CSKY_ARCH_NAME], sizeof (tattr));
  2562. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  2563. out_attr = elf_known_obj_attributes_proc (obfd);
  2564. /* Restore Tag_CSKY_CPU_NAME. */
  2565. memcpy (&out_attr[Tag_CSKY_ARCH_NAME], &tattr, sizeof (tattr));
  2566. /* Use the Tag_null value to indicate the attributes have been
  2567. initialized. */
  2568. out_attr[0].i = 1;
  2569. }
  2570. in_attr = elf_known_obj_attributes_proc (ibfd);
  2571. out_attr = elf_known_obj_attributes_proc (obfd);
  2572. for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
  2573. {
  2574. /* Merge this attribute with existing attributes. */
  2575. switch (i)
  2576. {
  2577. case Tag_CSKY_CPU_NAME:
  2578. case Tag_CSKY_ARCH_NAME:
  2579. /* Do arch merge. */
  2580. new_arch = csky_find_arch_with_name (in_attr[Tag_CSKY_ARCH_NAME].s);
  2581. old_arch = csky_find_arch_with_name (out_attr[Tag_CSKY_ARCH_NAME].s);
  2582. if (new_arch != NULL && old_arch != NULL)
  2583. {
  2584. if (new_arch->class != old_arch->class)
  2585. {
  2586. msg = _("%pB: machine flag conflict with target");
  2587. (*_bfd_error_handler) (msg, ibfd);
  2588. bfd_set_error (bfd_error_wrong_format);
  2589. return false;
  2590. }
  2591. else if (new_arch->class_level != old_arch->class_level)
  2592. {
  2593. csky_arch_for_merge *newest_arch =
  2594. ((new_arch->class_level > old_arch->class_level) ?
  2595. new_arch : old_arch);
  2596. if (new_arch->do_warning || old_arch->do_warning)
  2597. {
  2598. msg = _("warning: file %pB's arch flag %s conflict "
  2599. "with target %s,set target arch flag to %s");
  2600. (*_bfd_error_handler) (msg, ibfd, new_arch->name,
  2601. old_arch->name,
  2602. (newest_arch->name));
  2603. bfd_set_error (bfd_error_wrong_format);
  2604. }
  2605. if (out_attr[Tag_CSKY_ARCH_NAME].s != NULL)
  2606. bfd_release (obfd, out_attr[Tag_CSKY_ARCH_NAME].s);
  2607. out_attr[Tag_CSKY_ARCH_NAME].s =
  2608. _bfd_elf_attr_strdup (obfd, newest_arch->name);
  2609. }
  2610. }
  2611. break;
  2612. case Tag_CSKY_ISA_FLAGS:
  2613. case Tag_CSKY_ISA_EXT_FLAGS:
  2614. /* Do ISA merge. */
  2615. break;
  2616. case Tag_CSKY_VDSP_VERSION:
  2617. if (out_attr[i].i == 0)
  2618. out_attr[i].i = in_attr[i].i;
  2619. else if (out_attr[i].i != in_attr[i].i)
  2620. {
  2621. _bfd_error_handler
  2622. (_("Error: %pB and %pB has different VDSP version"), ibfd, obfd);
  2623. result = false;
  2624. }
  2625. break;
  2626. case Tag_CSKY_FPU_VERSION:
  2627. if (out_attr[i].i <= in_attr[i].i
  2628. && out_attr[i].i == 0)
  2629. out_attr[i].i = in_attr[i].i;
  2630. break;
  2631. case Tag_CSKY_DSP_VERSION:
  2632. if (out_attr[i].i == 0)
  2633. out_attr[i].i = in_attr[i].i;
  2634. else if (out_attr[i].i != in_attr[i].i)
  2635. {
  2636. _bfd_error_handler
  2637. (_("Error: %pB and %pB has different DSP version"), ibfd, obfd);
  2638. result = false;
  2639. }
  2640. break;
  2641. case Tag_CSKY_FPU_ABI:
  2642. if (out_attr[i].i != in_attr[i].i
  2643. && (out_attr[i].i == 0
  2644. || (out_attr[i].i == VAL_CSKY_FPU_ABI_SOFT
  2645. && in_attr[i].i == VAL_CSKY_FPU_ABI_SOFTFP)))
  2646. {
  2647. out_attr[i].i = in_attr[i].i;
  2648. }
  2649. else if (out_attr[i].i == VAL_CSKY_FPU_ABI_HARD
  2650. && (out_attr[i].i != in_attr[i].i
  2651. && in_attr[i].i != 0))
  2652. {
  2653. _bfd_error_handler
  2654. (_("Error: %pB and %pB has different FPU ABI"), ibfd, obfd);
  2655. result = false;
  2656. }
  2657. break;
  2658. default:
  2659. result =
  2660. result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
  2661. break;
  2662. }
  2663. /* If out_attr was copied from in_attr then it won't have a type yet. */
  2664. if (in_attr[i].type && !out_attr[i].type)
  2665. out_attr[i].type = in_attr[i].type;
  2666. }
  2667. /* Merge Tag_compatibility attributes and any common GNU ones. */
  2668. if (!_bfd_elf_merge_object_attributes (ibfd, info))
  2669. return false;
  2670. /* Check for any attributes not known on CSKY. */
  2671. result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
  2672. return result;
  2673. }
  2674. /* Merge backend specific data from an object file to the output
  2675. object file when linking. */
  2676. static bool
  2677. csky_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  2678. {
  2679. bfd *obfd = info->output_bfd;
  2680. flagword old_flags;
  2681. flagword new_flags;
  2682. csky_arch_for_merge *old_arch = NULL;
  2683. csky_arch_for_merge *new_arch = NULL;
  2684. flagword newest_flag = 0;
  2685. const char *sec_name;
  2686. obj_attribute *out_attr;
  2687. /* Check if we have the same endianness. */
  2688. if (! _bfd_generic_verify_endian_match (ibfd, info))
  2689. return false;
  2690. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  2691. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  2692. return true;
  2693. /* Merge ".csky.attribute" section. */
  2694. if (!elf32_csky_merge_attributes (ibfd, info))
  2695. return false;
  2696. if (! elf_flags_init (obfd))
  2697. {
  2698. /* First call, no flags set. */
  2699. elf_flags_init (obfd) = true;
  2700. }
  2701. /* Try to merge e_flag. */
  2702. new_flags = elf_elfheader (ibfd)->e_flags;
  2703. old_flags = elf_elfheader (obfd)->e_flags;
  2704. out_attr = elf_known_obj_attributes_proc (obfd);
  2705. /* the flags like"e , f ,g ..." , we take collection. */
  2706. newest_flag = (old_flags & (~CSKY_ARCH_MASK))
  2707. | (new_flags & (~CSKY_ARCH_MASK));
  2708. sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
  2709. if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
  2710. {
  2711. /* Input BFDs have no ".csky.attribute" section. */
  2712. new_arch = csky_find_arch_with_eflag (new_flags & CSKY_ARCH_MASK);
  2713. old_arch = csky_find_arch_with_name (out_attr[Tag_CSKY_ARCH_NAME].s);
  2714. if (new_arch != NULL && old_arch != NULL)
  2715. {
  2716. if (new_arch->class != old_arch->class)
  2717. {
  2718. _bfd_error_handler
  2719. /* xgettext:c-format */
  2720. (_("%pB: machine flag conflict with target"), ibfd);
  2721. bfd_set_error (bfd_error_wrong_format);
  2722. return false;
  2723. }
  2724. else if (new_arch->class_level != old_arch->class_level)
  2725. {
  2726. csky_arch_for_merge *newest_arch =
  2727. (new_arch->class_level > old_arch->class_level
  2728. ? new_arch : old_arch);
  2729. if (new_arch->do_warning || old_arch->do_warning)
  2730. {
  2731. _bfd_error_handler
  2732. /* xgettext:c-format */
  2733. (_("warning: file %pB's arch flag %s conflicts with "
  2734. "target ck%s, using %s"),
  2735. ibfd, new_arch->name, old_arch->name,
  2736. newest_arch->name);
  2737. bfd_set_error (bfd_error_wrong_format);
  2738. }
  2739. if (out_attr[Tag_CSKY_ARCH_NAME].s != NULL)
  2740. bfd_release (obfd, out_attr[Tag_CSKY_ARCH_NAME].s);
  2741. out_attr[Tag_CSKY_ARCH_NAME].s =
  2742. _bfd_elf_attr_strdup (obfd, newest_arch->name);
  2743. }
  2744. else
  2745. newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
  2746. | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
  2747. }
  2748. else
  2749. {
  2750. if (new_arch && new_arch->name != NULL)
  2751. out_attr[Tag_CSKY_ARCH_NAME].s =
  2752. _bfd_elf_attr_strdup (obfd, new_arch->name);
  2753. }
  2754. }
  2755. elf_elfheader (obfd)->e_flags = newest_flag;
  2756. return true;
  2757. }
  2758. /* Ignore the discarded relocs in special sections in link time. */
  2759. static bool
  2760. csky_elf_ignore_discarded_relocs (asection *sec)
  2761. {
  2762. if (strcmp (sec->name, ".csky_stack_size") == 0)
  2763. return true;
  2764. return false;
  2765. }
  2766. /* .csky_stack_size are not referenced directly. This pass marks all of
  2767. them as required. */
  2768. static bool
  2769. elf32_csky_gc_mark_extra_sections (struct bfd_link_info *info,
  2770. elf_gc_mark_hook_fn gc_mark_hook ATTRIBUTE_UNUSED)
  2771. {
  2772. bfd *sub;
  2773. _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
  2774. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  2775. {
  2776. asection *o;
  2777. for (o = sub->sections; o != NULL; o = o->next)
  2778. if (strcmp (o->name, ".csky_stack_size") == 0)
  2779. o->gc_mark = 1;
  2780. }
  2781. return true;
  2782. }
  2783. /* The linker repeatedly calls this function for each input section,
  2784. in the order that input sections are linked into output sections.
  2785. Build lists of input sections to determine groupings between which
  2786. we may insert linker stubs. */
  2787. void
  2788. elf32_csky_next_input_section (struct bfd_link_info *info,
  2789. asection *isec)
  2790. {
  2791. struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
  2792. if (htab == NULL)
  2793. return;
  2794. if (isec->output_section->index <= htab->top_index)
  2795. {
  2796. asection **list = htab->input_list + isec->output_section->index;
  2797. if (*list != bfd_abs_section_ptr)
  2798. {
  2799. /* Steal the link_sec pointer for our list. */
  2800. #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
  2801. /* This happens to make the list in reverse order,
  2802. which we reverse later in group_sections. */
  2803. PREV_SEC (isec) = *list;
  2804. *list = isec;
  2805. }
  2806. }
  2807. }
  2808. /* See whether we can group stub sections together. Grouping stub
  2809. sections may result in fewer stubs. More importantly, we need to
  2810. put all .init* and .fini* stubs at the end of the .init or
  2811. .fini output sections respectively, because glibc splits the
  2812. _init and _fini functions into multiple parts. Putting a stub in
  2813. the middle of a function is not a good idea. */
  2814. static void
  2815. group_sections (struct csky_elf_link_hash_table *htab,
  2816. bfd_size_type stub_group_size,
  2817. bool stubs_always_after_branch)
  2818. {
  2819. asection **list = htab->input_list;
  2820. do
  2821. {
  2822. asection *tail = *list;
  2823. asection *head;
  2824. if (tail == bfd_abs_section_ptr)
  2825. continue;
  2826. /* Reverse the list: we must avoid placing stubs at the
  2827. beginning of the section because the beginning of the text
  2828. section may be required for an interrupt vector in bare metal
  2829. code. */
  2830. #define NEXT_SEC PREV_SEC
  2831. head = NULL;
  2832. while (tail != NULL)
  2833. {
  2834. /* Pop from tail. */
  2835. asection *item = tail;
  2836. tail = PREV_SEC (item);
  2837. /* Push on head. */
  2838. NEXT_SEC (item) = head;
  2839. head = item;
  2840. }
  2841. while (head != NULL)
  2842. {
  2843. asection *curr;
  2844. asection *next;
  2845. bfd_vma stub_group_start = head->output_offset;
  2846. bfd_vma end_of_next;
  2847. curr = head;
  2848. while (NEXT_SEC (curr) != NULL)
  2849. {
  2850. next = NEXT_SEC (curr);
  2851. end_of_next = next->output_offset + next->size;
  2852. if (end_of_next - stub_group_start >= stub_group_size)
  2853. /* End of NEXT is too far from start, so stop. */
  2854. break;
  2855. curr = next;
  2856. }
  2857. /* OK, the size from the start to the start of CURR is less
  2858. * than stub_group_size and thus can be handled by one stub
  2859. * section. (Or the head section is itself larger than
  2860. * stub_group_size, in which case we may be toast.)
  2861. * We should really be keeping track of the total size of
  2862. * stubs added here, as stubs contribute to the final output
  2863. * section size. */
  2864. do
  2865. {
  2866. next = NEXT_SEC (head);
  2867. /* Set up this stub group. */
  2868. htab->stub_group[head->id].link_sec = curr;
  2869. }
  2870. while (head != curr && (head = next) != NULL);
  2871. /* But wait, there's more! Input sections up to stub_group_size
  2872. * bytes after the stub section can be handled by it too. */
  2873. if (!stubs_always_after_branch)
  2874. {
  2875. stub_group_start = curr->output_offset + curr->size;
  2876. while (next != NULL)
  2877. {
  2878. end_of_next = next->output_offset + next->size;
  2879. if (end_of_next - stub_group_start >= stub_group_size)
  2880. /* End of NEXT is too far from stubs, so stop. */
  2881. break;
  2882. /* Add NEXT to the stub group. */
  2883. head = next;
  2884. next = NEXT_SEC (head);
  2885. htab->stub_group[head->id].link_sec = curr;
  2886. }
  2887. }
  2888. head = next;
  2889. }
  2890. }
  2891. while (list++ != htab->input_list + htab->top_index);
  2892. free (htab->input_list);
  2893. #undef PREV_SEC
  2894. #undef NEXT_SEC
  2895. }
  2896. /* If the symbol referenced by bsr is defined in shared object file,
  2897. or it is a weak symbol and we aim to create shared object file,
  2898. we must create a stub for this bsr. */
  2899. static bool
  2900. sym_must_create_stub (struct elf_link_hash_entry *h,
  2901. struct bfd_link_info *info)
  2902. {
  2903. if (h != NULL
  2904. && ((h->def_dynamic && !h->def_regular)
  2905. || (bfd_link_pic (info) && h->root.type == bfd_link_hash_defweak)))
  2906. return true;
  2907. else
  2908. return false;
  2909. }
  2910. /* Calculate the template, template size and instruction size for a stub.
  2911. Return value is the instruction size. */
  2912. static unsigned int
  2913. find_stub_size_and_template (enum elf32_csky_stub_type stub_type,
  2914. const insn_sequence **stub_template,
  2915. int *stub_template_size)
  2916. {
  2917. const insn_sequence *template_sequence = NULL;
  2918. int template_size = 0;
  2919. int i;
  2920. unsigned int size;
  2921. template_sequence = stub_definitions[stub_type].template_sequence;
  2922. template_size = stub_definitions[stub_type].template_size;
  2923. size = 0;
  2924. for (i = 0; i < template_size; i++)
  2925. {
  2926. switch (template_sequence[i].type)
  2927. {
  2928. case INSN16:
  2929. size += 2;
  2930. break;
  2931. case INSN32:
  2932. case DATA_TYPE:
  2933. size += 4;
  2934. break;
  2935. default:
  2936. BFD_FAIL ();
  2937. return false;
  2938. }
  2939. }
  2940. if (stub_template)
  2941. *stub_template = template_sequence;
  2942. if (stub_template_size)
  2943. *stub_template_size = template_size;
  2944. return size;
  2945. }
  2946. /* As above, but don't actually build the stub. Just bump offset so
  2947. we know stub section sizes. */
  2948. static bool
  2949. csky_size_one_stub (struct bfd_hash_entry *gen_entry,
  2950. void * in_arg ATTRIBUTE_UNUSED)
  2951. {
  2952. struct elf32_csky_stub_hash_entry *stub_entry;
  2953. const insn_sequence *template_sequence = NULL;
  2954. int template_size = 0;
  2955. int size = 0;
  2956. /* Massage our args to the form they really have. */
  2957. stub_entry = (struct elf32_csky_stub_hash_entry *) gen_entry;
  2958. BFD_ASSERT (stub_entry->stub_type > csky_stub_none
  2959. && stub_entry->stub_type < ARRAY_SIZE (stub_definitions));
  2960. size = find_stub_size_and_template (stub_entry->stub_type,
  2961. &template_sequence, &template_size);
  2962. stub_entry->stub_size = size;
  2963. stub_entry->stub_template = template_sequence;
  2964. stub_entry->stub_template_size = template_size;
  2965. size = (size + 7) & ~7;
  2966. stub_entry->stub_sec->size += size;
  2967. return true;
  2968. }
  2969. /* Add a new stub entry to the stub hash. Not all fields of the new
  2970. stub entry are initialised. */
  2971. static struct elf32_csky_stub_hash_entry *
  2972. elf32_csky_add_stub (const char *stub_name,
  2973. asection *section,
  2974. struct csky_elf_link_hash_table *htab)
  2975. {
  2976. asection *link_sec;
  2977. asection *stub_sec;
  2978. struct elf32_csky_stub_hash_entry *stub_entry;
  2979. stub_sec = elf32_csky_create_or_find_stub_sec (&link_sec, section, htab);
  2980. if (stub_sec == NULL)
  2981. return NULL;
  2982. /* Enter this entry into the linker stub hash table. */
  2983. stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table, stub_name,
  2984. true, false);
  2985. if (stub_entry == NULL)
  2986. {
  2987. _bfd_error_handler (_("%pB: cannot create stub entry %s"),
  2988. section->owner, stub_name);
  2989. return NULL;
  2990. }
  2991. stub_entry->stub_sec = stub_sec;
  2992. stub_entry->stub_offset = 0;
  2993. stub_entry->id_sec = link_sec;
  2994. return stub_entry;
  2995. }
  2996. /* Determine and set the size of the stub section for a final link.
  2997. The basic idea here is to examine all the relocations looking for
  2998. PC-relative calls to a target that is unreachable with a "bsr"
  2999. instruction. */
  3000. bool
  3001. elf32_csky_size_stubs (bfd *output_bfd,
  3002. bfd *stub_bfd,
  3003. struct bfd_link_info *info,
  3004. bfd_signed_vma group_size,
  3005. asection *(*add_stub_section) (const char*, asection*),
  3006. void (*layout_sections_again) (void))
  3007. {
  3008. bfd_size_type stub_group_size;
  3009. bool stubs_always_after_branch;
  3010. struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
  3011. if (htab == NULL)
  3012. return false;
  3013. /* Propagate mach to stub bfd, because it may not have been
  3014. finalized when we created stub_bfd. */
  3015. bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
  3016. bfd_get_mach (output_bfd));
  3017. /* Stash our params away. */
  3018. htab->stub_bfd = stub_bfd;
  3019. htab->add_stub_section = add_stub_section;
  3020. htab->layout_sections_again = layout_sections_again;
  3021. stubs_always_after_branch = group_size < 0;
  3022. if (group_size < 0)
  3023. stub_group_size = -group_size;
  3024. else
  3025. stub_group_size = group_size;
  3026. if (stub_group_size == 1)
  3027. /* The 'bsr' range in abiv2 is +-64MB has to be used as the
  3028. default maximum size.
  3029. This value is 128K less than that, which allows for 131072
  3030. byte stubs. If we exceed that, then we will fail to link.
  3031. The user will have to relink with an explicit group size
  3032. option. */
  3033. stub_group_size = 66977792;
  3034. group_sections (htab, stub_group_size, stubs_always_after_branch);
  3035. while (1)
  3036. {
  3037. bfd *input_bfd;
  3038. unsigned int bfd_indx;
  3039. asection *stub_sec;
  3040. bool stub_changed = false;
  3041. for (input_bfd = info->input_bfds, bfd_indx = 0;
  3042. input_bfd != NULL;
  3043. input_bfd = input_bfd->link.next, bfd_indx++)
  3044. {
  3045. Elf_Internal_Shdr *symtab_hdr;
  3046. asection *section;
  3047. Elf_Internal_Sym *local_syms = NULL;
  3048. /* We'll need the symbol table in a second. */
  3049. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  3050. if (symtab_hdr->sh_info == 0)
  3051. continue;
  3052. /* Walk over each section attached to the input bfd. */
  3053. for (section = input_bfd->sections;
  3054. section != NULL;
  3055. section = section->next)
  3056. {
  3057. Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  3058. /* If there aren't any relocs, then there's nothing more
  3059. * to do. */
  3060. if ((section->flags & SEC_RELOC) == 0
  3061. || section->reloc_count == 0
  3062. || (section->flags & SEC_CODE) == 0)
  3063. continue;
  3064. /* If this section is a link-once section that will be
  3065. discarded, then don't create any stubs. */
  3066. if (section->output_section == NULL
  3067. || section->output_section->owner != output_bfd)
  3068. continue;
  3069. /* Get the relocs. */
  3070. internal_relocs = _bfd_elf_link_read_relocs (input_bfd,
  3071. section,
  3072. NULL, NULL,
  3073. info->keep_memory);
  3074. if (internal_relocs == NULL)
  3075. goto error_ret_free_local;
  3076. /* Now examine each relocation. */
  3077. irela = internal_relocs;
  3078. irelaend = irela + section->reloc_count;
  3079. for (; irela < irelaend; irela++)
  3080. {
  3081. unsigned int r_type, r_indx;
  3082. enum elf32_csky_stub_type stub_type;
  3083. struct elf32_csky_stub_hash_entry *stub_entry;
  3084. asection *sym_sec;
  3085. bfd_vma sym_value;
  3086. bfd_vma destination;
  3087. struct csky_elf_link_hash_entry *hash;
  3088. const char *sym_name;
  3089. char *stub_name;
  3090. const asection *id_sec;
  3091. unsigned char st_type;
  3092. r_type = ELF32_R_TYPE (irela->r_info);
  3093. r_indx = ELF32_R_SYM (irela->r_info);
  3094. if (r_type >= (unsigned int) R_CKCORE_MAX)
  3095. {
  3096. bfd_set_error (bfd_error_bad_value);
  3097. error_ret_free_internal:
  3098. if (elf_section_data (section)->relocs == NULL)
  3099. free (internal_relocs);
  3100. goto error_ret_free_local;
  3101. }
  3102. /* Only look for stubs on branch instructions. */
  3103. if (r_type != (unsigned int) R_CKCORE_PCREL_IMM26BY2)
  3104. continue;
  3105. /* Now determine the call target, its name, value,
  3106. section. */
  3107. sym_sec = NULL;
  3108. sym_value = 0;
  3109. destination = 0;
  3110. hash = NULL;
  3111. sym_name = NULL;
  3112. if (r_indx < symtab_hdr->sh_info)
  3113. {
  3114. /* It's a local symbol. */
  3115. Elf_Internal_Sym *sym;
  3116. Elf_Internal_Shdr *hdr;
  3117. if (local_syms == NULL)
  3118. local_syms =
  3119. (Elf_Internal_Sym *) symtab_hdr->contents;
  3120. if (local_syms == NULL)
  3121. {
  3122. local_syms =
  3123. bfd_elf_get_elf_syms (input_bfd,
  3124. symtab_hdr,
  3125. symtab_hdr->sh_info,
  3126. 0, NULL, NULL, NULL);
  3127. if (local_syms == NULL)
  3128. goto error_ret_free_internal;
  3129. }
  3130. sym = local_syms + r_indx;
  3131. hdr = elf_elfsections (input_bfd)[sym->st_shndx];
  3132. sym_sec = hdr->bfd_section;
  3133. if (!sym_sec)
  3134. /* This is an undefined symbol. It can never
  3135. be resolved. */
  3136. continue;
  3137. if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  3138. sym_value = sym->st_value;
  3139. destination = (sym_value + irela->r_addend
  3140. + sym_sec->output_offset
  3141. + sym_sec->output_section->vma);
  3142. st_type = ELF_ST_TYPE (sym->st_info);
  3143. sym_name =
  3144. bfd_elf_string_from_elf_section (input_bfd,
  3145. symtab_hdr->sh_link,
  3146. sym->st_name);
  3147. }
  3148. else
  3149. {
  3150. /* It's an external symbol. */
  3151. int e_indx;
  3152. e_indx = r_indx - symtab_hdr->sh_info;
  3153. hash = ((struct csky_elf_link_hash_entry *)
  3154. elf_sym_hashes (input_bfd)[e_indx]);
  3155. while (hash->elf.root.type == bfd_link_hash_indirect
  3156. || hash->elf.root.type == bfd_link_hash_warning)
  3157. hash = ((struct csky_elf_link_hash_entry *)
  3158. hash->elf.root.u.i.link);
  3159. if (hash->elf.root.type == bfd_link_hash_defined
  3160. || hash->elf.root.type == bfd_link_hash_defweak)
  3161. {
  3162. sym_sec = hash->elf.root.u.def.section;
  3163. sym_value = hash->elf.root.u.def.value;
  3164. struct csky_elf_link_hash_table *globals =
  3165. csky_elf_hash_table (info);
  3166. /* FIXME For a destination in a shared library. */
  3167. if (globals->elf.splt != NULL && hash != NULL
  3168. && hash->elf.plt.offset != (bfd_vma) -1)
  3169. continue;
  3170. else if (sym_sec->output_section != NULL)
  3171. destination = (sym_value + irela->r_addend
  3172. + sym_sec->output_offset
  3173. + sym_sec->output_section->vma);
  3174. }
  3175. else if (hash->elf.root.type == bfd_link_hash_undefined
  3176. || (hash->elf.root.type
  3177. == bfd_link_hash_undefweak))
  3178. /* FIXME For a destination in a shared library. */
  3179. continue;
  3180. else
  3181. {
  3182. bfd_set_error (bfd_error_bad_value);
  3183. goto error_ret_free_internal;
  3184. }
  3185. st_type = ELF_ST_TYPE (hash->elf.type);
  3186. sym_name = hash->elf.root.root.string;
  3187. }
  3188. do
  3189. {
  3190. /* Determine what (if any) linker stub is needed. */
  3191. stub_type = csky_type_of_stub (info, section, irela,
  3192. st_type, hash,
  3193. destination, sym_sec,
  3194. input_bfd, sym_name);
  3195. if (stub_type == csky_stub_none)
  3196. break;
  3197. /* Support for grouping stub sections. */
  3198. id_sec = htab->stub_group[section->id].link_sec;
  3199. /* Get the name of this stub. */
  3200. stub_name = elf32_csky_stub_name (id_sec, sym_sec, hash,
  3201. irela);
  3202. if (!stub_name)
  3203. goto error_ret_free_internal;
  3204. /* We've either created a stub for this reloc already,
  3205. or we are about to. */
  3206. stub_entry
  3207. = csky_stub_hash_lookup (&htab->stub_hash_table,
  3208. stub_name,
  3209. false, false);
  3210. if (stub_entry != NULL)
  3211. {
  3212. /* The proper stub has already been created. */
  3213. free (stub_name);
  3214. stub_entry->target_value = sym_value;
  3215. break;
  3216. }
  3217. stub_entry = elf32_csky_add_stub (stub_name, section,
  3218. htab);
  3219. if (stub_entry == NULL)
  3220. {
  3221. free (stub_name);
  3222. goto error_ret_free_internal;
  3223. }
  3224. stub_entry->target_value = sym_value;
  3225. stub_entry->target_section = sym_sec;
  3226. stub_entry->stub_type = stub_type;
  3227. stub_entry->h = hash;
  3228. stub_entry->st_type = st_type;
  3229. if (sym_name == NULL)
  3230. sym_name = "unnamed";
  3231. stub_entry->output_name =
  3232. bfd_alloc (htab->stub_bfd,
  3233. (sizeof (STUB_ENTRY_NAME)
  3234. + strlen (sym_name)));
  3235. if (stub_entry->output_name == NULL)
  3236. {
  3237. free (stub_name);
  3238. goto error_ret_free_internal;
  3239. }
  3240. sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
  3241. sym_name);
  3242. stub_changed = true;
  3243. }
  3244. while (0);
  3245. }
  3246. /* We're done with the internal relocs, free them. */
  3247. if (elf_section_data (section)->relocs == NULL)
  3248. free (internal_relocs);
  3249. }
  3250. }
  3251. if (!stub_changed)
  3252. break;
  3253. /* OK, we've added some stubs. Find out the new size of the
  3254. stub sections. */
  3255. for (stub_sec = htab->stub_bfd->sections;
  3256. stub_sec != NULL;
  3257. stub_sec = stub_sec->next)
  3258. {
  3259. /* Ignore non-stub sections. */
  3260. if (!strstr (stub_sec->name, STUB_SUFFIX))
  3261. continue;
  3262. stub_sec->size = 0;
  3263. }
  3264. bfd_hash_traverse (&htab->stub_hash_table, csky_size_one_stub, htab);
  3265. /* Ask the linker to do its stuff. */
  3266. (*htab->layout_sections_again) ();
  3267. }
  3268. return true;
  3269. error_ret_free_local:
  3270. return false;
  3271. }
  3272. static bool
  3273. csky_build_one_stub (struct bfd_hash_entry *gen_entry,
  3274. void * in_arg)
  3275. {
  3276. #define MAXRELOCS 2
  3277. struct elf32_csky_stub_hash_entry *stub_entry;
  3278. struct bfd_link_info *info;
  3279. asection *stub_sec;
  3280. bfd *stub_bfd;
  3281. bfd_byte *loc;
  3282. bfd_vma sym_value;
  3283. int template_size;
  3284. int size;
  3285. const insn_sequence *template_sequence;
  3286. int i;
  3287. struct csky_elf_link_hash_table * globals;
  3288. int stub_reloc_idx[MAXRELOCS] = {-1, -1};
  3289. int stub_reloc_offset[MAXRELOCS] = {0, 0};
  3290. int nrelocs = 0;
  3291. struct elf_link_hash_entry *h = NULL;
  3292. /* Massage our args to the form they really have. */
  3293. stub_entry = (struct elf32_csky_stub_hash_entry *)gen_entry;
  3294. info = (struct bfd_link_info *) in_arg;
  3295. /* Fail if the target section could not be assigned to an output
  3296. section. The user should fix his linker script. */
  3297. if (stub_entry->target_section->output_section == NULL
  3298. && info->non_contiguous_regions)
  3299. info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
  3300. "Retry without --enable-non-contiguous-regions.\n"),
  3301. stub_entry->target_section);
  3302. globals = csky_elf_hash_table (info);
  3303. if (globals == NULL)
  3304. return false;
  3305. stub_sec = stub_entry->stub_sec;
  3306. /* Make a note of the offset within the stubs for this entry. */
  3307. stub_entry->stub_offset = stub_sec->size;
  3308. loc = stub_sec->contents + stub_entry->stub_offset;
  3309. stub_bfd = stub_sec->owner;
  3310. /* This is the address of the stub destination. */
  3311. h = &stub_entry->h->elf;
  3312. if (sym_must_create_stub (h, info)
  3313. && !(bfd_link_pic (info)
  3314. && h->root.type == bfd_link_hash_defweak
  3315. && h->def_regular
  3316. && !h->def_dynamic))
  3317. sym_value = 0;
  3318. else
  3319. sym_value = (stub_entry->target_value
  3320. + stub_entry->target_section->output_offset
  3321. + stub_entry->target_section->output_section->vma);
  3322. template_sequence = stub_entry->stub_template;
  3323. template_size = stub_entry->stub_template_size;
  3324. size = 0;
  3325. for (i = 0; i < template_size; i++)
  3326. switch (template_sequence[i].type)
  3327. {
  3328. case INSN16:
  3329. bfd_put_16 (stub_bfd, (bfd_vma) template_sequence[i].data,
  3330. loc + size);
  3331. size += 2;
  3332. break;
  3333. case INSN32:
  3334. csky_put_insn_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
  3335. loc + size);
  3336. size += 4;
  3337. break;
  3338. case DATA_TYPE:
  3339. bfd_put_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
  3340. loc + size);
  3341. stub_reloc_idx[nrelocs] = i;
  3342. stub_reloc_offset[nrelocs++] = size;
  3343. size += 4;
  3344. break;
  3345. default:
  3346. BFD_FAIL ();
  3347. return false;
  3348. }
  3349. stub_sec->size += size;
  3350. /* Stub size has already been computed in csky_size_one_stub. Check
  3351. consistency. */
  3352. BFD_ASSERT (size == stub_entry->stub_size);
  3353. /* Assume there is at least one and at most MAXRELOCS entries to relocate
  3354. in each stub. */
  3355. BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
  3356. for (i = 0; i < nrelocs; i++)
  3357. {
  3358. if (sym_must_create_stub (h, info))
  3359. {
  3360. Elf_Internal_Rela outrel;
  3361. asection * sreloc = globals->elf.srelgot;
  3362. outrel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
  3363. outrel.r_info =
  3364. ELF32_R_INFO (h->dynindx,
  3365. template_sequence[stub_reloc_idx[i]].r_type);
  3366. outrel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
  3367. loc = sreloc->contents;
  3368. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  3369. if (loc != NULL)
  3370. bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
  3371. }
  3372. _bfd_final_link_relocate (elf32_csky_howto_from_type
  3373. (template_sequence[stub_reloc_idx[i]].r_type),
  3374. stub_bfd, stub_sec, stub_sec->contents,
  3375. stub_entry->stub_offset + stub_reloc_offset[i],
  3376. sym_value + stub_entry->target_addend,
  3377. template_sequence[stub_reloc_idx[i]].reloc_addend);
  3378. }
  3379. return true;
  3380. #undef MAXRELOCS
  3381. }
  3382. /* Build all the stubs associated with the current output file. The
  3383. stubs are kept in a hash table attached to the main linker hash
  3384. table. We also set up the .plt entries for statically linked PIC
  3385. functions here. This function is called via arm_elf_finish in the
  3386. linker. */
  3387. bool
  3388. elf32_csky_build_stubs (struct bfd_link_info *info)
  3389. {
  3390. asection *stub_sec;
  3391. struct bfd_hash_table *table;
  3392. struct csky_elf_link_hash_table *htab;
  3393. htab = csky_elf_hash_table (info);
  3394. if (htab == NULL)
  3395. return false;
  3396. for (stub_sec = htab->stub_bfd->sections;
  3397. stub_sec != NULL;
  3398. stub_sec = stub_sec->next)
  3399. {
  3400. bfd_size_type size;
  3401. /* Ignore non-stub sections. */
  3402. if (!strstr (stub_sec->name, STUB_SUFFIX))
  3403. continue;
  3404. /* Allocate memory to hold the linker stubs. */
  3405. size = stub_sec->size;
  3406. stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
  3407. if (stub_sec->contents == NULL && size != 0)
  3408. return false;
  3409. stub_sec->size = 0;
  3410. }
  3411. /* Build the stubs as directed by the stub hash table. */
  3412. table = &htab->stub_hash_table;
  3413. bfd_hash_traverse (table, csky_build_one_stub, info);
  3414. return true;
  3415. }
  3416. /* Set up various things so that we can make a list of input sections
  3417. for each output section included in the link. Returns -1 on error,
  3418. 0 when no stubs will be needed, and 1 on success. */
  3419. int
  3420. elf32_csky_setup_section_lists (bfd *output_bfd,
  3421. struct bfd_link_info *info)
  3422. {
  3423. bfd *input_bfd;
  3424. unsigned int bfd_count;
  3425. unsigned int top_id, top_index;
  3426. asection *section;
  3427. asection **input_list, **list;
  3428. size_t amt;
  3429. struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
  3430. if (!htab)
  3431. return 0;
  3432. /* Count the number of input BFDs and find the top input section id. */
  3433. for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
  3434. input_bfd != NULL;
  3435. input_bfd = input_bfd->link.next)
  3436. {
  3437. bfd_count += 1;
  3438. for (section = input_bfd->sections;
  3439. section != NULL;
  3440. section = section->next)
  3441. if (top_id < section->id)
  3442. top_id = section->id;
  3443. }
  3444. htab->bfd_count = bfd_count;
  3445. amt = sizeof (struct map_stub) * (top_id + 1);
  3446. htab->stub_group = bfd_zmalloc (amt);
  3447. if (htab->stub_group == NULL)
  3448. return -1;
  3449. /* We can't use output_bfd->section_count here to find the top output
  3450. section index as some sections may have been removed, and
  3451. _bfd_strip_section_from_output doesn't renumber the indices. */
  3452. for (section = output_bfd->sections, top_index = 0;
  3453. section != NULL;
  3454. section = section->next)
  3455. if (top_index < section->index)
  3456. top_index = section->index;
  3457. htab->top_index = top_index;
  3458. amt = sizeof (asection *) * (top_index + 1);
  3459. input_list = bfd_malloc (amt);
  3460. htab->input_list = input_list;
  3461. if (input_list == NULL)
  3462. return -1;
  3463. /* For sections we aren't interested in, mark their entries with a
  3464. value we can check later. */
  3465. list = input_list + top_index;
  3466. do
  3467. *list = bfd_abs_section_ptr;
  3468. while (list-- != input_list);
  3469. for (section = output_bfd->sections;
  3470. section != NULL;
  3471. section = section->next)
  3472. if ((section->flags & SEC_CODE) != 0)
  3473. input_list[section->index] = NULL;
  3474. return 1;
  3475. }
  3476. static bfd_reloc_status_type
  3477. csky_relocate_contents (reloc_howto_type *howto,
  3478. bfd *input_bfd,
  3479. bfd_vma relocation,
  3480. bfd_byte *location)
  3481. {
  3482. int size;
  3483. bfd_vma x = 0;
  3484. bfd_reloc_status_type flag;
  3485. unsigned int rightshift = howto->rightshift;
  3486. unsigned int bitpos = howto->bitpos;
  3487. if (howto->negate)
  3488. relocation = -relocation;
  3489. /* FIXME: these macros should be defined at file head or head file head. */
  3490. #define CSKY_INSN_ADDI_TO_SUBI 0x04000000
  3491. #define CSKY_INSN_MOV_RTB 0xc41d4820 /* mov32 rx, r29, 0 */
  3492. #define CSKY_INSN_MOV_RDB 0xc41c4820 /* mov32 rx, r28, 0 */
  3493. #define CSKY_INSN_GET_ADDI_RZ(x) (((x) & 0x03e00000) >> 21)
  3494. #define CSKY_INSN_SET_MOV_RZ(x) ((x) & 0x0000001f)
  3495. #define CSKY_INSN_JSRI_TO_LRW 0xea9a0000
  3496. #define CSKY_INSN_JSR_R26 0xe8fa0000
  3497. /* Get the value we are going to relocate. */
  3498. size = bfd_get_reloc_size (howto);
  3499. switch (size)
  3500. {
  3501. default:
  3502. case 0:
  3503. abort ();
  3504. case 1:
  3505. x = bfd_get_8 (input_bfd, location);
  3506. break;
  3507. case 2:
  3508. x = bfd_get_16 (input_bfd, location);
  3509. break;
  3510. case 4:
  3511. if (need_reverse_bits)
  3512. {
  3513. x = csky_get_insn_32 (input_bfd, location);
  3514. if (R_CKCORE_DOFFSET_LO16 == howto->type)
  3515. {
  3516. if ((bfd_signed_vma) relocation < 0)
  3517. {
  3518. x |= CSKY_INSN_ADDI_TO_SUBI;
  3519. relocation = -relocation;
  3520. }
  3521. else if (0 == relocation)
  3522. x = (CSKY_INSN_MOV_RDB |
  3523. CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
  3524. }
  3525. else if (R_CKCORE_TOFFSET_LO16 == howto->type)
  3526. {
  3527. if ((bfd_signed_vma) relocation < 0)
  3528. {
  3529. x |= CSKY_INSN_ADDI_TO_SUBI;
  3530. relocation = -relocation;
  3531. }
  3532. else if (0 == relocation)
  3533. x = (CSKY_INSN_MOV_RTB |
  3534. CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
  3535. }
  3536. }
  3537. else
  3538. x = bfd_get_32 (input_bfd, location);
  3539. break;
  3540. }
  3541. /* Check for overflow. FIXME: We may drop bits during the addition
  3542. which we don't check for. We must either check at every single
  3543. operation, which would be tedious, or we must do the computations
  3544. in a type larger than bfd_vma, which would be inefficient. */
  3545. flag = bfd_reloc_ok;
  3546. if (howto->complain_on_overflow != complain_overflow_dont)
  3547. {
  3548. bfd_vma addrmask;
  3549. bfd_vma fieldmask;
  3550. bfd_vma signmask;
  3551. bfd_vma ss;
  3552. bfd_vma a;
  3553. bfd_vma b;
  3554. bfd_vma sum;
  3555. /* Get the values to be added together. For signed and unsigned
  3556. relocations, we assume that all values should be truncated to
  3557. the size of an address. For bitfields, all the bits matter.
  3558. See also bfd_check_overflow. */
  3559. #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
  3560. fieldmask = N_ONES (howto->bitsize);
  3561. signmask = ~fieldmask;
  3562. addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
  3563. a = (relocation & addrmask) >> rightshift;
  3564. if (read_content_substitute)
  3565. x = read_content_substitute;
  3566. b = (x & howto->src_mask & addrmask) >> bitpos;
  3567. switch (howto->complain_on_overflow)
  3568. {
  3569. case complain_overflow_signed:
  3570. /* If any sign bits are set, all sign bits must be set.
  3571. That is, A must be a valid negative address after
  3572. shifting. */
  3573. signmask = ~(fieldmask >> 1);
  3574. /* Fall through. */
  3575. case complain_overflow_bitfield:
  3576. /* Much like the signed check, but for a field one bit
  3577. wider. We allow a bitfield to represent numbers in the
  3578. range -2**n to 2**n-1, where n is the number of bits in the
  3579. field. Note that when bfd_vma is 32 bits, a 32-bit reloc
  3580. can't overflow, which is exactly what we want. */
  3581. ss = a & signmask;
  3582. if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
  3583. flag = bfd_reloc_overflow;
  3584. /* We only need this next bit of code if the sign bit of B
  3585. is below the sign bit of A. This would only happen if
  3586. SRC_MASK had fewer bits than BITSIZE. Note that if
  3587. SRC_MASK has more bits than BITSIZE, we can get into
  3588. trouble; we would need to verify that B is in range, as
  3589. we do for A above. */
  3590. ss = ((~howto->src_mask) >> 1) & howto->src_mask;
  3591. ss >>= bitpos;
  3592. /* Set all the bits above the sign bit. */
  3593. b = (b ^ ss) - ss;
  3594. /* Now we can do the addition. */
  3595. sum = a + b;
  3596. /* See if the result has the correct sign. Bits above the
  3597. sign bit are junk now; ignore them. If the sum is
  3598. positive, make sure we did not have all negative inputs;
  3599. if the sum is negative, make sure we did not have all
  3600. positive inputs. The test below looks only at the sign
  3601. bits, and it really just
  3602. SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
  3603. We mask with addrmask here to explicitly allow an address
  3604. wrap-around. The Linux kernel relies on it, and it is
  3605. the only way to write assembler code which can run when
  3606. loaded at a location 0x80000000 away from the location at
  3607. which it is linked. */
  3608. if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
  3609. flag = bfd_reloc_overflow;
  3610. break;
  3611. case complain_overflow_unsigned:
  3612. /* Checking for an unsigned overflow is relatively easy:
  3613. trim the addresses and add, and trim the result as well.
  3614. Overflow is normally indicated when the result does not
  3615. fit in the field. However, we also need to consider the
  3616. case when, e.g., fieldmask is 0x7fffffff or smaller, an
  3617. input is 0x80000000, and bfd_vma is only 32 bits; then we
  3618. will get sum == 0, but there is an overflow, since the
  3619. inputs did not fit in the field. Instead of doing a
  3620. separate test, we can check for this by or-ing in the
  3621. operands when testing for the sum overflowing its final
  3622. field. */
  3623. sum = (a + b) & addrmask;
  3624. if ((a | b | sum) & signmask)
  3625. flag = bfd_reloc_overflow;
  3626. break;
  3627. default:
  3628. abort ();
  3629. }
  3630. }
  3631. /* Put RELOCATION in the right bits. */
  3632. relocation >>= rightshift;
  3633. if ((howto->type == R_CKCORE_DOFFSET_LO16
  3634. || howto->type == R_CKCORE_TOFFSET_LO16)
  3635. && relocation == 0)
  3636. /* Do nothing lsli32 rx, rz, 0. */
  3637. ;
  3638. else
  3639. {
  3640. /* Fir V1, all this relocation must be x -1. */
  3641. if (howto->type == R_CKCORE_PCREL_IMM11BY2
  3642. || howto->type == R_CKCORE_PCREL_JSR_IMM11BY2
  3643. || howto->type == R_CKCORE_DOFFSET_LO16
  3644. || howto->type == R_CKCORE_TOFFSET_LO16)
  3645. relocation -= 1;
  3646. else if (howto->type == R_CKCORE_PCREL_IMM7BY4)
  3647. relocation = (relocation & 0x1f) + ((relocation << 3) & 0x300);
  3648. else if (howto->type == R_CKCORE_PCREL_FLRW_IMM8BY4)
  3649. relocation
  3650. = ((relocation << 4) & 0xf0) + ((relocation << 17) & 0x1e00000);
  3651. else if (howto->type == R_CKCORE_NOJSRI)
  3652. {
  3653. x = (x & howto->dst_mask) | CSKY_INSN_JSRI_TO_LRW;
  3654. relocation = 0;
  3655. csky_put_insn_32 (input_bfd, CSKY_INSN_JSR_R26, location + 4);
  3656. }
  3657. relocation <<= bitpos;
  3658. /* Add RELOCATION to the right bits of X. */
  3659. x = ((x & ~howto->dst_mask)
  3660. | (((x & howto->src_mask) + relocation) & howto->dst_mask));
  3661. }
  3662. /* Put the relocated value back in the object file. */
  3663. switch (size)
  3664. {
  3665. default:
  3666. abort ();
  3667. case 1:
  3668. bfd_put_8 (input_bfd, x, location);
  3669. break;
  3670. case 2:
  3671. bfd_put_16 (input_bfd, x, location);
  3672. break;
  3673. case 4:
  3674. if (need_reverse_bits)
  3675. csky_put_insn_32 (input_bfd, x, location);
  3676. else
  3677. bfd_put_32 (input_bfd, x, location);
  3678. break;
  3679. }
  3680. return flag;
  3681. }
  3682. /* Look up an entry in the stub hash. Stub entries are cached because
  3683. creating the stub name takes a bit of time. */
  3684. static struct elf32_csky_stub_hash_entry *
  3685. elf32_csky_get_stub_entry (const asection *input_section,
  3686. const asection *sym_sec,
  3687. struct elf_link_hash_entry *hash,
  3688. const Elf_Internal_Rela *rel,
  3689. struct csky_elf_link_hash_table *htab)
  3690. {
  3691. struct elf32_csky_stub_hash_entry *stub_entry;
  3692. struct csky_elf_link_hash_entry *h
  3693. = (struct csky_elf_link_hash_entry *) hash;
  3694. const asection *id_sec;
  3695. if ((input_section->flags & SEC_CODE) == 0)
  3696. return NULL;
  3697. /* If this input section is part of a group of sections sharing one
  3698. stub section, then use the id of the first section in the group.
  3699. Stub names need to include a section id, as there may well be
  3700. more than one stub used to reach say, printf, and we need to
  3701. distinguish between them. */
  3702. id_sec = htab->stub_group[input_section->id].link_sec;
  3703. if (h != NULL && h->stub_cache != NULL
  3704. && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
  3705. stub_entry = h->stub_cache;
  3706. else
  3707. {
  3708. char *stub_name;
  3709. stub_name = elf32_csky_stub_name (id_sec, sym_sec, h, rel);
  3710. if (stub_name == NULL)
  3711. return NULL;
  3712. stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table,
  3713. stub_name, false, false);
  3714. if (h != NULL)
  3715. h->stub_cache = stub_entry;
  3716. free (stub_name);
  3717. }
  3718. return stub_entry;
  3719. }
  3720. static bfd_reloc_status_type
  3721. csky_final_link_relocate (reloc_howto_type *howto,
  3722. bfd *input_bfd,
  3723. asection *input_section,
  3724. bfd_byte *contents,
  3725. bfd_vma address,
  3726. bfd_vma value,
  3727. bfd_vma addend)
  3728. {
  3729. bfd_vma relocation;
  3730. /* Sanity check the address. */
  3731. if (address > bfd_get_section_limit (input_bfd, input_section))
  3732. return bfd_reloc_outofrange;
  3733. /* This function assumes that we are dealing with a basic relocation
  3734. against a symbol. We want to compute the value of the symbol to
  3735. relocate to. This is just VALUE, the value of the symbol,
  3736. plus ADDEND, any addend associated with the reloc. */
  3737. relocation = value + addend;
  3738. /* If the relocation is PC relative, we want to set RELOCATION to
  3739. the distance between the symbol (currently in RELOCATION) and the
  3740. location we are relocating. Some targets (e.g., i386-aout)
  3741. arrange for the contents of the section to be the negative of the
  3742. offset of the location within the section; for such targets
  3743. pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
  3744. simply leave the contents of the section as zero; for such
  3745. targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
  3746. need to subtract out the offset of the location within the
  3747. section (which is just ADDRESS). */
  3748. if (howto->pc_relative)
  3749. {
  3750. relocation -= (input_section->output_section->vma
  3751. + input_section->output_offset);
  3752. if (howto->pcrel_offset)
  3753. relocation -= address;
  3754. }
  3755. return csky_relocate_contents (howto, input_bfd, relocation,
  3756. contents + address);
  3757. }
  3758. /* Return the base VMA address which should be subtracted from real addresses
  3759. when resolving @dtpoff relocation.
  3760. This is PT_TLS segment p_vaddr. */
  3761. static bfd_vma
  3762. dtpoff_base (struct bfd_link_info *info)
  3763. {
  3764. /* If tls_sec is NULL, we should have signalled an error already. */
  3765. if (elf_hash_table (info)->tls_sec == NULL)
  3766. return 0;
  3767. return elf_hash_table (info)->tls_sec->vma;
  3768. }
  3769. /* Return the relocation value for @tpoff relocation
  3770. if STT_TLS virtual address is ADDRESS. */
  3771. static bfd_vma
  3772. tpoff (struct bfd_link_info *info, bfd_vma address)
  3773. {
  3774. struct elf_link_hash_table *htab = elf_hash_table (info);
  3775. bfd_vma base;
  3776. /* If tls_sec is NULL, we should have signalled an error already. */
  3777. if (htab->tls_sec == NULL)
  3778. return 0;
  3779. base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
  3780. return address - htab->tls_sec->vma + base;
  3781. }
  3782. /* Relocate a csky section. */
  3783. static int
  3784. csky_elf_relocate_section (bfd * output_bfd,
  3785. struct bfd_link_info * info,
  3786. bfd * input_bfd,
  3787. asection * input_section,
  3788. bfd_byte * contents,
  3789. Elf_Internal_Rela * relocs,
  3790. Elf_Internal_Sym * local_syms,
  3791. asection ** local_sections)
  3792. {
  3793. Elf_Internal_Shdr *symtab_hdr;
  3794. struct elf_link_hash_entry **sym_hashes;
  3795. Elf_Internal_Rela *rel;
  3796. Elf_Internal_Rela *relend;
  3797. const char *name;
  3798. bool ret = true;
  3799. struct csky_elf_link_hash_table * htab;
  3800. bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
  3801. htab = csky_elf_hash_table (info);
  3802. if (htab == NULL)
  3803. return false;
  3804. symtab_hdr = & elf_symtab_hdr (input_bfd);
  3805. sym_hashes = elf_sym_hashes (input_bfd);
  3806. rel = relocs;
  3807. relend = relocs + input_section->reloc_count;
  3808. for (; rel < relend; rel++)
  3809. {
  3810. enum elf_csky_reloc_type r_type
  3811. = (enum elf_csky_reloc_type) ELF32_R_TYPE (rel->r_info);
  3812. unsigned long r_symndx;
  3813. reloc_howto_type *howto;
  3814. Elf_Internal_Sym *sym;
  3815. asection *sec;
  3816. bfd_vma relocation;
  3817. bfd_vma off;
  3818. struct elf_link_hash_entry * h;
  3819. bfd_vma addend = (bfd_vma)rel->r_addend;
  3820. bfd_reloc_status_type r = bfd_reloc_ok;
  3821. bool unresolved_reloc = false;
  3822. int do_final_relocate = true;
  3823. bool relative_reloc = false;
  3824. bfd_signed_vma disp;
  3825. /* Ignore these relocation types:
  3826. R_CKCORE_GNU_VTINHERIT, R_CKCORE_GNU_VTENTRY. */
  3827. if (r_type == R_CKCORE_GNU_VTINHERIT || r_type == R_CKCORE_GNU_VTENTRY)
  3828. continue;
  3829. if ((unsigned) r_type >= (unsigned) R_CKCORE_MAX)
  3830. {
  3831. /* The r_type is error, not support it. */
  3832. /* xgettext:c-format */
  3833. _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
  3834. input_bfd, r_type);
  3835. bfd_set_error (bfd_error_bad_value);
  3836. ret = false;
  3837. continue;
  3838. }
  3839. howto = &csky_elf_howto_table[(int) r_type];
  3840. r_symndx = ELF32_R_SYM(rel->r_info);
  3841. h = NULL;
  3842. sym = NULL;
  3843. sec = NULL;
  3844. unresolved_reloc = false;
  3845. if (r_symndx < symtab_hdr->sh_info)
  3846. {
  3847. /* Get symbol table entry. */
  3848. sym = local_syms + r_symndx;
  3849. sec = local_sections[r_symndx];
  3850. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  3851. addend = (bfd_vma)rel->r_addend;
  3852. }
  3853. else
  3854. {
  3855. bool warned, ignored;
  3856. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  3857. r_symndx, symtab_hdr, sym_hashes,
  3858. h, sec, relocation,
  3859. unresolved_reloc, warned, ignored);
  3860. }
  3861. if (sec != NULL && discarded_section (sec))
  3862. {
  3863. /* For relocs against symbols from removed linkonce sections,
  3864. or sections discarded by a linker script, we just want the
  3865. section contents zeroed. Avoid any special processing.
  3866. And if the symbol is referenced in '.csky_stack_size' section,
  3867. set the address to SEC_DISCARDED(0xffffffff). */
  3868. #if 0
  3869. /* The .csky_stack_size section is just for callgraph. */
  3870. if (strcmp (input_section->name, ".csky_stack_size") == 0)
  3871. {
  3872. /* FIXME: it should define in head file. */
  3873. #define SEC_DISCARDED 0xffffffff
  3874. bfd_put_32 (input_bfd, SEC_DISCARDED, contents + rel->r_offset);
  3875. rel->r_info = 0;
  3876. rel->r_addend = 0;
  3877. continue;
  3878. }
  3879. else
  3880. #endif
  3881. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3882. rel, 1, relend, howto, 0,
  3883. contents);
  3884. }
  3885. if (bfd_link_relocatable (info))
  3886. continue;
  3887. read_content_substitute = 0;
  3888. /* Final link. */
  3889. disp = (relocation
  3890. + (bfd_signed_vma) addend
  3891. - input_section->output_section->vma
  3892. - input_section->output_offset
  3893. - rel->r_offset);
  3894. /* It is for ck8xx. */
  3895. #define CSKY_INSN_BSR32 0xe0000000
  3896. /* It is for ck5xx/ck6xx. */
  3897. #define CSKY_INSN_BSR16 0xf800
  3898. #define within_range(x, L) (-(1 << (L - 1)) < (x) && (x) < (1 << (L -1)) - 2)
  3899. switch (howto->type)
  3900. {
  3901. case R_CKCORE_PCREL_IMM18BY2:
  3902. /* When h is NULL, means the instruction written as
  3903. grs rx, imm32
  3904. if the highest bit is set, prevent the high 32bits
  3905. turn to 0xffffffff when signed extern in 64bit
  3906. host machine. */
  3907. if (h == NULL && (addend & 0x80000000))
  3908. addend &= 0xffffffff;
  3909. break;
  3910. case R_CKCORE_PCREL32:
  3911. break;
  3912. case R_CKCORE_GOT12:
  3913. case R_CKCORE_PLT12:
  3914. case R_CKCORE_GOT_HI16:
  3915. case R_CKCORE_GOT_LO16:
  3916. case R_CKCORE_PLT_HI16:
  3917. case R_CKCORE_PLT_LO16:
  3918. case R_CKCORE_GOT32:
  3919. case R_CKCORE_GOT_IMM18BY4:
  3920. /* Relocation is to the entry for this symbol in the global
  3921. offset table. */
  3922. BFD_ASSERT (htab->elf.sgot != NULL);
  3923. if (h != NULL)
  3924. {
  3925. /* Global symbol is defined by other modules. */
  3926. bool dyn;
  3927. off = h->got.offset;
  3928. dyn = htab->elf.dynamic_sections_created;
  3929. if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  3930. bfd_link_pic (info), h)
  3931. || (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info,h))
  3932. || (ELF_ST_VISIBILITY(h->other)
  3933. && h->root.type == bfd_link_hash_undefweak))
  3934. {
  3935. /* This is actually a static link, or it is a
  3936. -Bsymbolic link and the symbol is defined
  3937. locally, or the symbol was forced to be local
  3938. because of a version file. We must initialize
  3939. this entry in the global offset table. Since the
  3940. offset must always be a multiple of 4, we use the
  3941. least significant bit to record whether we have
  3942. initialized it already.
  3943. When doing a dynamic link, we create a .rela.dyn
  3944. relocation entry to initialize the value. This
  3945. is done in the finish_dynamic_symbol routine. FIXME */
  3946. if (off & 1)
  3947. off &= ~1;
  3948. else
  3949. {
  3950. bfd_put_32 (output_bfd, relocation,
  3951. htab->elf.sgot->contents + off);
  3952. h->got.offset |= 1;
  3953. /* TRUE if relative relocation should be generated. GOT reference to
  3954. global symbol in PIC will lead to dynamic symbol. It becomes a
  3955. problem when "time" or "times" is defined as a variable in an
  3956. executable, clashing with functions of the same name in libc. If a
  3957. symbol isn't undefined weak symbol, don't make it dynamic in PIC and
  3958. generate relative relocation. */
  3959. #define GENERATE_RELATIVE_RELOC_P(INFO, H) \
  3960. ((H)->dynindx == -1 \
  3961. && !(H)->forced_local \
  3962. && (H)->root.type != bfd_link_hash_undefweak \
  3963. && bfd_link_pic (INFO))
  3964. if (GENERATE_RELATIVE_RELOC_P (info, h))
  3965. /* If this symbol isn't dynamic
  3966. in PIC, generate R_CKCORE_RELATIVE here. */
  3967. relative_reloc = true;
  3968. }
  3969. }
  3970. else
  3971. unresolved_reloc = false;
  3972. } /* End if h != NULL. */
  3973. else
  3974. {
  3975. BFD_ASSERT (local_got_offsets != NULL);
  3976. off = local_got_offsets[r_symndx];
  3977. /* The offset must always be a multiple of 4. We use
  3978. the least significant bit to record whether we have
  3979. already generated the necessary reloc. */
  3980. if (off & 1)
  3981. off &= ~1;
  3982. else
  3983. {
  3984. bfd_put_32 (output_bfd, relocation,
  3985. htab->elf.sgot->contents + off);
  3986. local_got_offsets[r_symndx] |= 1;
  3987. if (bfd_link_pic (info))
  3988. relative_reloc = true;
  3989. }
  3990. }
  3991. if (relative_reloc)
  3992. {
  3993. asection *srelgot;
  3994. Elf_Internal_Rela outrel;
  3995. bfd_byte *loc;
  3996. srelgot = htab->elf.srelgot;
  3997. BFD_ASSERT (srelgot != NULL);
  3998. outrel.r_offset
  3999. = (htab->elf.sgot->output_section->vma
  4000. + htab->elf.sgot->output_offset + off);
  4001. outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
  4002. outrel.r_addend = relocation;
  4003. loc = srelgot->contents;
  4004. loc += (srelgot->reloc_count++ * sizeof (Elf32_External_Rela));
  4005. if (loc != NULL)
  4006. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4007. }
  4008. relocation = htab->elf.sgot->output_offset + off;
  4009. break;
  4010. case R_CKCORE_GOTOFF_IMM18:
  4011. case R_CKCORE_GOTOFF:
  4012. case R_CKCORE_GOTOFF_HI16:
  4013. case R_CKCORE_GOTOFF_LO16:
  4014. /* Relocation is relative to the start of the global offset
  4015. table. */
  4016. /* Note that sgot->output_offset is not involved in this
  4017. calculation. We always want the start of .got. If we
  4018. defined _GLOBAL_OFFSET_TABLE in a different way, as is
  4019. permitted by the ABI, we might have to change this
  4020. calculation. */
  4021. relocation -= htab->elf.sgot->output_section->vma;
  4022. break;
  4023. case R_CKCORE_GOTPC:
  4024. case R_CKCORE_GOTPC_HI16:
  4025. case R_CKCORE_GOTPC_LO16:
  4026. /* Use global offset table as symbol value. */
  4027. relocation = htab->elf.sgot->output_section->vma;
  4028. addend = -addend;
  4029. unresolved_reloc = false;
  4030. break;
  4031. case R_CKCORE_DOFFSET_IMM18:
  4032. case R_CKCORE_DOFFSET_IMM18BY2:
  4033. case R_CKCORE_DOFFSET_IMM18BY4:
  4034. {
  4035. asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
  4036. relocation -= sdata->output_section->vma;
  4037. }
  4038. break;
  4039. case R_CKCORE_DOFFSET_LO16:
  4040. {
  4041. asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
  4042. relocation -= sdata->output_section->vma;
  4043. }
  4044. break;
  4045. case R_CKCORE_TOFFSET_LO16:
  4046. {
  4047. asection *stext = bfd_get_section_by_name (output_bfd, ".text");
  4048. if (stext)
  4049. relocation -= stext->output_section->vma;
  4050. }
  4051. break;
  4052. case R_CKCORE_PLT_IMM18BY4:
  4053. case R_CKCORE_PLT32:
  4054. /* Relocation is to the entry for this symbol in the
  4055. procedure linkage table. */
  4056. /* Resolve a PLT32 reloc against a local symbol directly,
  4057. without using the procedure linkage table. */
  4058. if (h == NULL)
  4059. break;
  4060. if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
  4061. {
  4062. /* We didn't make a PLT entry for this symbol. This
  4063. happens when statically linking PIC code, or when
  4064. using -Bsymbolic. */
  4065. if (h->got.offset != (bfd_vma) -1)
  4066. {
  4067. bool dyn;
  4068. off = h->got.offset;
  4069. dyn = htab->elf.dynamic_sections_created;
  4070. if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  4071. bfd_link_pic (info), h)
  4072. || (bfd_link_pic (info)
  4073. && SYMBOL_REFERENCES_LOCAL (info, h))
  4074. || (ELF_ST_VISIBILITY (h->other)
  4075. && h->root.type == bfd_link_hash_undefweak))
  4076. {
  4077. /* This is actually a static link, or it is a
  4078. -Bsymbolic link and the symbol is defined
  4079. locally, or the symbol was forced to be local
  4080. because of a version file. We must initialize
  4081. this entry in the global offset table. Since the
  4082. offset must always be a multiple of 4, we use the
  4083. least significant bit to record whether we have
  4084. initialized it already.
  4085. When doing a dynamic link, we create a .rela.dyn
  4086. relocation entry to initialize the value. This
  4087. is done in the finish_dynamic_symbol routine.
  4088. FIXME! */
  4089. if (off & 1)
  4090. off &= ~1;
  4091. else
  4092. {
  4093. h->got.offset |= 1;
  4094. if (GENERATE_RELATIVE_RELOC_P (info, h))
  4095. relative_reloc = true;
  4096. }
  4097. }
  4098. bfd_put_32 (output_bfd, relocation,
  4099. htab->elf.sgot->contents + off);
  4100. if (relative_reloc)
  4101. {
  4102. asection *srelgot;
  4103. Elf_Internal_Rela outrel;
  4104. bfd_byte *loc;
  4105. srelgot = htab->elf.srelgot;
  4106. BFD_ASSERT (srelgot != NULL);
  4107. outrel.r_offset
  4108. = (htab->elf.sgot->output_section->vma
  4109. + htab->elf.sgot->output_offset + off);
  4110. outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
  4111. outrel.r_addend = relocation;
  4112. loc = srelgot->contents;
  4113. loc += (srelgot->reloc_count++
  4114. * sizeof (Elf32_External_Rela));
  4115. if (loc != NULL)
  4116. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4117. }
  4118. relocation = off + htab->elf.sgot->output_offset;
  4119. }
  4120. break;
  4121. }
  4122. /* The relocation is the got offset. */
  4123. if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
  4124. relocation = (h->plt.offset / PLT_ENTRY_SIZE + 2) * 4;
  4125. else
  4126. relocation = (h->plt.offset / PLT_ENTRY_SIZE_P + 2) * 4;
  4127. unresolved_reloc = false;
  4128. break;
  4129. case R_CKCORE_PCREL_IMM26BY2:
  4130. case R_CKCORE_PCREL_JSR_IMM26BY2:
  4131. case R_CKCORE_PCREL_JSR_IMM11BY2:
  4132. case R_CKCORE_PCREL_IMM11BY2:
  4133. case R_CKCORE_CALLGRAPH:
  4134. /* Emit callgraph information first. */
  4135. /* TODO: deal with callgraph. */
  4136. if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_CALLGRAPH)
  4137. break;
  4138. /* Some reloc need further handling. */
  4139. /* h == NULL means the symbol is a local symbol,
  4140. r_symndx == 0 means the symbol is 'ABS' and
  4141. the relocation is already handled in assemble,
  4142. here just use for callgraph. */
  4143. /* TODO: deal with callgraph. */
  4144. if (h == NULL && r_symndx == 0)
  4145. {
  4146. do_final_relocate = false;
  4147. break;
  4148. }
  4149. /* Ignore weak references to undefined symbols. */
  4150. if (h != NULL && h->root.type == bfd_link_hash_undefweak)
  4151. {
  4152. do_final_relocate = false;
  4153. break;
  4154. }
  4155. /* Using branch stub. */
  4156. if (use_branch_stub == true
  4157. && ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2)
  4158. {
  4159. struct elf32_csky_stub_hash_entry *stub_entry = NULL;
  4160. if (sym_must_create_stub (h, info))
  4161. stub_entry = elf32_csky_get_stub_entry (input_section,
  4162. input_section,
  4163. h, rel, htab);
  4164. else if (disp > BSR_MAX_FWD_BRANCH_OFFSET
  4165. || disp < BSR_MAX_BWD_BRANCH_OFFSET)
  4166. stub_entry = elf32_csky_get_stub_entry (input_section,
  4167. input_section,
  4168. h, rel, htab);
  4169. if (stub_entry != NULL)
  4170. relocation
  4171. = (stub_entry->stub_offset
  4172. + stub_entry->stub_sec->output_offset
  4173. + stub_entry->stub_sec->output_section->vma);
  4174. break;
  4175. }
  4176. else if (h == NULL
  4177. || (h->root.type == bfd_link_hash_defined
  4178. && h->dynindx == -1)
  4179. || ((h->def_regular && !h->def_dynamic)
  4180. && (h->root.type != bfd_link_hash_defweak
  4181. || ! bfd_link_pic (info))))
  4182. {
  4183. if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_JSR_IMM26BY2)
  4184. {
  4185. if (within_range (disp, 26))
  4186. {
  4187. /* In range for BSR32. */
  4188. howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM26BY2];
  4189. read_content_substitute = CSKY_INSN_BSR32;
  4190. }
  4191. else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810)
  4192. /* if bsr32 cannot reach, generate
  4193. "lrw r25, label; jsr r25" instead of
  4194. jsri label. */
  4195. howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
  4196. } /* if ELF32_R_TYPE (rel->r_info)... */
  4197. else if (ELF32_R_TYPE (rel->r_info)
  4198. == R_CKCORE_PCREL_JSR_IMM11BY2)
  4199. {
  4200. if (within_range (disp, 11))
  4201. {
  4202. /* In range for BSR16. */
  4203. howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM11BY2];
  4204. read_content_substitute = CSKY_INSN_BSR16;
  4205. }
  4206. }
  4207. break;
  4208. } /* else if h == NULL... */
  4209. else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810
  4210. && (ELF32_R_TYPE (rel->r_info)
  4211. == R_CKCORE_PCREL_JSR_IMM26BY2))
  4212. {
  4213. howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
  4214. break;
  4215. }
  4216. /* Other situation, h->def_dynamic == 1,
  4217. undefined_symbol when output file is shared object, etc. */
  4218. /* Else fall through. */
  4219. case R_CKCORE_ADDR_HI16:
  4220. case R_CKCORE_ADDR_LO16:
  4221. if (bfd_link_pic (info)
  4222. || (!bfd_link_pic (info)
  4223. && h != NULL
  4224. && h->dynindx != -1
  4225. && !h->non_got_ref
  4226. && ((h->def_dynamic && !h->def_regular)
  4227. || (htab->elf.dynamic_sections_created
  4228. && (h->root.type == bfd_link_hash_undefweak
  4229. || h->root.type == bfd_link_hash_undefined
  4230. || h->root.type == bfd_link_hash_indirect)))))
  4231. {
  4232. Elf_Internal_Rela outrel;
  4233. bool skip, relocate;
  4234. bfd_byte *loc;
  4235. /* When generating a shared object, these relocations
  4236. are copied into the output file to be resolved at
  4237. run time. */
  4238. skip = false;
  4239. relocate = false;
  4240. outrel.r_offset =
  4241. _bfd_elf_section_offset (output_bfd, info, input_section,
  4242. rel->r_offset);
  4243. if (outrel.r_offset == (bfd_vma) -1)
  4244. skip = true;
  4245. else if (outrel.r_offset == (bfd_vma) -2)
  4246. {
  4247. skip = true;
  4248. relocate = true;
  4249. }
  4250. outrel.r_offset += (input_section->output_section->vma
  4251. + input_section->output_offset);
  4252. if (skip)
  4253. memset (&outrel, 0, sizeof (outrel));
  4254. else if (h != NULL
  4255. && h->dynindx != -1
  4256. && (!bfd_link_pic (info)
  4257. || (!SYMBOLIC_BIND (info, h)
  4258. && h->root.type == bfd_link_hash_defweak)
  4259. || !h->def_regular))
  4260. {
  4261. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  4262. outrel.r_addend = rel->r_addend;
  4263. }
  4264. else
  4265. {
  4266. /* This symbol is local, or marked to become local. */
  4267. relocate = true;
  4268. outrel.r_info = ELF32_R_INFO (0, r_type);
  4269. outrel.r_addend = relocation + rel->r_addend;
  4270. }
  4271. loc = htab->elf.srelgot->contents;
  4272. loc += (htab->elf.srelgot->reloc_count++
  4273. * sizeof (Elf32_External_Rela));
  4274. if (loc != NULL)
  4275. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4276. /* If this reloc is against an external symbol, we do not
  4277. want to diddle with the addend. Otherwise, we need to
  4278. include the symbol value so that it becomes an addend
  4279. for the dynamic reloc. */
  4280. if (!relocate)
  4281. continue;
  4282. } /* if bfd_link_pic (info) ... */
  4283. break;
  4284. case R_CKCORE_ADDR32:
  4285. /* r_symndx will be zero only for relocs against symbols
  4286. from removed linkonce sections, or sections discarded
  4287. by a linker script.
  4288. This relocation don't nedd to handle, the value will
  4289. be set to SEC_DISCARDED(0xffffffff). */
  4290. if (r_symndx == 0
  4291. && strcmp (sec->name, ".csky_stack_size") == 0)
  4292. {
  4293. do_final_relocate = false;
  4294. break;
  4295. }
  4296. if (r_symndx >= symtab_hdr->sh_info
  4297. && h->non_got_ref
  4298. && bfd_link_executable (info))
  4299. break;
  4300. if (r_symndx == 0 || (input_section->flags & SEC_ALLOC) == 0)
  4301. break;
  4302. if (bfd_link_pic (info)
  4303. || (h != NULL
  4304. && h->dynindx != -1
  4305. && ((h->def_dynamic && !h->def_regular)
  4306. || (htab->elf.dynamic_sections_created
  4307. && (h->root.type == bfd_link_hash_undefweak
  4308. || h->root.type == bfd_link_hash_undefined
  4309. || h->root.type == bfd_link_hash_indirect)))))
  4310. {
  4311. Elf_Internal_Rela outrel;
  4312. bool skip, relocate;
  4313. bfd_byte *loc;
  4314. /* When generating a shared object, these relocations
  4315. are copied into the output file to be resolved at
  4316. run time. */
  4317. skip = false;
  4318. relocate = false;
  4319. outrel.r_offset =
  4320. _bfd_elf_section_offset (output_bfd, info, input_section,
  4321. rel->r_offset);
  4322. if (outrel.r_offset == (bfd_vma) -1)
  4323. skip = true;
  4324. else if (outrel.r_offset == (bfd_vma) -2)
  4325. {
  4326. skip = true;
  4327. relocate = true;
  4328. }
  4329. outrel.r_offset += (input_section->output_section->vma
  4330. + input_section->output_offset);
  4331. if (skip)
  4332. memset (&outrel, 0, sizeof (outrel));
  4333. else if (h != NULL
  4334. && h->dynindx != -1
  4335. && (!bfd_link_pic (info)
  4336. || (!SYMBOLIC_BIND (info, h)
  4337. && h->root.type == bfd_link_hash_defweak)
  4338. || !h->def_regular))
  4339. {
  4340. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  4341. outrel.r_addend = rel->r_addend;
  4342. }
  4343. else
  4344. {
  4345. /* This symbol is local, or marked to become local. */
  4346. outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
  4347. outrel.r_addend = relocation + rel->r_addend;
  4348. }
  4349. loc = htab->elf.srelgot->contents;
  4350. loc += (htab->elf.srelgot->reloc_count++
  4351. * sizeof (Elf32_External_Rela));
  4352. if (loc != NULL)
  4353. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4354. /* If this reloc is against an external symbol, we do
  4355. want to diddle with the addend. Otherwise, we need to
  4356. include the symbol value so that it becomes an addend
  4357. for the dynamic reloc. */
  4358. if (! relocate)
  4359. continue;
  4360. }
  4361. break;
  4362. case R_CKCORE_TLS_LDO32:
  4363. relocation = relocation - dtpoff_base (info);
  4364. break;
  4365. case R_CKCORE_TLS_LDM32:
  4366. BFD_ASSERT (htab->elf.sgot != NULL);
  4367. off = htab->tls_ldm_got.offset;
  4368. if (off & 1)
  4369. off &= ~1;
  4370. else
  4371. {
  4372. /* If we don't know the module number,
  4373. create a relocation for it. */
  4374. if (!bfd_link_executable (info))
  4375. {
  4376. Elf_Internal_Rela outrel;
  4377. bfd_byte *loc;
  4378. BFD_ASSERT (htab->elf.srelgot != NULL);
  4379. outrel.r_addend = 0;
  4380. outrel.r_offset
  4381. = (htab->elf.sgot->output_section->vma
  4382. + htab->elf.sgot->output_offset + off);
  4383. outrel.r_info = ELF32_R_INFO (0, R_CKCORE_TLS_DTPMOD32);
  4384. bfd_put_32 (output_bfd, outrel.r_addend,
  4385. htab->elf.sgot->contents + off);
  4386. loc = htab->elf.srelgot->contents;
  4387. loc += (htab->elf.srelgot->reloc_count++
  4388. * sizeof (Elf32_External_Rela));
  4389. if (loc)
  4390. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4391. }
  4392. else
  4393. bfd_put_32 (output_bfd, 1,
  4394. htab->elf.sgot->contents + off);
  4395. htab->tls_ldm_got.offset |= 1;
  4396. }
  4397. relocation
  4398. = (htab->elf.sgot->output_section->vma
  4399. + htab->elf.sgot->output_offset + off
  4400. - (input_section->output_section->vma
  4401. + input_section->output_offset + rel->r_offset));
  4402. break;
  4403. case R_CKCORE_TLS_LE32:
  4404. if (bfd_link_dll (info))
  4405. {
  4406. _bfd_error_handler
  4407. /* xgettext:c-format */
  4408. (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
  4409. "in shared object"),
  4410. input_bfd, input_section, (uint64_t)rel->r_offset,
  4411. howto->name);
  4412. return false;
  4413. }
  4414. else
  4415. relocation = tpoff (info, relocation);
  4416. break;
  4417. case R_CKCORE_TLS_GD32:
  4418. case R_CKCORE_TLS_IE32:
  4419. {
  4420. int indx;
  4421. char tls_type;
  4422. BFD_ASSERT (htab->elf.sgot != NULL);
  4423. indx = 0;
  4424. if (h != NULL)
  4425. {
  4426. bool dyn;
  4427. dyn = htab->elf.dynamic_sections_created;
  4428. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  4429. bfd_link_pic (info), h)
  4430. && (!bfd_link_pic (info)
  4431. || !SYMBOL_REFERENCES_LOCAL (info, h)))
  4432. {
  4433. unresolved_reloc = false;
  4434. indx = h->dynindx;
  4435. }
  4436. off = h->got.offset;
  4437. tls_type = ((struct csky_elf_link_hash_entry *)h)->tls_type;
  4438. }
  4439. else
  4440. {
  4441. BFD_ASSERT (local_got_offsets != NULL);
  4442. off = local_got_offsets[r_symndx];
  4443. tls_type = csky_elf_local_got_tls_type (input_bfd)[r_symndx];
  4444. }
  4445. BFD_ASSERT (tls_type != GOT_UNKNOWN);
  4446. if (off & 1)
  4447. off &= ~1;
  4448. else
  4449. {
  4450. bool need_relocs = false;
  4451. Elf_Internal_Rela outrel;
  4452. bfd_byte *loc = NULL;
  4453. int cur_off = off;
  4454. /* The GOT entries have not been initialized yet. Do it
  4455. now, and emit any relocations. If both an IE GOT and a
  4456. GD GOT are necessary, we emit the GD first. */
  4457. if ((!bfd_link_executable (info) || indx != 0)
  4458. && (h == NULL
  4459. || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  4460. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
  4461. || h->root.type != bfd_link_hash_undefined))
  4462. {
  4463. need_relocs = true;
  4464. BFD_ASSERT (htab->elf.srelgot != NULL);
  4465. loc = htab->elf.srelgot->contents;
  4466. loc += (htab->elf.srelgot->reloc_count
  4467. * sizeof (Elf32_External_Rela));
  4468. }
  4469. if (tls_type & GOT_TLS_GD)
  4470. {
  4471. if (need_relocs)
  4472. {
  4473. outrel.r_addend = 0;
  4474. outrel.r_offset
  4475. = (htab->elf.sgot->output_section->vma
  4476. + htab->elf.sgot->output_offset
  4477. + cur_off);
  4478. outrel.r_info
  4479. = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPMOD32);
  4480. bfd_put_32 (output_bfd, outrel.r_addend,
  4481. htab->elf.sgot->contents + cur_off);
  4482. if (loc)
  4483. bfd_elf32_swap_reloca_out (output_bfd,
  4484. &outrel, loc);
  4485. loc += sizeof (Elf32_External_Rela);
  4486. htab->elf.srelgot->reloc_count++;
  4487. if (indx == 0)
  4488. bfd_put_32 (output_bfd,
  4489. relocation - dtpoff_base (info),
  4490. (htab->elf.sgot->contents
  4491. + cur_off + 4));
  4492. else
  4493. {
  4494. outrel.r_addend = 0;
  4495. outrel.r_info
  4496. = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPOFF32);
  4497. outrel.r_offset += 4;
  4498. bfd_put_32 (output_bfd, outrel.r_addend,
  4499. (htab->elf.sgot->contents
  4500. + cur_off + 4));
  4501. outrel.r_info =
  4502. ELF32_R_INFO (indx,
  4503. R_CKCORE_TLS_DTPOFF32);
  4504. if (loc)
  4505. bfd_elf32_swap_reloca_out (output_bfd,
  4506. &outrel,
  4507. loc);
  4508. htab->elf.srelgot->reloc_count++;
  4509. loc += sizeof (Elf32_External_Rela);
  4510. }
  4511. }
  4512. else
  4513. {
  4514. /* If are not emitting relocations for a
  4515. general dynamic reference, then we must be in a
  4516. static link or an executable link with the
  4517. symbol binding locally. Mark it as belonging
  4518. to module 1, the executable. */
  4519. bfd_put_32 (output_bfd, 1,
  4520. htab->elf.sgot->contents + cur_off);
  4521. bfd_put_32 (output_bfd,
  4522. relocation - dtpoff_base (info),
  4523. htab->elf.sgot->contents
  4524. + cur_off + 4);
  4525. }
  4526. cur_off += 8;
  4527. }
  4528. if (tls_type & GOT_TLS_IE)
  4529. {
  4530. if (need_relocs)
  4531. {
  4532. if (indx == 0)
  4533. outrel.r_addend = relocation - dtpoff_base (info);
  4534. else
  4535. outrel.r_addend = 0;
  4536. outrel.r_offset
  4537. = (htab->elf.sgot->output_section->vma
  4538. + htab->elf.sgot->output_offset + cur_off);
  4539. outrel.r_info
  4540. = ELF32_R_INFO (indx, R_CKCORE_TLS_TPOFF32);
  4541. bfd_put_32 (output_bfd, outrel.r_addend,
  4542. htab->elf.sgot->contents + cur_off);
  4543. if (loc)
  4544. bfd_elf32_swap_reloca_out (output_bfd,
  4545. &outrel, loc);
  4546. htab->elf.srelgot->reloc_count++;
  4547. loc += sizeof (Elf32_External_Rela);
  4548. }
  4549. else
  4550. bfd_put_32 (output_bfd, tpoff (info, relocation),
  4551. htab->elf.sgot->contents + cur_off);
  4552. }
  4553. if (h != NULL)
  4554. h->got.offset |= 1;
  4555. else
  4556. local_got_offsets[r_symndx] |= 1;
  4557. }
  4558. if ((tls_type & GOT_TLS_GD) && howto->type != R_CKCORE_TLS_GD32)
  4559. off += 8;
  4560. relocation
  4561. = (htab->elf.sgot->output_section->vma
  4562. + htab->elf.sgot->output_offset + off
  4563. - (input_section->output_section->vma
  4564. + input_section->output_offset
  4565. + rel->r_offset));
  4566. break;
  4567. }
  4568. default:
  4569. /* No substitution when final linking. */
  4570. read_content_substitute = 0;
  4571. break;
  4572. } /* End switch (howto->type). */
  4573. /* Make sure 32-bit data in the text section will not be affected by
  4574. our special endianness.
  4575. However, this currently affects noting, since the ADDR32 howto type
  4576. does no change with the data read. But we may need this mechanism in
  4577. the future. */
  4578. if (howto->size == 2
  4579. && (howto->type == R_CKCORE_ADDR32
  4580. || howto->type == R_CKCORE_PCREL32
  4581. || howto->type == R_CKCORE_GOT32
  4582. || howto->type == R_CKCORE_GOTOFF
  4583. || howto->type == R_CKCORE_GOTPC
  4584. || howto->type == R_CKCORE_PLT32
  4585. || howto->type == R_CKCORE_TLS_LE32
  4586. || howto->type == R_CKCORE_TLS_IE32
  4587. || howto->type == R_CKCORE_TLS_LDM32
  4588. || howto->type == R_CKCORE_TLS_GD32
  4589. || howto->type == R_CKCORE_TLS_LDO32
  4590. || howto->type == R_CKCORE_RELATIVE))
  4591. need_reverse_bits = 0;
  4592. else
  4593. need_reverse_bits = 1;
  4594. /* Do the final link. */
  4595. if (howto->type != R_CKCORE_PCREL_JSR_IMM11BY2
  4596. && howto->type != R_CKCORE_PCREL_JSR_IMM26BY2
  4597. && howto->type != R_CKCORE_CALLGRAPH
  4598. && do_final_relocate)
  4599. r = csky_final_link_relocate (howto, input_bfd, input_section,
  4600. contents, rel->r_offset,
  4601. relocation, addend);
  4602. if (r != bfd_reloc_ok)
  4603. {
  4604. ret = false;
  4605. switch (r)
  4606. {
  4607. default:
  4608. break;
  4609. case bfd_reloc_overflow:
  4610. if (h != NULL)
  4611. name = NULL;
  4612. else
  4613. {
  4614. name = bfd_elf_string_from_elf_section (input_bfd,
  4615. symtab_hdr->sh_link,
  4616. sym->st_name);
  4617. if (name == NULL)
  4618. break;
  4619. if (*name == '\0')
  4620. name = bfd_section_name (sec);
  4621. }
  4622. (*info->callbacks->reloc_overflow)
  4623. (info,
  4624. (h ? &h->root : NULL),
  4625. name, howto->name, (bfd_vma) 0,
  4626. input_bfd, input_section, rel->r_offset);
  4627. break;
  4628. }
  4629. }
  4630. } /* End for (;rel < relend; rel++). */
  4631. return ret;
  4632. }
  4633. static bool
  4634. csky_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  4635. {
  4636. int offset;
  4637. size_t size;
  4638. switch (note->descsz)
  4639. {
  4640. default:
  4641. return false;
  4642. /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
  4643. case 148:
  4644. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  4645. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  4646. offset = 72;
  4647. size = 72;
  4648. break;
  4649. /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
  4650. case 220:
  4651. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  4652. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  4653. offset = 72;
  4654. size = 34 * 4;
  4655. break;
  4656. }
  4657. /* Make a ".reg/999" section. */
  4658. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  4659. size, note->descpos + offset);
  4660. }
  4661. static bool
  4662. csky_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  4663. {
  4664. switch (note->descsz)
  4665. {
  4666. default:
  4667. return false;
  4668. /* Sizeof (struct elf_prpsinfo) on linux csky. */
  4669. case 124:
  4670. elf_tdata (abfd)->core->program
  4671. = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  4672. elf_tdata (abfd)->core->command
  4673. = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  4674. }
  4675. /* Note that for some reason, a spurious space is tacked
  4676. onto the end of the args in some (at least one anyway)
  4677. implementations, so strip it off if it exists. */
  4678. {
  4679. char *command = elf_tdata (abfd)->core->command;
  4680. int n = strlen (command);
  4681. if (0 < n && command[n - 1] == ' ')
  4682. command[n - 1] = '\0';
  4683. }
  4684. return true;
  4685. }
  4686. /* Determine whether an object attribute tag takes an integer, a
  4687. string or both. */
  4688. static int
  4689. elf32_csky_obj_attrs_arg_type (int tag)
  4690. {
  4691. switch (tag)
  4692. {
  4693. case Tag_compatibility:
  4694. return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
  4695. case Tag_CSKY_ARCH_NAME:
  4696. case Tag_CSKY_CPU_NAME:
  4697. case Tag_CSKY_FPU_NUMBER_MODULE:
  4698. return ATTR_TYPE_FLAG_STR_VAL;
  4699. case Tag_CSKY_ISA_FLAGS:
  4700. case Tag_CSKY_ISA_EXT_FLAGS:
  4701. case Tag_CSKY_DSP_VERSION:
  4702. case Tag_CSKY_VDSP_VERSION:
  4703. case Tag_CSKY_FPU_VERSION:
  4704. case Tag_CSKY_FPU_ABI:
  4705. case Tag_CSKY_FPU_ROUNDING:
  4706. case Tag_CSKY_FPU_HARDFP:
  4707. case Tag_CSKY_FPU_Exception:
  4708. case Tag_CSKY_FPU_DENORMAL:
  4709. return ATTR_TYPE_FLAG_INT_VAL;
  4710. default:
  4711. break;
  4712. }
  4713. return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
  4714. }
  4715. /* Attribute numbers >=64 (mod 128) can be safely ignored. */
  4716. static bool
  4717. elf32_csky_obj_attrs_handle_unknown (bfd *abfd ATTRIBUTE_UNUSED,
  4718. int tag ATTRIBUTE_UNUSED)
  4719. {
  4720. return true;
  4721. }
  4722. /* End of external entry points for sizing and building linker stubs. */
  4723. /* CPU-related basic API. */
  4724. #define TARGET_BIG_SYM csky_elf32_be_vec
  4725. #define TARGET_BIG_NAME "elf32-csky-big"
  4726. #define TARGET_LITTLE_SYM csky_elf32_le_vec
  4727. #define TARGET_LITTLE_NAME "elf32-csky-little"
  4728. #define ELF_ARCH bfd_arch_csky
  4729. #define ELF_MACHINE_CODE EM_CSKY
  4730. #define ELF_MACHINE_ALT1 EM_CSKY_OLD
  4731. #define ELF_MAXPAGESIZE 0x1000
  4732. #define elf_info_to_howto csky_elf_info_to_howto
  4733. #define elf_info_to_howto_rel NULL
  4734. #define elf_backend_special_sections csky_elf_special_sections
  4735. #define bfd_elf32_bfd_link_hash_table_create csky_elf_link_hash_table_create
  4736. /* Target related API. */
  4737. #define bfd_elf32_mkobject csky_elf_mkobject
  4738. #define bfd_elf32_bfd_merge_private_bfd_data csky_elf_merge_private_bfd_data
  4739. #define bfd_elf32_bfd_set_private_flags csky_elf_set_private_flags
  4740. #define elf_backend_copy_indirect_symbol csky_elf_copy_indirect_symbol
  4741. #define bfd_elf32_bfd_is_target_special_symbol csky_elf_is_target_special_symbol
  4742. #define elf_backend_maybe_function_sym csky_elf_maybe_function_sym
  4743. /* GC section related API. */
  4744. #define elf_backend_can_gc_sections 1
  4745. #define elf_backend_gc_mark_hook csky_elf_gc_mark_hook
  4746. #define elf_backend_gc_mark_extra_sections elf32_csky_gc_mark_extra_sections
  4747. /* Relocation related API. */
  4748. #define elf_backend_reloc_type_class csky_elf_reloc_type_class
  4749. #define bfd_elf32_bfd_reloc_type_lookup csky_elf_reloc_type_lookup
  4750. #define bfd_elf32_bfd_reloc_name_lookup csky_elf_reloc_name_lookup
  4751. #define elf_backend_ignore_discarded_relocs csky_elf_ignore_discarded_relocs
  4752. #define elf_backend_relocate_section csky_elf_relocate_section
  4753. #define elf_backend_check_relocs csky_elf_check_relocs
  4754. /* Dynamic relocate related API. */
  4755. #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
  4756. #define elf_backend_adjust_dynamic_symbol csky_elf_adjust_dynamic_symbol
  4757. #define elf_backend_size_dynamic_sections csky_elf_size_dynamic_sections
  4758. #define elf_backend_finish_dynamic_symbol csky_elf_finish_dynamic_symbol
  4759. #define elf_backend_finish_dynamic_sections csky_elf_finish_dynamic_sections
  4760. #define elf_backend_rela_normal 1
  4761. #define elf_backend_can_refcount 1
  4762. #define elf_backend_plt_readonly 1
  4763. #define elf_backend_want_got_sym 1
  4764. #define elf_backend_want_dynrelro 1
  4765. #define elf_backend_got_header_size 12
  4766. #define elf_backend_want_got_plt 1
  4767. /* C-SKY coredump support. */
  4768. #define elf_backend_grok_prstatus csky_elf_grok_prstatus
  4769. #define elf_backend_grok_psinfo csky_elf_grok_psinfo
  4770. /* Attribute sections. */
  4771. #undef elf_backend_obj_attrs_vendor
  4772. #define elf_backend_obj_attrs_vendor "csky"
  4773. #undef elf_backend_obj_attrs_section
  4774. #define elf_backend_obj_attrs_section ".csky.attributes"
  4775. #undef elf_backend_obj_attrs_arg_type
  4776. #define elf_backend_obj_attrs_arg_type elf32_csky_obj_attrs_arg_type
  4777. #undef elf_backend_obj_attrs_section_type
  4778. #define elf_backend_obj_attrs_section_type SHT_CSKY_ATTRIBUTES
  4779. #define elf_backend_obj_attrs_handle_unknown elf32_csky_obj_attrs_handle_unknown
  4780. #include "elf32-target.h"