elf-m10300.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520
  1. /* Matsushita 10300 specific support for 32-bit ELF
  2. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/mn10300.h"
  21. #include "libiberty.h"
  22. /* The mn10300 linker needs to keep track of the number of relocs that
  23. it decides to copy in check_relocs for each symbol. This is so
  24. that it can discard PC relative relocs if it doesn't need them when
  25. linking with -Bsymbolic. We store the information in a field
  26. extending the regular ELF linker hash table. */
  27. struct elf32_mn10300_link_hash_entry
  28. {
  29. /* The basic elf link hash table entry. */
  30. struct elf_link_hash_entry root;
  31. /* For function symbols, the number of times this function is
  32. called directly (ie by name). */
  33. unsigned int direct_calls;
  34. /* For function symbols, the size of this function's stack
  35. (if <= 255 bytes). We stuff this into "call" instructions
  36. to this target when it's valid and profitable to do so.
  37. This does not include stack allocated by movm! */
  38. unsigned char stack_size;
  39. /* For function symbols, arguments (if any) for movm instruction
  40. in the prologue. We stuff this value into "call" instructions
  41. to the target when it's valid and profitable to do so. */
  42. unsigned char movm_args;
  43. /* For function symbols, the amount of stack space that would be allocated
  44. by the movm instruction. This is redundant with movm_args, but we
  45. add it to the hash table to avoid computing it over and over. */
  46. unsigned char movm_stack_size;
  47. /* When set, convert all "call" instructions to this target into "calls"
  48. instructions. */
  49. #define MN10300_CONVERT_CALL_TO_CALLS 0x1
  50. /* Used to mark functions which have had redundant parts of their
  51. prologue deleted. */
  52. #define MN10300_DELETED_PROLOGUE_BYTES 0x2
  53. unsigned char flags;
  54. /* Calculated value. */
  55. bfd_vma value;
  56. #define GOT_UNKNOWN 0
  57. #define GOT_NORMAL 1
  58. #define GOT_TLS_GD 2
  59. #define GOT_TLS_LD 3
  60. #define GOT_TLS_IE 4
  61. /* Used to distinguish GOT entries for TLS types from normal GOT entries. */
  62. unsigned char tls_type;
  63. };
  64. /* We derive a hash table from the main elf linker hash table so
  65. we can store state variables and a secondary hash table without
  66. resorting to global variables. */
  67. struct elf32_mn10300_link_hash_table
  68. {
  69. /* The main hash table. */
  70. struct elf_link_hash_table root;
  71. /* A hash table for static functions. We could derive a new hash table
  72. instead of using the full elf32_mn10300_link_hash_table if we wanted
  73. to save some memory. */
  74. struct elf32_mn10300_link_hash_table *static_hash_table;
  75. /* Random linker state flags. */
  76. #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
  77. char flags;
  78. struct
  79. {
  80. bfd_signed_vma refcount;
  81. bfd_vma offset;
  82. char got_allocated;
  83. char rel_emitted;
  84. } tls_ldm_got;
  85. };
  86. #define elf_mn10300_hash_entry(ent) ((struct elf32_mn10300_link_hash_entry *)(ent))
  87. struct elf_mn10300_obj_tdata
  88. {
  89. struct elf_obj_tdata root;
  90. /* tls_type for each local got entry. */
  91. char * local_got_tls_type;
  92. };
  93. #define elf_mn10300_tdata(abfd) \
  94. ((struct elf_mn10300_obj_tdata *) (abfd)->tdata.any)
  95. #define elf_mn10300_local_got_tls_type(abfd) \
  96. (elf_mn10300_tdata (abfd)->local_got_tls_type)
  97. #ifndef streq
  98. #define streq(a, b) (strcmp ((a),(b)) == 0)
  99. #endif
  100. /* For MN10300 linker hash table. */
  101. /* Get the MN10300 ELF linker hash table from a link_info structure. */
  102. #define elf32_mn10300_hash_table(p) \
  103. ((is_elf_hash_table ((p)->hash) \
  104. && elf_hash_table_id (elf_hash_table (p)) == MN10300_ELF_DATA) \
  105. ? (struct elf32_mn10300_link_hash_table *) (p)->hash : NULL)
  106. #define elf32_mn10300_link_hash_traverse(table, func, info) \
  107. (elf_link_hash_traverse \
  108. (&(table)->root, \
  109. (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
  110. (info)))
  111. static reloc_howto_type elf_mn10300_howto_table[] =
  112. {
  113. /* Dummy relocation. Does nothing. */
  114. HOWTO (R_MN10300_NONE,
  115. 0,
  116. 3,
  117. 0,
  118. false,
  119. 0,
  120. complain_overflow_dont,
  121. bfd_elf_generic_reloc,
  122. "R_MN10300_NONE",
  123. false,
  124. 0,
  125. 0,
  126. false),
  127. /* Standard 32 bit reloc. */
  128. HOWTO (R_MN10300_32,
  129. 0,
  130. 2,
  131. 32,
  132. false,
  133. 0,
  134. complain_overflow_bitfield,
  135. bfd_elf_generic_reloc,
  136. "R_MN10300_32",
  137. false,
  138. 0xffffffff,
  139. 0xffffffff,
  140. false),
  141. /* Standard 16 bit reloc. */
  142. HOWTO (R_MN10300_16,
  143. 0,
  144. 1,
  145. 16,
  146. false,
  147. 0,
  148. complain_overflow_bitfield,
  149. bfd_elf_generic_reloc,
  150. "R_MN10300_16",
  151. false,
  152. 0xffff,
  153. 0xffff,
  154. false),
  155. /* Standard 8 bit reloc. */
  156. HOWTO (R_MN10300_8,
  157. 0,
  158. 0,
  159. 8,
  160. false,
  161. 0,
  162. complain_overflow_bitfield,
  163. bfd_elf_generic_reloc,
  164. "R_MN10300_8",
  165. false,
  166. 0xff,
  167. 0xff,
  168. false),
  169. /* Standard 32bit pc-relative reloc. */
  170. HOWTO (R_MN10300_PCREL32,
  171. 0,
  172. 2,
  173. 32,
  174. true,
  175. 0,
  176. complain_overflow_bitfield,
  177. bfd_elf_generic_reloc,
  178. "R_MN10300_PCREL32",
  179. false,
  180. 0xffffffff,
  181. 0xffffffff,
  182. true),
  183. /* Standard 16bit pc-relative reloc. */
  184. HOWTO (R_MN10300_PCREL16,
  185. 0,
  186. 1,
  187. 16,
  188. true,
  189. 0,
  190. complain_overflow_bitfield,
  191. bfd_elf_generic_reloc,
  192. "R_MN10300_PCREL16",
  193. false,
  194. 0xffff,
  195. 0xffff,
  196. true),
  197. /* Standard 8 pc-relative reloc. */
  198. HOWTO (R_MN10300_PCREL8,
  199. 0,
  200. 0,
  201. 8,
  202. true,
  203. 0,
  204. complain_overflow_bitfield,
  205. bfd_elf_generic_reloc,
  206. "R_MN10300_PCREL8",
  207. false,
  208. 0xff,
  209. 0xff,
  210. true),
  211. /* GNU extension to record C++ vtable hierarchy. */
  212. HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
  213. 0, /* rightshift */
  214. 0, /* size (0 = byte, 1 = short, 2 = long) */
  215. 0, /* bitsize */
  216. false, /* pc_relative */
  217. 0, /* bitpos */
  218. complain_overflow_dont, /* complain_on_overflow */
  219. NULL, /* special_function */
  220. "R_MN10300_GNU_VTINHERIT", /* name */
  221. false, /* partial_inplace */
  222. 0, /* src_mask */
  223. 0, /* dst_mask */
  224. false), /* pcrel_offset */
  225. /* GNU extension to record C++ vtable member usage */
  226. HOWTO (R_MN10300_GNU_VTENTRY, /* type */
  227. 0, /* rightshift */
  228. 0, /* size (0 = byte, 1 = short, 2 = long) */
  229. 0, /* bitsize */
  230. false, /* pc_relative */
  231. 0, /* bitpos */
  232. complain_overflow_dont, /* complain_on_overflow */
  233. NULL, /* special_function */
  234. "R_MN10300_GNU_VTENTRY", /* name */
  235. false, /* partial_inplace */
  236. 0, /* src_mask */
  237. 0, /* dst_mask */
  238. false), /* pcrel_offset */
  239. /* Standard 24 bit reloc. */
  240. HOWTO (R_MN10300_24,
  241. 0,
  242. 2,
  243. 24,
  244. false,
  245. 0,
  246. complain_overflow_bitfield,
  247. bfd_elf_generic_reloc,
  248. "R_MN10300_24",
  249. false,
  250. 0xffffff,
  251. 0xffffff,
  252. false),
  253. HOWTO (R_MN10300_GOTPC32, /* type */
  254. 0, /* rightshift */
  255. 2, /* size (0 = byte, 1 = short, 2 = long) */
  256. 32, /* bitsize */
  257. true, /* pc_relative */
  258. 0, /* bitpos */
  259. complain_overflow_bitfield, /* complain_on_overflow */
  260. bfd_elf_generic_reloc, /* */
  261. "R_MN10300_GOTPC32", /* name */
  262. false, /* partial_inplace */
  263. 0xffffffff, /* src_mask */
  264. 0xffffffff, /* dst_mask */
  265. true), /* pcrel_offset */
  266. HOWTO (R_MN10300_GOTPC16, /* type */
  267. 0, /* rightshift */
  268. 1, /* size (0 = byte, 1 = short, 2 = long) */
  269. 16, /* bitsize */
  270. true, /* pc_relative */
  271. 0, /* bitpos */
  272. complain_overflow_bitfield, /* complain_on_overflow */
  273. bfd_elf_generic_reloc, /* */
  274. "R_MN10300_GOTPC16", /* name */
  275. false, /* partial_inplace */
  276. 0xffff, /* src_mask */
  277. 0xffff, /* dst_mask */
  278. true), /* pcrel_offset */
  279. HOWTO (R_MN10300_GOTOFF32, /* type */
  280. 0, /* rightshift */
  281. 2, /* size (0 = byte, 1 = short, 2 = long) */
  282. 32, /* bitsize */
  283. false, /* pc_relative */
  284. 0, /* bitpos */
  285. complain_overflow_bitfield, /* complain_on_overflow */
  286. bfd_elf_generic_reloc, /* */
  287. "R_MN10300_GOTOFF32", /* name */
  288. false, /* partial_inplace */
  289. 0xffffffff, /* src_mask */
  290. 0xffffffff, /* dst_mask */
  291. false), /* pcrel_offset */
  292. HOWTO (R_MN10300_GOTOFF24, /* type */
  293. 0, /* rightshift */
  294. 2, /* size (0 = byte, 1 = short, 2 = long) */
  295. 24, /* bitsize */
  296. false, /* pc_relative */
  297. 0, /* bitpos */
  298. complain_overflow_bitfield, /* complain_on_overflow */
  299. bfd_elf_generic_reloc, /* */
  300. "R_MN10300_GOTOFF24", /* name */
  301. false, /* partial_inplace */
  302. 0xffffff, /* src_mask */
  303. 0xffffff, /* dst_mask */
  304. false), /* pcrel_offset */
  305. HOWTO (R_MN10300_GOTOFF16, /* type */
  306. 0, /* rightshift */
  307. 1, /* size (0 = byte, 1 = short, 2 = long) */
  308. 16, /* bitsize */
  309. false, /* pc_relative */
  310. 0, /* bitpos */
  311. complain_overflow_bitfield, /* complain_on_overflow */
  312. bfd_elf_generic_reloc, /* */
  313. "R_MN10300_GOTOFF16", /* name */
  314. false, /* partial_inplace */
  315. 0xffff, /* src_mask */
  316. 0xffff, /* dst_mask */
  317. false), /* pcrel_offset */
  318. HOWTO (R_MN10300_PLT32, /* type */
  319. 0, /* rightshift */
  320. 2, /* size (0 = byte, 1 = short, 2 = long) */
  321. 32, /* bitsize */
  322. true, /* pc_relative */
  323. 0, /* bitpos */
  324. complain_overflow_bitfield, /* complain_on_overflow */
  325. bfd_elf_generic_reloc, /* */
  326. "R_MN10300_PLT32", /* name */
  327. false, /* partial_inplace */
  328. 0xffffffff, /* src_mask */
  329. 0xffffffff, /* dst_mask */
  330. true), /* pcrel_offset */
  331. HOWTO (R_MN10300_PLT16, /* type */
  332. 0, /* rightshift */
  333. 1, /* size (0 = byte, 1 = short, 2 = long) */
  334. 16, /* bitsize */
  335. true, /* pc_relative */
  336. 0, /* bitpos */
  337. complain_overflow_bitfield, /* complain_on_overflow */
  338. bfd_elf_generic_reloc, /* */
  339. "R_MN10300_PLT16", /* name */
  340. false, /* partial_inplace */
  341. 0xffff, /* src_mask */
  342. 0xffff, /* dst_mask */
  343. true), /* pcrel_offset */
  344. HOWTO (R_MN10300_GOT32, /* type */
  345. 0, /* rightshift */
  346. 2, /* size (0 = byte, 1 = short, 2 = long) */
  347. 32, /* bitsize */
  348. false, /* pc_relative */
  349. 0, /* bitpos */
  350. complain_overflow_bitfield, /* complain_on_overflow */
  351. bfd_elf_generic_reloc, /* */
  352. "R_MN10300_GOT32", /* name */
  353. false, /* partial_inplace */
  354. 0xffffffff, /* src_mask */
  355. 0xffffffff, /* dst_mask */
  356. false), /* pcrel_offset */
  357. HOWTO (R_MN10300_GOT24, /* type */
  358. 0, /* rightshift */
  359. 2, /* size (0 = byte, 1 = short, 2 = long) */
  360. 24, /* bitsize */
  361. false, /* pc_relative */
  362. 0, /* bitpos */
  363. complain_overflow_bitfield, /* complain_on_overflow */
  364. bfd_elf_generic_reloc, /* */
  365. "R_MN10300_GOT24", /* name */
  366. false, /* partial_inplace */
  367. 0xffffffff, /* src_mask */
  368. 0xffffffff, /* dst_mask */
  369. false), /* pcrel_offset */
  370. HOWTO (R_MN10300_GOT16, /* type */
  371. 0, /* rightshift */
  372. 1, /* size (0 = byte, 1 = short, 2 = long) */
  373. 16, /* bitsize */
  374. false, /* pc_relative */
  375. 0, /* bitpos */
  376. complain_overflow_bitfield, /* complain_on_overflow */
  377. bfd_elf_generic_reloc, /* */
  378. "R_MN10300_GOT16", /* name */
  379. false, /* partial_inplace */
  380. 0xffffffff, /* src_mask */
  381. 0xffffffff, /* dst_mask */
  382. false), /* pcrel_offset */
  383. HOWTO (R_MN10300_COPY, /* type */
  384. 0, /* rightshift */
  385. 2, /* size (0 = byte, 1 = short, 2 = long) */
  386. 32, /* bitsize */
  387. false, /* pc_relative */
  388. 0, /* bitpos */
  389. complain_overflow_bitfield, /* complain_on_overflow */
  390. bfd_elf_generic_reloc, /* */
  391. "R_MN10300_COPY", /* name */
  392. false, /* partial_inplace */
  393. 0xffffffff, /* src_mask */
  394. 0xffffffff, /* dst_mask */
  395. false), /* pcrel_offset */
  396. HOWTO (R_MN10300_GLOB_DAT, /* type */
  397. 0, /* rightshift */
  398. 2, /* size (0 = byte, 1 = short, 2 = long) */
  399. 32, /* bitsize */
  400. false, /* pc_relative */
  401. 0, /* bitpos */
  402. complain_overflow_bitfield, /* complain_on_overflow */
  403. bfd_elf_generic_reloc, /* */
  404. "R_MN10300_GLOB_DAT", /* name */
  405. false, /* partial_inplace */
  406. 0xffffffff, /* src_mask */
  407. 0xffffffff, /* dst_mask */
  408. false), /* pcrel_offset */
  409. HOWTO (R_MN10300_JMP_SLOT, /* type */
  410. 0, /* rightshift */
  411. 2, /* size (0 = byte, 1 = short, 2 = long) */
  412. 32, /* bitsize */
  413. false, /* pc_relative */
  414. 0, /* bitpos */
  415. complain_overflow_bitfield, /* complain_on_overflow */
  416. bfd_elf_generic_reloc, /* */
  417. "R_MN10300_JMP_SLOT", /* name */
  418. false, /* partial_inplace */
  419. 0xffffffff, /* src_mask */
  420. 0xffffffff, /* dst_mask */
  421. false), /* pcrel_offset */
  422. HOWTO (R_MN10300_RELATIVE, /* type */
  423. 0, /* rightshift */
  424. 2, /* size (0 = byte, 1 = short, 2 = long) */
  425. 32, /* bitsize */
  426. false, /* pc_relative */
  427. 0, /* bitpos */
  428. complain_overflow_bitfield, /* complain_on_overflow */
  429. bfd_elf_generic_reloc, /* */
  430. "R_MN10300_RELATIVE", /* name */
  431. false, /* partial_inplace */
  432. 0xffffffff, /* src_mask */
  433. 0xffffffff, /* dst_mask */
  434. false), /* pcrel_offset */
  435. HOWTO (R_MN10300_TLS_GD, /* type */
  436. 0, /* rightshift */
  437. 2, /* size (0 = byte, 1 = short, 2 = long) */
  438. 32, /* bitsize */
  439. false, /* pc_relative */
  440. 0, /* bitpos */
  441. complain_overflow_bitfield, /* complain_on_overflow */
  442. bfd_elf_generic_reloc, /* */
  443. "R_MN10300_TLS_GD", /* name */
  444. false, /* partial_inplace */
  445. 0xffffffff, /* src_mask */
  446. 0xffffffff, /* dst_mask */
  447. false), /* pcrel_offset */
  448. HOWTO (R_MN10300_TLS_LD, /* type */
  449. 0, /* rightshift */
  450. 2, /* size (0 = byte, 1 = short, 2 = long) */
  451. 32, /* bitsize */
  452. false, /* pc_relative */
  453. 0, /* bitpos */
  454. complain_overflow_bitfield, /* complain_on_overflow */
  455. bfd_elf_generic_reloc, /* */
  456. "R_MN10300_TLS_LD", /* name */
  457. false, /* partial_inplace */
  458. 0xffffffff, /* src_mask */
  459. 0xffffffff, /* dst_mask */
  460. false), /* pcrel_offset */
  461. HOWTO (R_MN10300_TLS_LDO, /* type */
  462. 0, /* rightshift */
  463. 2, /* size (0 = byte, 1 = short, 2 = long) */
  464. 32, /* bitsize */
  465. false, /* pc_relative */
  466. 0, /* bitpos */
  467. complain_overflow_bitfield, /* complain_on_overflow */
  468. bfd_elf_generic_reloc, /* */
  469. "R_MN10300_TLS_LDO", /* name */
  470. false, /* partial_inplace */
  471. 0xffffffff, /* src_mask */
  472. 0xffffffff, /* dst_mask */
  473. false), /* pcrel_offset */
  474. HOWTO (R_MN10300_TLS_GOTIE, /* type */
  475. 0, /* rightshift */
  476. 2, /* size (0 = byte, 1 = short, 2 = long) */
  477. 32, /* bitsize */
  478. false, /* pc_relative */
  479. 0, /* bitpos */
  480. complain_overflow_bitfield, /* complain_on_overflow */
  481. bfd_elf_generic_reloc, /* */
  482. "R_MN10300_TLS_GOTIE", /* name */
  483. false, /* partial_inplace */
  484. 0xffffffff, /* src_mask */
  485. 0xffffffff, /* dst_mask */
  486. false), /* pcrel_offset */
  487. HOWTO (R_MN10300_TLS_IE, /* type */
  488. 0, /* rightshift */
  489. 2, /* size (0 = byte, 1 = short, 2 = long) */
  490. 32, /* bitsize */
  491. false, /* pc_relative */
  492. 0, /* bitpos */
  493. complain_overflow_bitfield, /* complain_on_overflow */
  494. bfd_elf_generic_reloc, /* */
  495. "R_MN10300_TLS_IE", /* name */
  496. false, /* partial_inplace */
  497. 0xffffffff, /* src_mask */
  498. 0xffffffff, /* dst_mask */
  499. false), /* pcrel_offset */
  500. HOWTO (R_MN10300_TLS_LE, /* type */
  501. 0, /* rightshift */
  502. 2, /* size (0 = byte, 1 = short, 2 = long) */
  503. 32, /* bitsize */
  504. false, /* pc_relative */
  505. 0, /* bitpos */
  506. complain_overflow_bitfield, /* complain_on_overflow */
  507. bfd_elf_generic_reloc, /* */
  508. "R_MN10300_TLS_LE", /* name */
  509. false, /* partial_inplace */
  510. 0xffffffff, /* src_mask */
  511. 0xffffffff, /* dst_mask */
  512. false), /* pcrel_offset */
  513. HOWTO (R_MN10300_TLS_DTPMOD, /* type */
  514. 0, /* rightshift */
  515. 2, /* size (0 = byte, 1 = short, 2 = long) */
  516. 32, /* bitsize */
  517. false, /* pc_relative */
  518. 0, /* bitpos */
  519. complain_overflow_bitfield, /* complain_on_overflow */
  520. bfd_elf_generic_reloc, /* */
  521. "R_MN10300_TLS_DTPMOD", /* name */
  522. false, /* partial_inplace */
  523. 0xffffffff, /* src_mask */
  524. 0xffffffff, /* dst_mask */
  525. false), /* pcrel_offset */
  526. HOWTO (R_MN10300_TLS_DTPOFF, /* type */
  527. 0, /* rightshift */
  528. 2, /* size (0 = byte, 1 = short, 2 = long) */
  529. 32, /* bitsize */
  530. false, /* pc_relative */
  531. 0, /* bitpos */
  532. complain_overflow_bitfield, /* complain_on_overflow */
  533. bfd_elf_generic_reloc, /* */
  534. "R_MN10300_TLS_DTPOFF", /* name */
  535. false, /* partial_inplace */
  536. 0xffffffff, /* src_mask */
  537. 0xffffffff, /* dst_mask */
  538. false), /* pcrel_offset */
  539. HOWTO (R_MN10300_TLS_TPOFF, /* type */
  540. 0, /* rightshift */
  541. 2, /* size (0 = byte, 1 = short, 2 = long) */
  542. 32, /* bitsize */
  543. false, /* pc_relative */
  544. 0, /* bitpos */
  545. complain_overflow_bitfield, /* complain_on_overflow */
  546. bfd_elf_generic_reloc, /* */
  547. "R_MN10300_TLS_TPOFF", /* name */
  548. false, /* partial_inplace */
  549. 0xffffffff, /* src_mask */
  550. 0xffffffff, /* dst_mask */
  551. false), /* pcrel_offset */
  552. HOWTO (R_MN10300_SYM_DIFF, /* type */
  553. 0, /* rightshift */
  554. 2, /* size (0 = byte, 1 = short, 2 = long) */
  555. 32, /* bitsize */
  556. false, /* pc_relative */
  557. 0, /* bitpos */
  558. complain_overflow_dont,/* complain_on_overflow */
  559. NULL, /* special handler. */
  560. "R_MN10300_SYM_DIFF", /* name */
  561. false, /* partial_inplace */
  562. 0xffffffff, /* src_mask */
  563. 0xffffffff, /* dst_mask */
  564. false), /* pcrel_offset */
  565. HOWTO (R_MN10300_ALIGN, /* type */
  566. 0, /* rightshift */
  567. 0, /* size (0 = byte, 1 = short, 2 = long) */
  568. 32, /* bitsize */
  569. false, /* pc_relative */
  570. 0, /* bitpos */
  571. complain_overflow_dont,/* complain_on_overflow */
  572. NULL, /* special handler. */
  573. "R_MN10300_ALIGN", /* name */
  574. false, /* partial_inplace */
  575. 0, /* src_mask */
  576. 0, /* dst_mask */
  577. false) /* pcrel_offset */
  578. };
  579. struct mn10300_reloc_map
  580. {
  581. bfd_reloc_code_real_type bfd_reloc_val;
  582. unsigned char elf_reloc_val;
  583. };
  584. static const struct mn10300_reloc_map mn10300_reloc_map[] =
  585. {
  586. { BFD_RELOC_NONE, R_MN10300_NONE, },
  587. { BFD_RELOC_32, R_MN10300_32, },
  588. { BFD_RELOC_16, R_MN10300_16, },
  589. { BFD_RELOC_8, R_MN10300_8, },
  590. { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
  591. { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
  592. { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
  593. { BFD_RELOC_24, R_MN10300_24, },
  594. { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
  595. { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
  596. { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
  597. { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
  598. { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
  599. { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
  600. { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
  601. { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
  602. { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
  603. { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
  604. { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
  605. { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
  606. { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
  607. { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
  608. { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
  609. { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
  610. { BFD_RELOC_MN10300_TLS_GD, R_MN10300_TLS_GD },
  611. { BFD_RELOC_MN10300_TLS_LD, R_MN10300_TLS_LD },
  612. { BFD_RELOC_MN10300_TLS_LDO, R_MN10300_TLS_LDO },
  613. { BFD_RELOC_MN10300_TLS_GOTIE, R_MN10300_TLS_GOTIE },
  614. { BFD_RELOC_MN10300_TLS_IE, R_MN10300_TLS_IE },
  615. { BFD_RELOC_MN10300_TLS_LE, R_MN10300_TLS_LE },
  616. { BFD_RELOC_MN10300_TLS_DTPMOD, R_MN10300_TLS_DTPMOD },
  617. { BFD_RELOC_MN10300_TLS_DTPOFF, R_MN10300_TLS_DTPOFF },
  618. { BFD_RELOC_MN10300_TLS_TPOFF, R_MN10300_TLS_TPOFF },
  619. { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF },
  620. { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN }
  621. };
  622. /* Create the GOT section. */
  623. static bool
  624. _bfd_mn10300_elf_create_got_section (bfd * abfd,
  625. struct bfd_link_info * info)
  626. {
  627. flagword flags;
  628. flagword pltflags;
  629. asection * s;
  630. struct elf_link_hash_entry * h;
  631. const struct elf_backend_data * bed = get_elf_backend_data (abfd);
  632. struct elf_link_hash_table *htab;
  633. int ptralign;
  634. /* This function may be called more than once. */
  635. htab = elf_hash_table (info);
  636. if (htab->sgot != NULL)
  637. return true;
  638. switch (bed->s->arch_size)
  639. {
  640. case 32:
  641. ptralign = 2;
  642. break;
  643. case 64:
  644. ptralign = 3;
  645. break;
  646. default:
  647. bfd_set_error (bfd_error_bad_value);
  648. return false;
  649. }
  650. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  651. | SEC_LINKER_CREATED);
  652. pltflags = flags;
  653. pltflags |= SEC_CODE;
  654. if (bed->plt_not_loaded)
  655. pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
  656. if (bed->plt_readonly)
  657. pltflags |= SEC_READONLY;
  658. s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
  659. htab->splt = s;
  660. if (s == NULL
  661. || !bfd_set_section_alignment (s, bed->plt_alignment))
  662. return false;
  663. /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  664. .plt section. */
  665. if (bed->want_plt_sym)
  666. {
  667. h = _bfd_elf_define_linkage_sym (abfd, info, s,
  668. "_PROCEDURE_LINKAGE_TABLE_");
  669. htab->hplt = h;
  670. if (h == NULL)
  671. return false;
  672. }
  673. s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  674. htab->sgot = s;
  675. if (s == NULL
  676. || !bfd_set_section_alignment (s, ptralign))
  677. return false;
  678. if (bed->want_got_plt)
  679. {
  680. s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  681. htab->sgotplt = s;
  682. if (s == NULL
  683. || !bfd_set_section_alignment (s, ptralign))
  684. return false;
  685. }
  686. /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  687. (or .got.plt) section. We don't do this in the linker script
  688. because we don't want to define the symbol if we are not creating
  689. a global offset table. */
  690. h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
  691. htab->hgot = h;
  692. if (h == NULL)
  693. return false;
  694. /* The first bit of the global offset table is the header. */
  695. s->size += bed->got_header_size;
  696. return true;
  697. }
  698. static reloc_howto_type *
  699. bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  700. bfd_reloc_code_real_type code)
  701. {
  702. unsigned int i;
  703. for (i = ARRAY_SIZE (mn10300_reloc_map); i--;)
  704. if (mn10300_reloc_map[i].bfd_reloc_val == code)
  705. return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
  706. return NULL;
  707. }
  708. static reloc_howto_type *
  709. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  710. const char *r_name)
  711. {
  712. unsigned int i;
  713. for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;)
  714. if (elf_mn10300_howto_table[i].name != NULL
  715. && strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
  716. return elf_mn10300_howto_table + i;
  717. return NULL;
  718. }
  719. /* Set the howto pointer for an MN10300 ELF reloc. */
  720. static bool
  721. mn10300_info_to_howto (bfd *abfd,
  722. arelent *cache_ptr,
  723. Elf_Internal_Rela *dst)
  724. {
  725. unsigned int r_type;
  726. r_type = ELF32_R_TYPE (dst->r_info);
  727. if (r_type >= R_MN10300_MAX)
  728. {
  729. /* xgettext:c-format */
  730. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  731. abfd, r_type);
  732. bfd_set_error (bfd_error_bad_value);
  733. return false;
  734. }
  735. cache_ptr->howto = elf_mn10300_howto_table + r_type;
  736. return true;
  737. }
  738. static int
  739. elf_mn10300_tls_transition (struct bfd_link_info * info,
  740. int r_type,
  741. struct elf_link_hash_entry * h,
  742. asection * sec,
  743. bool counting)
  744. {
  745. bool is_local;
  746. if (r_type == R_MN10300_TLS_GD
  747. && h != NULL
  748. && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_IE)
  749. return R_MN10300_TLS_GOTIE;
  750. if (bfd_link_pic (info))
  751. return r_type;
  752. if (! (sec->flags & SEC_CODE))
  753. return r_type;
  754. if (! counting && h != NULL && ! elf_hash_table (info)->dynamic_sections_created)
  755. is_local = true;
  756. else
  757. is_local = SYMBOL_CALLS_LOCAL (info, h);
  758. /* For the main program, these are the transitions we do. */
  759. switch (r_type)
  760. {
  761. case R_MN10300_TLS_GD: return is_local ? R_MN10300_TLS_LE : R_MN10300_TLS_GOTIE;
  762. case R_MN10300_TLS_LD: return R_MN10300_NONE;
  763. case R_MN10300_TLS_LDO: return R_MN10300_TLS_LE;
  764. case R_MN10300_TLS_IE:
  765. case R_MN10300_TLS_GOTIE: return is_local ? R_MN10300_TLS_LE : r_type;
  766. }
  767. return r_type;
  768. }
  769. /* Return the relocation value for @tpoff relocation
  770. if STT_TLS virtual address is ADDRESS. */
  771. static bfd_vma
  772. dtpoff (struct bfd_link_info * info, bfd_vma address)
  773. {
  774. struct elf_link_hash_table *htab = elf_hash_table (info);
  775. /* If tls_sec is NULL, we should have signalled an error already. */
  776. if (htab->tls_sec == NULL)
  777. return 0;
  778. return address - htab->tls_sec->vma;
  779. }
  780. /* Return the relocation value for @tpoff relocation
  781. if STT_TLS virtual address is ADDRESS. */
  782. static bfd_vma
  783. tpoff (struct bfd_link_info * info, bfd_vma address)
  784. {
  785. struct elf_link_hash_table *htab = elf_hash_table (info);
  786. /* If tls_sec is NULL, we should have signalled an error already. */
  787. if (htab->tls_sec == NULL)
  788. return 0;
  789. return address - (htab->tls_size + htab->tls_sec->vma);
  790. }
  791. /* Returns nonzero if there's a R_MN10300_PLT32 reloc that we now need
  792. to skip, after this one. The actual value is the offset between
  793. this reloc and the PLT reloc. */
  794. static int
  795. mn10300_do_tls_transition (bfd * input_bfd,
  796. unsigned int r_type,
  797. unsigned int tls_r_type,
  798. bfd_byte * contents,
  799. bfd_vma offset)
  800. {
  801. bfd_byte *op = contents + offset;
  802. int gotreg = 0;
  803. #define TLS_PAIR(r1,r2) ((r1) * R_MN10300_MAX + (r2))
  804. /* This is common to all GD/LD transitions, so break it out. */
  805. if (r_type == R_MN10300_TLS_GD
  806. || r_type == R_MN10300_TLS_LD)
  807. {
  808. op -= 2;
  809. /* mov imm,d0. */
  810. BFD_ASSERT (bfd_get_8 (input_bfd, op) == 0xFC);
  811. BFD_ASSERT (bfd_get_8 (input_bfd, op + 1) == 0xCC);
  812. /* add aN,d0. */
  813. BFD_ASSERT (bfd_get_8 (input_bfd, op + 6) == 0xF1);
  814. gotreg = (bfd_get_8 (input_bfd, op + 7) & 0x0c) >> 2;
  815. /* Call. */
  816. BFD_ASSERT (bfd_get_8 (input_bfd, op + 8) == 0xDD);
  817. }
  818. switch (TLS_PAIR (r_type, tls_r_type))
  819. {
  820. case TLS_PAIR (R_MN10300_TLS_GD, R_MN10300_TLS_GOTIE):
  821. {
  822. /* Keep track of which register we put GOTptr in. */
  823. /* mov (_x@indntpoff,a2),a0. */
  824. memcpy (op, "\xFC\x20\x00\x00\x00\x00", 6);
  825. op[1] |= gotreg;
  826. /* add e2,a0. */
  827. memcpy (op+6, "\xF9\x78\x28", 3);
  828. /* or 0x00000000, d0 - six byte nop. */
  829. memcpy (op+9, "\xFC\xE4\x00\x00\x00\x00", 6);
  830. }
  831. return 7;
  832. case TLS_PAIR (R_MN10300_TLS_GD, R_MN10300_TLS_LE):
  833. {
  834. /* Register is *always* a0. */
  835. /* mov _x@tpoff,a0. */
  836. memcpy (op, "\xFC\xDC\x00\x00\x00\x00", 6);
  837. /* add e2,a0. */
  838. memcpy (op+6, "\xF9\x78\x28", 3);
  839. /* or 0x00000000, d0 - six byte nop. */
  840. memcpy (op+9, "\xFC\xE4\x00\x00\x00\x00", 6);
  841. }
  842. return 7;
  843. case TLS_PAIR (R_MN10300_TLS_LD, R_MN10300_NONE):
  844. {
  845. /* Register is *always* a0. */
  846. /* mov e2,a0. */
  847. memcpy (op, "\xF5\x88", 2);
  848. /* or 0x00000000, d0 - six byte nop. */
  849. memcpy (op+2, "\xFC\xE4\x00\x00\x00\x00", 6);
  850. /* or 0x00000000, e2 - seven byte nop. */
  851. memcpy (op+8, "\xFE\x19\x22\x00\x00\x00\x00", 7);
  852. }
  853. return 7;
  854. case TLS_PAIR (R_MN10300_TLS_LDO, R_MN10300_TLS_LE):
  855. /* No changes needed, just the reloc change. */
  856. return 0;
  857. /* These are a little tricky, because we have to detect which
  858. opcode is being used (they're different sizes, with the reloc
  859. at different offsets within the opcode) and convert each
  860. accordingly, copying the operands as needed. The conversions
  861. we do are as follows (IE,GOTIE,LE):
  862. 1111 1100 1010 01Dn [-- abs32 --] MOV (x@indntpoff),Dn
  863. 1111 1100 0000 DnAm [-- abs32 --] MOV (x@gotntpoff,Am),Dn
  864. 1111 1100 1100 11Dn [-- abs32 --] MOV x@tpoff,Dn
  865. 1111 1100 1010 00An [-- abs32 --] MOV (x@indntpoff),An
  866. 1111 1100 0010 AnAm [-- abs32 --] MOV (x@gotntpoff,Am),An
  867. 1111 1100 1101 11An [-- abs32 --] MOV x@tpoff,An
  868. 1111 1110 0000 1110 Rnnn Xxxx [-- abs32 --] MOV (x@indntpoff),Rn
  869. 1111 1110 0000 1010 Rnnn Rmmm [-- abs32 --] MOV (x@indntpoff,Rm),Rn
  870. 1111 1110 0000 1000 Rnnn Xxxx [-- abs32 --] MOV x@tpoff,Rn
  871. Since the GOT pointer is always $a2, we assume the last
  872. normally won't happen, but let's be paranoid and plan for the
  873. day that GCC optimizes it somewhow. */
  874. case TLS_PAIR (R_MN10300_TLS_IE, R_MN10300_TLS_LE):
  875. if (op[-2] == 0xFC)
  876. {
  877. op -= 2;
  878. if ((op[1] & 0xFC) == 0xA4) /* Dn */
  879. {
  880. op[1] &= 0x03; /* Leaves Dn. */
  881. op[1] |= 0xCC;
  882. }
  883. else /* An */
  884. {
  885. op[1] &= 0x03; /* Leaves An. */
  886. op[1] |= 0xDC;
  887. }
  888. }
  889. else if (op[-3] == 0xFE)
  890. op[-2] = 0x08;
  891. else
  892. abort ();
  893. break;
  894. case TLS_PAIR (R_MN10300_TLS_GOTIE, R_MN10300_TLS_LE):
  895. if (op[-2] == 0xFC)
  896. {
  897. op -= 2;
  898. if ((op[1] & 0xF0) == 0x00) /* Dn */
  899. {
  900. op[1] &= 0x0C; /* Leaves Dn. */
  901. op[1] >>= 2;
  902. op[1] |= 0xCC;
  903. }
  904. else /* An */
  905. {
  906. op[1] &= 0x0C; /* Leaves An. */
  907. op[1] >>= 2;
  908. op[1] |= 0xDC;
  909. }
  910. }
  911. else if (op[-3] == 0xFE)
  912. op[-2] = 0x08;
  913. else
  914. abort ();
  915. break;
  916. default:
  917. _bfd_error_handler
  918. /* xgettext:c-format */
  919. (_("%pB: unsupported transition from %s to %s"),
  920. input_bfd,
  921. elf_mn10300_howto_table[r_type].name,
  922. elf_mn10300_howto_table[tls_r_type].name);
  923. break;
  924. }
  925. #undef TLS_PAIR
  926. return 0;
  927. }
  928. /* Look through the relocs for a section during the first phase.
  929. Since we don't do .gots or .plts, we just need to consider the
  930. virtual table relocs for gc. */
  931. static bool
  932. mn10300_elf_check_relocs (bfd *abfd,
  933. struct bfd_link_info *info,
  934. asection *sec,
  935. const Elf_Internal_Rela *relocs)
  936. {
  937. struct elf32_mn10300_link_hash_table * htab = elf32_mn10300_hash_table (info);
  938. bool sym_diff_reloc_seen;
  939. Elf_Internal_Shdr *symtab_hdr;
  940. Elf_Internal_Sym * isymbuf = NULL;
  941. struct elf_link_hash_entry **sym_hashes;
  942. const Elf_Internal_Rela *rel;
  943. const Elf_Internal_Rela *rel_end;
  944. bfd * dynobj;
  945. bfd_vma * local_got_offsets;
  946. asection * sgot;
  947. asection * srelgot;
  948. asection * sreloc;
  949. bool result = false;
  950. sgot = NULL;
  951. srelgot = NULL;
  952. sreloc = NULL;
  953. if (bfd_link_relocatable (info))
  954. return true;
  955. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  956. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  957. sym_hashes = elf_sym_hashes (abfd);
  958. dynobj = elf_hash_table (info)->dynobj;
  959. local_got_offsets = elf_local_got_offsets (abfd);
  960. rel_end = relocs + sec->reloc_count;
  961. sym_diff_reloc_seen = false;
  962. for (rel = relocs; rel < rel_end; rel++)
  963. {
  964. struct elf_link_hash_entry *h;
  965. unsigned long r_symndx;
  966. unsigned int r_type;
  967. int tls_type = GOT_NORMAL;
  968. r_symndx = ELF32_R_SYM (rel->r_info);
  969. if (r_symndx < symtab_hdr->sh_info)
  970. h = NULL;
  971. else
  972. {
  973. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  974. while (h->root.type == bfd_link_hash_indirect
  975. || h->root.type == bfd_link_hash_warning)
  976. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  977. }
  978. r_type = ELF32_R_TYPE (rel->r_info);
  979. r_type = elf_mn10300_tls_transition (info, r_type, h, sec, true);
  980. /* Some relocs require a global offset table. */
  981. if (dynobj == NULL)
  982. {
  983. switch (r_type)
  984. {
  985. case R_MN10300_GOT32:
  986. case R_MN10300_GOT24:
  987. case R_MN10300_GOT16:
  988. case R_MN10300_GOTOFF32:
  989. case R_MN10300_GOTOFF24:
  990. case R_MN10300_GOTOFF16:
  991. case R_MN10300_GOTPC32:
  992. case R_MN10300_GOTPC16:
  993. case R_MN10300_TLS_GD:
  994. case R_MN10300_TLS_LD:
  995. case R_MN10300_TLS_GOTIE:
  996. case R_MN10300_TLS_IE:
  997. elf_hash_table (info)->dynobj = dynobj = abfd;
  998. if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
  999. goto fail;
  1000. break;
  1001. default:
  1002. break;
  1003. }
  1004. }
  1005. switch (r_type)
  1006. {
  1007. /* This relocation describes the C++ object vtable hierarchy.
  1008. Reconstruct it for later use during GC. */
  1009. case R_MN10300_GNU_VTINHERIT:
  1010. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  1011. goto fail;
  1012. break;
  1013. /* This relocation describes which C++ vtable entries are actually
  1014. used. Record for later use during GC. */
  1015. case R_MN10300_GNU_VTENTRY:
  1016. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  1017. goto fail;
  1018. break;
  1019. case R_MN10300_TLS_LD:
  1020. htab->tls_ldm_got.refcount ++;
  1021. tls_type = GOT_TLS_LD;
  1022. if (htab->tls_ldm_got.got_allocated)
  1023. break;
  1024. goto create_got;
  1025. case R_MN10300_TLS_IE:
  1026. case R_MN10300_TLS_GOTIE:
  1027. if (bfd_link_pic (info))
  1028. info->flags |= DF_STATIC_TLS;
  1029. /* Fall through */
  1030. case R_MN10300_TLS_GD:
  1031. case R_MN10300_GOT32:
  1032. case R_MN10300_GOT24:
  1033. case R_MN10300_GOT16:
  1034. create_got:
  1035. /* This symbol requires a global offset table entry. */
  1036. switch (r_type)
  1037. {
  1038. case R_MN10300_TLS_IE:
  1039. case R_MN10300_TLS_GOTIE: tls_type = GOT_TLS_IE; break;
  1040. case R_MN10300_TLS_GD: tls_type = GOT_TLS_GD; break;
  1041. default: tls_type = GOT_NORMAL; break;
  1042. }
  1043. sgot = htab->root.sgot;
  1044. srelgot = htab->root.srelgot;
  1045. BFD_ASSERT (sgot != NULL && srelgot != NULL);
  1046. if (r_type == R_MN10300_TLS_LD)
  1047. {
  1048. htab->tls_ldm_got.offset = sgot->size;
  1049. htab->tls_ldm_got.got_allocated ++;
  1050. }
  1051. else if (h != NULL)
  1052. {
  1053. if (elf_mn10300_hash_entry (h)->tls_type != tls_type
  1054. && elf_mn10300_hash_entry (h)->tls_type != GOT_UNKNOWN)
  1055. {
  1056. if (tls_type == GOT_TLS_IE
  1057. && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_GD)
  1058. /* No change - this is ok. */;
  1059. else if (tls_type == GOT_TLS_GD
  1060. && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_IE)
  1061. /* Transition GD->IE. */
  1062. tls_type = GOT_TLS_IE;
  1063. else
  1064. _bfd_error_handler
  1065. /* xgettext:c-format */
  1066. (_("%pB: %s' accessed both as normal and thread local symbol"),
  1067. abfd, h ? h->root.root.string : "<local>");
  1068. }
  1069. elf_mn10300_hash_entry (h)->tls_type = tls_type;
  1070. if (h->got.offset != (bfd_vma) -1)
  1071. /* We have already allocated space in the .got. */
  1072. break;
  1073. h->got.offset = sgot->size;
  1074. if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
  1075. /* Make sure this symbol is output as a dynamic symbol. */
  1076. && h->dynindx == -1)
  1077. {
  1078. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1079. goto fail;
  1080. }
  1081. srelgot->size += sizeof (Elf32_External_Rela);
  1082. if (r_type == R_MN10300_TLS_GD)
  1083. srelgot->size += sizeof (Elf32_External_Rela);
  1084. }
  1085. else
  1086. {
  1087. /* This is a global offset table entry for a local
  1088. symbol. */
  1089. if (local_got_offsets == NULL)
  1090. {
  1091. size_t size;
  1092. unsigned int i;
  1093. size = symtab_hdr->sh_info * (sizeof (bfd_vma) + sizeof (char));
  1094. local_got_offsets = bfd_alloc (abfd, size);
  1095. if (local_got_offsets == NULL)
  1096. goto fail;
  1097. elf_local_got_offsets (abfd) = local_got_offsets;
  1098. elf_mn10300_local_got_tls_type (abfd)
  1099. = (char *) (local_got_offsets + symtab_hdr->sh_info);
  1100. for (i = 0; i < symtab_hdr->sh_info; i++)
  1101. local_got_offsets[i] = (bfd_vma) -1;
  1102. }
  1103. if (local_got_offsets[r_symndx] != (bfd_vma) -1)
  1104. /* We have already allocated space in the .got. */
  1105. break;
  1106. local_got_offsets[r_symndx] = sgot->size;
  1107. if (bfd_link_pic (info))
  1108. {
  1109. /* If we are generating a shared object, we need to
  1110. output a R_MN10300_RELATIVE reloc so that the dynamic
  1111. linker can adjust this GOT entry. */
  1112. srelgot->size += sizeof (Elf32_External_Rela);
  1113. if (r_type == R_MN10300_TLS_GD)
  1114. /* And a R_MN10300_TLS_DTPOFF reloc as well. */
  1115. srelgot->size += sizeof (Elf32_External_Rela);
  1116. }
  1117. elf_mn10300_local_got_tls_type (abfd) [r_symndx] = tls_type;
  1118. }
  1119. sgot->size += 4;
  1120. if (r_type == R_MN10300_TLS_GD
  1121. || r_type == R_MN10300_TLS_LD)
  1122. sgot->size += 4;
  1123. goto need_shared_relocs;
  1124. case R_MN10300_PLT32:
  1125. case R_MN10300_PLT16:
  1126. /* This symbol requires a procedure linkage table entry. We
  1127. actually build the entry in adjust_dynamic_symbol,
  1128. because this might be a case of linking PIC code which is
  1129. never referenced by a dynamic object, in which case we
  1130. don't need to generate a procedure linkage table entry
  1131. after all. */
  1132. /* If this is a local symbol, we resolve it directly without
  1133. creating a procedure linkage table entry. */
  1134. if (h == NULL)
  1135. continue;
  1136. if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
  1137. || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
  1138. break;
  1139. h->needs_plt = 1;
  1140. break;
  1141. case R_MN10300_24:
  1142. case R_MN10300_16:
  1143. case R_MN10300_8:
  1144. case R_MN10300_PCREL32:
  1145. case R_MN10300_PCREL16:
  1146. case R_MN10300_PCREL8:
  1147. if (h != NULL)
  1148. h->non_got_ref = 1;
  1149. break;
  1150. case R_MN10300_SYM_DIFF:
  1151. sym_diff_reloc_seen = true;
  1152. break;
  1153. case R_MN10300_32:
  1154. if (h != NULL)
  1155. h->non_got_ref = 1;
  1156. need_shared_relocs:
  1157. /* If we are creating a shared library, then we
  1158. need to copy the reloc into the shared library. */
  1159. if (bfd_link_pic (info)
  1160. && (sec->flags & SEC_ALLOC) != 0
  1161. /* Do not generate a dynamic reloc for a
  1162. reloc associated with a SYM_DIFF operation. */
  1163. && ! sym_diff_reloc_seen)
  1164. {
  1165. asection * sym_section = NULL;
  1166. /* Find the section containing the
  1167. symbol involved in the relocation. */
  1168. if (h == NULL)
  1169. {
  1170. Elf_Internal_Sym * isym;
  1171. if (isymbuf == NULL)
  1172. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  1173. symtab_hdr->sh_info, 0,
  1174. NULL, NULL, NULL);
  1175. if (isymbuf)
  1176. {
  1177. isym = isymbuf + r_symndx;
  1178. /* All we care about is whether this local symbol is absolute. */
  1179. if (isym->st_shndx == SHN_ABS)
  1180. sym_section = bfd_abs_section_ptr;
  1181. }
  1182. }
  1183. else
  1184. {
  1185. if (h->root.type == bfd_link_hash_defined
  1186. || h->root.type == bfd_link_hash_defweak)
  1187. sym_section = h->root.u.def.section;
  1188. }
  1189. /* If the symbol is absolute then the relocation can
  1190. be resolved during linking and there is no need for
  1191. a dynamic reloc. */
  1192. if (sym_section != bfd_abs_section_ptr)
  1193. {
  1194. /* When creating a shared object, we must copy these
  1195. reloc types into the output file. We create a reloc
  1196. section in dynobj and make room for this reloc. */
  1197. if (sreloc == NULL)
  1198. {
  1199. sreloc = _bfd_elf_make_dynamic_reloc_section
  1200. (sec, dynobj, 2, abfd, /*rela?*/ true);
  1201. if (sreloc == NULL)
  1202. goto fail;
  1203. }
  1204. sreloc->size += sizeof (Elf32_External_Rela);
  1205. }
  1206. }
  1207. break;
  1208. }
  1209. if (ELF32_R_TYPE (rel->r_info) != R_MN10300_SYM_DIFF)
  1210. sym_diff_reloc_seen = false;
  1211. }
  1212. result = true;
  1213. fail:
  1214. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  1215. free (isymbuf);
  1216. return result;
  1217. }
  1218. /* Return the section that should be marked against GC for a given
  1219. relocation. */
  1220. static asection *
  1221. mn10300_elf_gc_mark_hook (asection *sec,
  1222. struct bfd_link_info *info,
  1223. Elf_Internal_Rela *rel,
  1224. struct elf_link_hash_entry *h,
  1225. Elf_Internal_Sym *sym)
  1226. {
  1227. if (h != NULL)
  1228. switch (ELF32_R_TYPE (rel->r_info))
  1229. {
  1230. case R_MN10300_GNU_VTINHERIT:
  1231. case R_MN10300_GNU_VTENTRY:
  1232. return NULL;
  1233. }
  1234. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1235. }
  1236. /* Perform a relocation as part of a final link. */
  1237. static bfd_reloc_status_type
  1238. mn10300_elf_final_link_relocate (reloc_howto_type *howto,
  1239. bfd *input_bfd,
  1240. bfd *output_bfd ATTRIBUTE_UNUSED,
  1241. asection *input_section,
  1242. bfd_byte *contents,
  1243. bfd_vma offset,
  1244. bfd_vma value,
  1245. bfd_vma addend,
  1246. struct elf_link_hash_entry * h,
  1247. unsigned long symndx,
  1248. struct bfd_link_info *info,
  1249. asection *sym_sec ATTRIBUTE_UNUSED,
  1250. int is_local ATTRIBUTE_UNUSED)
  1251. {
  1252. struct elf32_mn10300_link_hash_table * htab = elf32_mn10300_hash_table (info);
  1253. static asection * sym_diff_section;
  1254. static bfd_vma sym_diff_value;
  1255. bool is_sym_diff_reloc;
  1256. unsigned long r_type = howto->type;
  1257. bfd_byte * hit_data = contents + offset;
  1258. bfd * dynobj;
  1259. asection * sgot;
  1260. asection * splt;
  1261. asection * sreloc;
  1262. dynobj = elf_hash_table (info)->dynobj;
  1263. sgot = NULL;
  1264. splt = NULL;
  1265. sreloc = NULL;
  1266. switch (r_type)
  1267. {
  1268. case R_MN10300_24:
  1269. case R_MN10300_16:
  1270. case R_MN10300_8:
  1271. case R_MN10300_PCREL8:
  1272. case R_MN10300_PCREL16:
  1273. case R_MN10300_PCREL32:
  1274. case R_MN10300_GOTOFF32:
  1275. case R_MN10300_GOTOFF24:
  1276. case R_MN10300_GOTOFF16:
  1277. if (bfd_link_pic (info)
  1278. && (input_section->flags & SEC_ALLOC) != 0
  1279. && h != NULL
  1280. && ! SYMBOL_REFERENCES_LOCAL (info, h))
  1281. return bfd_reloc_dangerous;
  1282. /* Fall through. */
  1283. case R_MN10300_GOT32:
  1284. /* Issue 2052223:
  1285. Taking the address of a protected function in a shared library
  1286. is illegal. Issue an error message here. */
  1287. if (bfd_link_pic (info)
  1288. && (input_section->flags & SEC_ALLOC) != 0
  1289. && h != NULL
  1290. && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED
  1291. && (h->type == STT_FUNC || h->type == STT_GNU_IFUNC)
  1292. && ! SYMBOL_REFERENCES_LOCAL (info, h))
  1293. return bfd_reloc_dangerous;
  1294. }
  1295. is_sym_diff_reloc = false;
  1296. if (sym_diff_section != NULL)
  1297. {
  1298. BFD_ASSERT (sym_diff_section == input_section);
  1299. switch (r_type)
  1300. {
  1301. case R_MN10300_32:
  1302. case R_MN10300_24:
  1303. case R_MN10300_16:
  1304. case R_MN10300_8:
  1305. value -= sym_diff_value;
  1306. /* If we are computing a 32-bit value for the location lists
  1307. and the result is 0 then we add one to the value. A zero
  1308. value can result because of linker relaxation deleteing
  1309. prologue instructions and using a value of 1 (for the begin
  1310. and end offsets in the location list entry) results in a
  1311. nul entry which does not prevent the following entries from
  1312. being parsed. */
  1313. if (r_type == R_MN10300_32
  1314. && value == 0
  1315. && strcmp (input_section->name, ".debug_loc") == 0)
  1316. value = 1;
  1317. sym_diff_section = NULL;
  1318. is_sym_diff_reloc = true;
  1319. break;
  1320. default:
  1321. sym_diff_section = NULL;
  1322. break;
  1323. }
  1324. }
  1325. switch (r_type)
  1326. {
  1327. case R_MN10300_SYM_DIFF:
  1328. BFD_ASSERT (addend == 0);
  1329. /* Cache the input section and value.
  1330. The offset is unreliable, since relaxation may
  1331. have reduced the following reloc's offset. */
  1332. sym_diff_section = input_section;
  1333. sym_diff_value = value;
  1334. return bfd_reloc_ok;
  1335. case R_MN10300_ALIGN:
  1336. case R_MN10300_NONE:
  1337. return bfd_reloc_ok;
  1338. case R_MN10300_32:
  1339. if (bfd_link_pic (info)
  1340. /* Do not generate relocs when an R_MN10300_32 has been used
  1341. with an R_MN10300_SYM_DIFF to compute a difference of two
  1342. symbols. */
  1343. && !is_sym_diff_reloc
  1344. /* Also, do not generate a reloc when the symbol associated
  1345. with the R_MN10300_32 reloc is absolute - there is no
  1346. need for a run time computation in this case. */
  1347. && sym_sec != bfd_abs_section_ptr
  1348. /* If the section is not going to be allocated at load time
  1349. then there is no need to generate relocs for it. */
  1350. && (input_section->flags & SEC_ALLOC) != 0)
  1351. {
  1352. Elf_Internal_Rela outrel;
  1353. bool skip, relocate;
  1354. /* When generating a shared object, these relocations are
  1355. copied into the output file to be resolved at run
  1356. time. */
  1357. if (sreloc == NULL)
  1358. {
  1359. sreloc = _bfd_elf_get_dynamic_reloc_section
  1360. (input_bfd, input_section, /*rela?*/ true);
  1361. if (sreloc == NULL)
  1362. return false;
  1363. }
  1364. skip = false;
  1365. outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
  1366. input_section, offset);
  1367. if (outrel.r_offset == (bfd_vma) -1)
  1368. skip = true;
  1369. outrel.r_offset += (input_section->output_section->vma
  1370. + input_section->output_offset);
  1371. if (skip)
  1372. {
  1373. memset (&outrel, 0, sizeof outrel);
  1374. relocate = false;
  1375. }
  1376. else
  1377. {
  1378. /* h->dynindx may be -1 if this symbol was marked to
  1379. become local. */
  1380. if (h == NULL
  1381. || SYMBOL_REFERENCES_LOCAL (info, h))
  1382. {
  1383. relocate = true;
  1384. outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
  1385. outrel.r_addend = value + addend;
  1386. }
  1387. else
  1388. {
  1389. BFD_ASSERT (h->dynindx != -1);
  1390. relocate = false;
  1391. outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
  1392. outrel.r_addend = value + addend;
  1393. }
  1394. }
  1395. bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  1396. (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
  1397. + sreloc->reloc_count));
  1398. ++sreloc->reloc_count;
  1399. /* If this reloc is against an external symbol, we do
  1400. not want to fiddle with the addend. Otherwise, we
  1401. need to include the symbol value so that it becomes
  1402. an addend for the dynamic reloc. */
  1403. if (! relocate)
  1404. return bfd_reloc_ok;
  1405. }
  1406. value += addend;
  1407. bfd_put_32 (input_bfd, value, hit_data);
  1408. return bfd_reloc_ok;
  1409. case R_MN10300_24:
  1410. value += addend;
  1411. if ((long) value > 0x7fffff || (long) value < -0x800000)
  1412. return bfd_reloc_overflow;
  1413. bfd_put_8 (input_bfd, value & 0xff, hit_data);
  1414. bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
  1415. bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
  1416. return bfd_reloc_ok;
  1417. case R_MN10300_16:
  1418. value += addend;
  1419. if ((long) value > 0x7fff || (long) value < -0x8000)
  1420. return bfd_reloc_overflow;
  1421. bfd_put_16 (input_bfd, value, hit_data);
  1422. return bfd_reloc_ok;
  1423. case R_MN10300_8:
  1424. value += addend;
  1425. if ((long) value > 0x7f || (long) value < -0x80)
  1426. return bfd_reloc_overflow;
  1427. bfd_put_8 (input_bfd, value, hit_data);
  1428. return bfd_reloc_ok;
  1429. case R_MN10300_PCREL8:
  1430. value -= (input_section->output_section->vma
  1431. + input_section->output_offset);
  1432. value -= offset;
  1433. value += addend;
  1434. if ((long) value > 0x7f || (long) value < -0x80)
  1435. return bfd_reloc_overflow;
  1436. bfd_put_8 (input_bfd, value, hit_data);
  1437. return bfd_reloc_ok;
  1438. case R_MN10300_PCREL16:
  1439. value -= (input_section->output_section->vma
  1440. + input_section->output_offset);
  1441. value -= offset;
  1442. value += addend;
  1443. if ((long) value > 0x7fff || (long) value < -0x8000)
  1444. return bfd_reloc_overflow;
  1445. bfd_put_16 (input_bfd, value, hit_data);
  1446. return bfd_reloc_ok;
  1447. case R_MN10300_PCREL32:
  1448. value -= (input_section->output_section->vma
  1449. + input_section->output_offset);
  1450. value -= offset;
  1451. value += addend;
  1452. bfd_put_32 (input_bfd, value, hit_data);
  1453. return bfd_reloc_ok;
  1454. case R_MN10300_GNU_VTINHERIT:
  1455. case R_MN10300_GNU_VTENTRY:
  1456. return bfd_reloc_ok;
  1457. case R_MN10300_GOTPC32:
  1458. if (dynobj == NULL)
  1459. return bfd_reloc_dangerous;
  1460. /* Use global offset table as symbol value. */
  1461. value = htab->root.sgot->output_section->vma;
  1462. value -= (input_section->output_section->vma
  1463. + input_section->output_offset);
  1464. value -= offset;
  1465. value += addend;
  1466. bfd_put_32 (input_bfd, value, hit_data);
  1467. return bfd_reloc_ok;
  1468. case R_MN10300_GOTPC16:
  1469. if (dynobj == NULL)
  1470. return bfd_reloc_dangerous;
  1471. /* Use global offset table as symbol value. */
  1472. value = htab->root.sgot->output_section->vma;
  1473. value -= (input_section->output_section->vma
  1474. + input_section->output_offset);
  1475. value -= offset;
  1476. value += addend;
  1477. if ((long) value > 0x7fff || (long) value < -0x8000)
  1478. return bfd_reloc_overflow;
  1479. bfd_put_16 (input_bfd, value, hit_data);
  1480. return bfd_reloc_ok;
  1481. case R_MN10300_GOTOFF32:
  1482. if (dynobj == NULL)
  1483. return bfd_reloc_dangerous;
  1484. value -= htab->root.sgot->output_section->vma;
  1485. value += addend;
  1486. bfd_put_32 (input_bfd, value, hit_data);
  1487. return bfd_reloc_ok;
  1488. case R_MN10300_GOTOFF24:
  1489. if (dynobj == NULL)
  1490. return bfd_reloc_dangerous;
  1491. value -= htab->root.sgot->output_section->vma;
  1492. value += addend;
  1493. if ((long) value > 0x7fffff || (long) value < -0x800000)
  1494. return bfd_reloc_overflow;
  1495. bfd_put_8 (input_bfd, value, hit_data);
  1496. bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
  1497. bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
  1498. return bfd_reloc_ok;
  1499. case R_MN10300_GOTOFF16:
  1500. if (dynobj == NULL)
  1501. return bfd_reloc_dangerous;
  1502. value -= htab->root.sgot->output_section->vma;
  1503. value += addend;
  1504. if ((long) value > 0x7fff || (long) value < -0x8000)
  1505. return bfd_reloc_overflow;
  1506. bfd_put_16 (input_bfd, value, hit_data);
  1507. return bfd_reloc_ok;
  1508. case R_MN10300_PLT32:
  1509. if (h != NULL
  1510. && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
  1511. && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
  1512. && h->plt.offset != (bfd_vma) -1)
  1513. {
  1514. if (dynobj == NULL)
  1515. return bfd_reloc_dangerous;
  1516. splt = htab->root.splt;
  1517. value = (splt->output_section->vma
  1518. + splt->output_offset
  1519. + h->plt.offset) - value;
  1520. }
  1521. value -= (input_section->output_section->vma
  1522. + input_section->output_offset);
  1523. value -= offset;
  1524. value += addend;
  1525. bfd_put_32 (input_bfd, value, hit_data);
  1526. return bfd_reloc_ok;
  1527. case R_MN10300_PLT16:
  1528. if (h != NULL
  1529. && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
  1530. && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
  1531. && h->plt.offset != (bfd_vma) -1)
  1532. {
  1533. if (dynobj == NULL)
  1534. return bfd_reloc_dangerous;
  1535. splt = htab->root.splt;
  1536. value = (splt->output_section->vma
  1537. + splt->output_offset
  1538. + h->plt.offset) - value;
  1539. }
  1540. value -= (input_section->output_section->vma
  1541. + input_section->output_offset);
  1542. value -= offset;
  1543. value += addend;
  1544. if ((long) value > 0x7fff || (long) value < -0x8000)
  1545. return bfd_reloc_overflow;
  1546. bfd_put_16 (input_bfd, value, hit_data);
  1547. return bfd_reloc_ok;
  1548. case R_MN10300_TLS_LDO:
  1549. value = dtpoff (info, value);
  1550. bfd_put_32 (input_bfd, value + addend, hit_data);
  1551. return bfd_reloc_ok;
  1552. case R_MN10300_TLS_LE:
  1553. value = tpoff (info, value);
  1554. bfd_put_32 (input_bfd, value + addend, hit_data);
  1555. return bfd_reloc_ok;
  1556. case R_MN10300_TLS_LD:
  1557. if (dynobj == NULL)
  1558. return bfd_reloc_dangerous;
  1559. sgot = htab->root.sgot;
  1560. BFD_ASSERT (sgot != NULL);
  1561. value = htab->tls_ldm_got.offset + sgot->output_offset;
  1562. bfd_put_32 (input_bfd, value, hit_data);
  1563. if (!htab->tls_ldm_got.rel_emitted)
  1564. {
  1565. asection *srelgot = htab->root.srelgot;
  1566. Elf_Internal_Rela rel;
  1567. BFD_ASSERT (srelgot != NULL);
  1568. htab->tls_ldm_got.rel_emitted ++;
  1569. rel.r_offset = (sgot->output_section->vma
  1570. + sgot->output_offset
  1571. + htab->tls_ldm_got.offset);
  1572. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + htab->tls_ldm_got.offset);
  1573. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + htab->tls_ldm_got.offset+4);
  1574. rel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPMOD);
  1575. rel.r_addend = 0;
  1576. bfd_elf32_swap_reloca_out (output_bfd, & rel,
  1577. (bfd_byte *) ((Elf32_External_Rela *) srelgot->contents
  1578. + srelgot->reloc_count));
  1579. ++ srelgot->reloc_count;
  1580. }
  1581. return bfd_reloc_ok;
  1582. case R_MN10300_TLS_GOTIE:
  1583. value = tpoff (info, value);
  1584. /* Fall Through. */
  1585. case R_MN10300_TLS_GD:
  1586. case R_MN10300_TLS_IE:
  1587. case R_MN10300_GOT32:
  1588. case R_MN10300_GOT24:
  1589. case R_MN10300_GOT16:
  1590. if (dynobj == NULL)
  1591. return bfd_reloc_dangerous;
  1592. sgot = htab->root.sgot;
  1593. if (r_type == R_MN10300_TLS_GD)
  1594. value = dtpoff (info, value);
  1595. if (h != NULL)
  1596. {
  1597. bfd_vma off;
  1598. off = h->got.offset;
  1599. /* Offsets in the GOT are allocated in check_relocs
  1600. which is not called for shared libraries... */
  1601. if (off == (bfd_vma) -1)
  1602. off = 0;
  1603. if (sgot->contents != NULL
  1604. && (! elf_hash_table (info)->dynamic_sections_created
  1605. || SYMBOL_REFERENCES_LOCAL (info, h)))
  1606. /* This is actually a static link, or it is a
  1607. -Bsymbolic link and the symbol is defined
  1608. locally, or the symbol was forced to be local
  1609. because of a version file. We must initialize
  1610. this entry in the global offset table.
  1611. When doing a dynamic link, we create a .rela.got
  1612. relocation entry to initialize the value. This
  1613. is done in the finish_dynamic_symbol routine. */
  1614. bfd_put_32 (output_bfd, value,
  1615. sgot->contents + off);
  1616. value = sgot->output_offset + off;
  1617. }
  1618. else
  1619. {
  1620. bfd_vma off;
  1621. off = elf_local_got_offsets (input_bfd)[symndx];
  1622. if (off & 1)
  1623. bfd_put_32 (output_bfd, value, sgot->contents + (off & ~ 1));
  1624. else
  1625. {
  1626. bfd_put_32 (output_bfd, value, sgot->contents + off);
  1627. if (bfd_link_pic (info))
  1628. {
  1629. asection *srelgot = htab->root.srelgot;;
  1630. Elf_Internal_Rela outrel;
  1631. BFD_ASSERT (srelgot != NULL);
  1632. outrel.r_offset = (sgot->output_section->vma
  1633. + sgot->output_offset
  1634. + off);
  1635. switch (r_type)
  1636. {
  1637. case R_MN10300_TLS_GD:
  1638. outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPOFF);
  1639. outrel.r_offset = (sgot->output_section->vma
  1640. + sgot->output_offset
  1641. + off + 4);
  1642. bfd_elf32_swap_reloca_out (output_bfd, & outrel,
  1643. (bfd_byte *) (((Elf32_External_Rela *)
  1644. srelgot->contents)
  1645. + srelgot->reloc_count));
  1646. ++ srelgot->reloc_count;
  1647. outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPMOD);
  1648. break;
  1649. case R_MN10300_TLS_GOTIE:
  1650. case R_MN10300_TLS_IE:
  1651. outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_TPOFF);
  1652. break;
  1653. default:
  1654. outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
  1655. break;
  1656. }
  1657. outrel.r_addend = value;
  1658. bfd_elf32_swap_reloca_out (output_bfd, &outrel,
  1659. (bfd_byte *) (((Elf32_External_Rela *)
  1660. srelgot->contents)
  1661. + srelgot->reloc_count));
  1662. ++ srelgot->reloc_count;
  1663. elf_local_got_offsets (input_bfd)[symndx] |= 1;
  1664. }
  1665. value = sgot->output_offset + (off & ~(bfd_vma) 1);
  1666. }
  1667. }
  1668. value += addend;
  1669. if (r_type == R_MN10300_TLS_IE)
  1670. {
  1671. value += sgot->output_section->vma;
  1672. bfd_put_32 (input_bfd, value, hit_data);
  1673. return bfd_reloc_ok;
  1674. }
  1675. else if (r_type == R_MN10300_TLS_GOTIE
  1676. || r_type == R_MN10300_TLS_GD
  1677. || r_type == R_MN10300_TLS_LD)
  1678. {
  1679. bfd_put_32 (input_bfd, value, hit_data);
  1680. return bfd_reloc_ok;
  1681. }
  1682. else if (r_type == R_MN10300_GOT32)
  1683. {
  1684. bfd_put_32 (input_bfd, value, hit_data);
  1685. return bfd_reloc_ok;
  1686. }
  1687. else if (r_type == R_MN10300_GOT24)
  1688. {
  1689. if ((long) value > 0x7fffff || (long) value < -0x800000)
  1690. return bfd_reloc_overflow;
  1691. bfd_put_8 (input_bfd, value & 0xff, hit_data);
  1692. bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
  1693. bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
  1694. return bfd_reloc_ok;
  1695. }
  1696. else if (r_type == R_MN10300_GOT16)
  1697. {
  1698. if ((long) value > 0x7fff || (long) value < -0x8000)
  1699. return bfd_reloc_overflow;
  1700. bfd_put_16 (input_bfd, value, hit_data);
  1701. return bfd_reloc_ok;
  1702. }
  1703. /* Fall through. */
  1704. default:
  1705. return bfd_reloc_notsupported;
  1706. }
  1707. }
  1708. /* Relocate an MN10300 ELF section. */
  1709. static int
  1710. mn10300_elf_relocate_section (bfd *output_bfd,
  1711. struct bfd_link_info *info,
  1712. bfd *input_bfd,
  1713. asection *input_section,
  1714. bfd_byte *contents,
  1715. Elf_Internal_Rela *relocs,
  1716. Elf_Internal_Sym *local_syms,
  1717. asection **local_sections)
  1718. {
  1719. Elf_Internal_Shdr *symtab_hdr;
  1720. struct elf_link_hash_entry **sym_hashes;
  1721. Elf_Internal_Rela *rel, *relend;
  1722. Elf_Internal_Rela * trel;
  1723. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1724. sym_hashes = elf_sym_hashes (input_bfd);
  1725. rel = relocs;
  1726. relend = relocs + input_section->reloc_count;
  1727. for (; rel < relend; rel++)
  1728. {
  1729. int r_type;
  1730. reloc_howto_type *howto;
  1731. unsigned long r_symndx;
  1732. Elf_Internal_Sym *sym;
  1733. asection *sec;
  1734. struct elf32_mn10300_link_hash_entry *h;
  1735. bfd_vma relocation;
  1736. bfd_reloc_status_type r;
  1737. int tls_r_type;
  1738. bool unresolved_reloc = false;
  1739. bool warned, ignored;
  1740. struct elf_link_hash_entry * hh;
  1741. relocation = 0;
  1742. r_symndx = ELF32_R_SYM (rel->r_info);
  1743. r_type = ELF32_R_TYPE (rel->r_info);
  1744. howto = elf_mn10300_howto_table + r_type;
  1745. /* Just skip the vtable gc relocs. */
  1746. if (r_type == R_MN10300_GNU_VTINHERIT
  1747. || r_type == R_MN10300_GNU_VTENTRY)
  1748. continue;
  1749. h = NULL;
  1750. sym = NULL;
  1751. sec = NULL;
  1752. if (r_symndx < symtab_hdr->sh_info)
  1753. hh = NULL;
  1754. else
  1755. {
  1756. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1757. r_symndx, symtab_hdr, sym_hashes,
  1758. hh, sec, relocation,
  1759. unresolved_reloc, warned, ignored);
  1760. }
  1761. h = elf_mn10300_hash_entry (hh);
  1762. tls_r_type = elf_mn10300_tls_transition (info, r_type, hh, input_section, 0);
  1763. if (tls_r_type != r_type)
  1764. {
  1765. bool had_plt;
  1766. had_plt = mn10300_do_tls_transition (input_bfd, r_type, tls_r_type,
  1767. contents, rel->r_offset);
  1768. r_type = tls_r_type;
  1769. howto = elf_mn10300_howto_table + r_type;
  1770. if (had_plt)
  1771. for (trel = rel+1; trel < relend; trel++)
  1772. if ((ELF32_R_TYPE (trel->r_info) == R_MN10300_PLT32
  1773. || ELF32_R_TYPE (trel->r_info) == R_MN10300_PCREL32)
  1774. && rel->r_offset + had_plt == trel->r_offset)
  1775. trel->r_info = ELF32_R_INFO (0, R_MN10300_NONE);
  1776. }
  1777. if (r_symndx < symtab_hdr->sh_info)
  1778. {
  1779. sym = local_syms + r_symndx;
  1780. sec = local_sections[r_symndx];
  1781. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1782. }
  1783. else
  1784. {
  1785. if ((h->root.root.type == bfd_link_hash_defined
  1786. || h->root.root.type == bfd_link_hash_defweak)
  1787. && ( r_type == R_MN10300_GOTPC32
  1788. || r_type == R_MN10300_GOTPC16
  1789. || (( r_type == R_MN10300_PLT32
  1790. || r_type == R_MN10300_PLT16)
  1791. && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
  1792. && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
  1793. && h->root.plt.offset != (bfd_vma) -1)
  1794. || (( r_type == R_MN10300_GOT32
  1795. || r_type == R_MN10300_GOT24
  1796. || r_type == R_MN10300_TLS_GD
  1797. || r_type == R_MN10300_TLS_LD
  1798. || r_type == R_MN10300_TLS_GOTIE
  1799. || r_type == R_MN10300_TLS_IE
  1800. || r_type == R_MN10300_GOT16)
  1801. && elf_hash_table (info)->dynamic_sections_created
  1802. && !SYMBOL_REFERENCES_LOCAL (info, hh))
  1803. || (r_type == R_MN10300_32
  1804. && !SYMBOL_REFERENCES_LOCAL (info, hh)
  1805. /* _32 relocs in executables force _COPY relocs,
  1806. such that the address of the symbol ends up
  1807. being local. */
  1808. && (((input_section->flags & SEC_ALLOC) != 0
  1809. && !bfd_link_executable (info))
  1810. /* DWARF will emit R_MN10300_32 relocations
  1811. in its sections against symbols defined
  1812. externally in shared libraries. We can't
  1813. do anything with them here. */
  1814. || ((input_section->flags & SEC_DEBUGGING) != 0
  1815. && h->root.def_dynamic)))))
  1816. /* In these cases, we don't need the relocation
  1817. value. We check specially because in some
  1818. obscure cases sec->output_section will be NULL. */
  1819. relocation = 0;
  1820. else if (!bfd_link_relocatable (info) && unresolved_reloc
  1821. && _bfd_elf_section_offset (output_bfd, info, input_section,
  1822. rel->r_offset) != (bfd_vma) -1)
  1823. _bfd_error_handler
  1824. /* xgettext:c-format */
  1825. (_("%pB(%pA+%#" PRIx64 "): "
  1826. "unresolvable %s relocation against symbol `%s'"),
  1827. input_bfd,
  1828. input_section,
  1829. (uint64_t) rel->r_offset,
  1830. howto->name,
  1831. h->root.root.root.string);
  1832. }
  1833. if (sec != NULL && discarded_section (sec))
  1834. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1835. rel, 1, relend, howto, 0, contents);
  1836. if (bfd_link_relocatable (info))
  1837. continue;
  1838. r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
  1839. input_section,
  1840. contents, rel->r_offset,
  1841. relocation, rel->r_addend,
  1842. (struct elf_link_hash_entry *) h,
  1843. r_symndx,
  1844. info, sec, h == NULL);
  1845. if (r != bfd_reloc_ok)
  1846. {
  1847. const char *name;
  1848. const char *msg = NULL;
  1849. if (h != NULL)
  1850. name = h->root.root.root.string;
  1851. else
  1852. {
  1853. name = (bfd_elf_string_from_elf_section
  1854. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  1855. if (name == NULL || *name == '\0')
  1856. name = bfd_section_name (sec);
  1857. }
  1858. switch (r)
  1859. {
  1860. case bfd_reloc_overflow:
  1861. (*info->callbacks->reloc_overflow)
  1862. (info, (h ? &h->root.root : NULL), name, howto->name,
  1863. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  1864. break;
  1865. case bfd_reloc_undefined:
  1866. (*info->callbacks->undefined_symbol)
  1867. (info, name, input_bfd, input_section, rel->r_offset, true);
  1868. break;
  1869. case bfd_reloc_outofrange:
  1870. msg = _("internal error: out of range error");
  1871. goto common_error;
  1872. case bfd_reloc_notsupported:
  1873. msg = _("internal error: unsupported relocation error");
  1874. goto common_error;
  1875. case bfd_reloc_dangerous:
  1876. if (r_type == R_MN10300_PCREL32)
  1877. msg = _("error: inappropriate relocation type for shared"
  1878. " library (did you forget -fpic?)");
  1879. else if (r_type == R_MN10300_GOT32)
  1880. /* xgettext:c-format */
  1881. msg = _("%pB: taking the address of protected function"
  1882. " '%s' cannot be done when making a shared library");
  1883. else
  1884. msg = _("internal error: suspicious relocation type used"
  1885. " in shared library");
  1886. goto common_error;
  1887. default:
  1888. msg = _("internal error: unknown error");
  1889. /* Fall through. */
  1890. common_error:
  1891. _bfd_error_handler (msg, input_bfd, name);
  1892. bfd_set_error (bfd_error_bad_value);
  1893. return false;
  1894. }
  1895. }
  1896. }
  1897. return true;
  1898. }
  1899. /* Finish initializing one hash table entry. */
  1900. static bool
  1901. elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
  1902. void * in_args)
  1903. {
  1904. struct elf32_mn10300_link_hash_entry *entry;
  1905. struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
  1906. unsigned int byte_count = 0;
  1907. entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
  1908. /* If we already know we want to convert "call" to "calls" for calls
  1909. to this symbol, then return now. */
  1910. if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
  1911. return true;
  1912. /* If there are no named calls to this symbol, or there's nothing we
  1913. can move from the function itself into the "call" instruction,
  1914. then note that all "call" instructions should be converted into
  1915. "calls" instructions and return. If a symbol is available for
  1916. dynamic symbol resolution (overridable or overriding), avoid
  1917. custom calling conventions. */
  1918. if (entry->direct_calls == 0
  1919. || (entry->stack_size == 0 && entry->movm_args == 0)
  1920. || (elf_hash_table (link_info)->dynamic_sections_created
  1921. && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
  1922. && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
  1923. {
  1924. /* Make a note that we should convert "call" instructions to "calls"
  1925. instructions for calls to this symbol. */
  1926. entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
  1927. return true;
  1928. }
  1929. /* We may be able to move some instructions from the function itself into
  1930. the "call" instruction. Count how many bytes we might be able to
  1931. eliminate in the function itself. */
  1932. /* A movm instruction is two bytes. */
  1933. if (entry->movm_args)
  1934. byte_count += 2;
  1935. /* Count the insn to allocate stack space too. */
  1936. if (entry->stack_size > 0)
  1937. {
  1938. if (entry->stack_size <= 128)
  1939. byte_count += 3;
  1940. else
  1941. byte_count += 4;
  1942. }
  1943. /* If using "call" will result in larger code, then turn all
  1944. the associated "call" instructions into "calls" instructions. */
  1945. if (byte_count < entry->direct_calls)
  1946. entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
  1947. /* This routine never fails. */
  1948. return true;
  1949. }
  1950. /* Used to count hash table entries. */
  1951. static bool
  1952. elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
  1953. void * in_args)
  1954. {
  1955. int *count = (int *) in_args;
  1956. (*count) ++;
  1957. return true;
  1958. }
  1959. /* Used to enumerate hash table entries into a linear array. */
  1960. static bool
  1961. elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
  1962. void * in_args)
  1963. {
  1964. struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
  1965. **ptr = gen_entry;
  1966. (*ptr) ++;
  1967. return true;
  1968. }
  1969. /* Used to sort the array created by the above. */
  1970. static int
  1971. sort_by_value (const void *va, const void *vb)
  1972. {
  1973. struct elf32_mn10300_link_hash_entry *a
  1974. = *(struct elf32_mn10300_link_hash_entry **) va;
  1975. struct elf32_mn10300_link_hash_entry *b
  1976. = *(struct elf32_mn10300_link_hash_entry **) vb;
  1977. return a->value - b->value;
  1978. }
  1979. /* Compute the stack size and movm arguments for the function
  1980. referred to by HASH at address ADDR in section with
  1981. contents CONTENTS, store the information in the hash table. */
  1982. static void
  1983. compute_function_info (bfd *abfd,
  1984. struct elf32_mn10300_link_hash_entry *hash,
  1985. bfd_vma addr,
  1986. unsigned char *contents)
  1987. {
  1988. unsigned char byte1, byte2;
  1989. /* We only care about a very small subset of the possible prologue
  1990. sequences here. Basically we look for:
  1991. movm [d2,d3,a2,a3],sp (optional)
  1992. add <size>,sp (optional, and only for sizes which fit in an unsigned
  1993. 8 bit number)
  1994. If we find anything else, we quit. */
  1995. /* Look for movm [regs],sp. */
  1996. byte1 = bfd_get_8 (abfd, contents + addr);
  1997. byte2 = bfd_get_8 (abfd, contents + addr + 1);
  1998. if (byte1 == 0xcf)
  1999. {
  2000. hash->movm_args = byte2;
  2001. addr += 2;
  2002. byte1 = bfd_get_8 (abfd, contents + addr);
  2003. byte2 = bfd_get_8 (abfd, contents + addr + 1);
  2004. }
  2005. /* Now figure out how much stack space will be allocated by the movm
  2006. instruction. We need this kept separate from the function's normal
  2007. stack space. */
  2008. if (hash->movm_args)
  2009. {
  2010. /* Space for d2. */
  2011. if (hash->movm_args & 0x80)
  2012. hash->movm_stack_size += 4;
  2013. /* Space for d3. */
  2014. if (hash->movm_args & 0x40)
  2015. hash->movm_stack_size += 4;
  2016. /* Space for a2. */
  2017. if (hash->movm_args & 0x20)
  2018. hash->movm_stack_size += 4;
  2019. /* Space for a3. */
  2020. if (hash->movm_args & 0x10)
  2021. hash->movm_stack_size += 4;
  2022. /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
  2023. if (hash->movm_args & 0x08)
  2024. hash->movm_stack_size += 8 * 4;
  2025. if (bfd_get_mach (abfd) == bfd_mach_am33
  2026. || bfd_get_mach (abfd) == bfd_mach_am33_2)
  2027. {
  2028. /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
  2029. if (hash->movm_args & 0x1)
  2030. hash->movm_stack_size += 6 * 4;
  2031. /* exreg1 space. e4, e5, e6, e7 */
  2032. if (hash->movm_args & 0x2)
  2033. hash->movm_stack_size += 4 * 4;
  2034. /* exreg0 space. e2, e3 */
  2035. if (hash->movm_args & 0x4)
  2036. hash->movm_stack_size += 2 * 4;
  2037. }
  2038. }
  2039. /* Now look for the two stack adjustment variants. */
  2040. if (byte1 == 0xf8 && byte2 == 0xfe)
  2041. {
  2042. int temp = bfd_get_8 (abfd, contents + addr + 2);
  2043. temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
  2044. hash->stack_size = -temp;
  2045. }
  2046. else if (byte1 == 0xfa && byte2 == 0xfe)
  2047. {
  2048. int temp = bfd_get_16 (abfd, contents + addr + 2);
  2049. temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
  2050. temp = -temp;
  2051. if (temp < 255)
  2052. hash->stack_size = temp;
  2053. }
  2054. /* If the total stack to be allocated by the call instruction is more
  2055. than 255 bytes, then we can't remove the stack adjustment by using
  2056. "call" (we might still be able to remove the "movm" instruction. */
  2057. if (hash->stack_size + hash->movm_stack_size > 255)
  2058. hash->stack_size = 0;
  2059. }
  2060. /* Delete some bytes from a section while relaxing. */
  2061. static bool
  2062. mn10300_elf_relax_delete_bytes (bfd *abfd,
  2063. asection *sec,
  2064. bfd_vma addr,
  2065. int count)
  2066. {
  2067. Elf_Internal_Shdr *symtab_hdr;
  2068. unsigned int sec_shndx;
  2069. bfd_byte *contents;
  2070. Elf_Internal_Rela *irel, *irelend;
  2071. Elf_Internal_Rela *irelalign;
  2072. bfd_vma toaddr;
  2073. Elf_Internal_Sym *isym, *isymend;
  2074. struct elf_link_hash_entry **sym_hashes;
  2075. struct elf_link_hash_entry **end_hashes;
  2076. unsigned int symcount;
  2077. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  2078. contents = elf_section_data (sec)->this_hdr.contents;
  2079. irelalign = NULL;
  2080. toaddr = sec->size;
  2081. irel = elf_section_data (sec)->relocs;
  2082. irelend = irel + sec->reloc_count;
  2083. if (sec->reloc_count > 0)
  2084. {
  2085. /* If there is an align reloc at the end of the section ignore it.
  2086. GAS creates these relocs for reasons of its own, and they just
  2087. serve to keep the section artifically inflated. */
  2088. if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
  2089. --irelend;
  2090. /* The deletion must stop at the next ALIGN reloc for an alignment
  2091. power larger than, or not a multiple of, the number of bytes we
  2092. are deleting. */
  2093. for (; irel < irelend; irel++)
  2094. {
  2095. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
  2096. && irel->r_offset > addr
  2097. && irel->r_offset < toaddr)
  2098. {
  2099. int alignment = 1 << irel->r_addend;
  2100. if (count < alignment
  2101. || alignment % count != 0)
  2102. {
  2103. irelalign = irel;
  2104. toaddr = irel->r_offset;
  2105. break;
  2106. }
  2107. }
  2108. }
  2109. }
  2110. /* Actually delete the bytes. */
  2111. memmove (contents + addr, contents + addr + count,
  2112. (size_t) (toaddr - addr - count));
  2113. /* Adjust the section's size if we are shrinking it, or else
  2114. pad the bytes between the end of the shrunken region and
  2115. the start of the next region with NOP codes. */
  2116. if (irelalign == NULL)
  2117. {
  2118. sec->size -= count;
  2119. /* Include symbols at the end of the section, but
  2120. not at the end of a sub-region of the section. */
  2121. toaddr ++;
  2122. }
  2123. else
  2124. {
  2125. int i;
  2126. #define NOP_OPCODE 0xcb
  2127. for (i = 0; i < count; i ++)
  2128. bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
  2129. }
  2130. /* Adjust all the relocs. */
  2131. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  2132. {
  2133. /* Get the new reloc address. */
  2134. if ((irel->r_offset > addr
  2135. && irel->r_offset < toaddr)
  2136. || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
  2137. && irel->r_offset == toaddr))
  2138. irel->r_offset -= count;
  2139. }
  2140. /* Adjust the local symbols in the section, reducing their value
  2141. by the number of bytes deleted. Note - symbols within the deleted
  2142. region are moved to the address of the start of the region, which
  2143. actually means that they will address the byte beyond the end of
  2144. the region once the deletion has been completed. */
  2145. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2146. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  2147. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  2148. {
  2149. if (isym->st_shndx == sec_shndx
  2150. && isym->st_value > addr
  2151. && isym->st_value < toaddr)
  2152. {
  2153. if (isym->st_value < addr + count)
  2154. isym->st_value = addr;
  2155. else
  2156. isym->st_value -= count;
  2157. }
  2158. /* Adjust the function symbol's size as well. */
  2159. else if (isym->st_shndx == sec_shndx
  2160. && ELF_ST_TYPE (isym->st_info) == STT_FUNC
  2161. && isym->st_value + isym->st_size > addr
  2162. && isym->st_value + isym->st_size < toaddr)
  2163. isym->st_size -= count;
  2164. }
  2165. /* Now adjust the global symbols defined in this section. */
  2166. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  2167. - symtab_hdr->sh_info);
  2168. sym_hashes = elf_sym_hashes (abfd);
  2169. end_hashes = sym_hashes + symcount;
  2170. for (; sym_hashes < end_hashes; sym_hashes++)
  2171. {
  2172. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  2173. if ((sym_hash->root.type == bfd_link_hash_defined
  2174. || sym_hash->root.type == bfd_link_hash_defweak)
  2175. && sym_hash->root.u.def.section == sec
  2176. && sym_hash->root.u.def.value > addr
  2177. && sym_hash->root.u.def.value < toaddr)
  2178. {
  2179. if (sym_hash->root.u.def.value < addr + count)
  2180. sym_hash->root.u.def.value = addr;
  2181. else
  2182. sym_hash->root.u.def.value -= count;
  2183. }
  2184. /* Adjust the function symbol's size as well. */
  2185. else if (sym_hash->root.type == bfd_link_hash_defined
  2186. && sym_hash->root.u.def.section == sec
  2187. && sym_hash->type == STT_FUNC
  2188. && sym_hash->root.u.def.value + sym_hash->size > addr
  2189. && sym_hash->root.u.def.value + sym_hash->size < toaddr)
  2190. sym_hash->size -= count;
  2191. }
  2192. /* See if we can move the ALIGN reloc forward.
  2193. We have adjusted r_offset for it already. */
  2194. if (irelalign != NULL)
  2195. {
  2196. bfd_vma alignto, alignaddr;
  2197. if ((int) irelalign->r_addend > 0)
  2198. {
  2199. /* This is the old address. */
  2200. alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
  2201. /* This is where the align points to now. */
  2202. alignaddr = BFD_ALIGN (irelalign->r_offset,
  2203. 1 << irelalign->r_addend);
  2204. if (alignaddr < alignto)
  2205. /* Tail recursion. */
  2206. return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
  2207. (int) (alignto - alignaddr));
  2208. }
  2209. }
  2210. return true;
  2211. }
  2212. /* Return TRUE if a symbol exists at the given address, else return
  2213. FALSE. */
  2214. static bool
  2215. mn10300_elf_symbol_address_p (bfd *abfd,
  2216. asection *sec,
  2217. Elf_Internal_Sym *isym,
  2218. bfd_vma addr)
  2219. {
  2220. Elf_Internal_Shdr *symtab_hdr;
  2221. unsigned int sec_shndx;
  2222. Elf_Internal_Sym *isymend;
  2223. struct elf_link_hash_entry **sym_hashes;
  2224. struct elf_link_hash_entry **end_hashes;
  2225. unsigned int symcount;
  2226. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  2227. /* Examine all the symbols. */
  2228. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2229. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  2230. if (isym->st_shndx == sec_shndx
  2231. && isym->st_value == addr)
  2232. return true;
  2233. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  2234. - symtab_hdr->sh_info);
  2235. sym_hashes = elf_sym_hashes (abfd);
  2236. end_hashes = sym_hashes + symcount;
  2237. for (; sym_hashes < end_hashes; sym_hashes++)
  2238. {
  2239. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  2240. if ((sym_hash->root.type == bfd_link_hash_defined
  2241. || sym_hash->root.type == bfd_link_hash_defweak)
  2242. && sym_hash->root.u.def.section == sec
  2243. && sym_hash->root.u.def.value == addr)
  2244. return true;
  2245. }
  2246. return false;
  2247. }
  2248. /* This function handles relaxing for the mn10300.
  2249. There are quite a few relaxing opportunities available on the mn10300:
  2250. * calls:32 -> calls:16 2 bytes
  2251. * call:32 -> call:16 2 bytes
  2252. * call:32 -> calls:32 1 byte
  2253. * call:16 -> calls:16 1 byte
  2254. * These are done anytime using "calls" would result
  2255. in smaller code, or when necessary to preserve the
  2256. meaning of the program.
  2257. * call:32 varies
  2258. * call:16
  2259. * In some circumstances we can move instructions
  2260. from a function prologue into a "call" instruction.
  2261. This is only done if the resulting code is no larger
  2262. than the original code.
  2263. * jmp:32 -> jmp:16 2 bytes
  2264. * jmp:16 -> bra:8 1 byte
  2265. * If the previous instruction is a conditional branch
  2266. around the jump/bra, we may be able to reverse its condition
  2267. and change its target to the jump's target. The jump/bra
  2268. can then be deleted. 2 bytes
  2269. * mov abs32 -> mov abs16 1 or 2 bytes
  2270. * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
  2271. - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
  2272. * Most instructions which accept d32 can relax to d16 1 or 2 bytes
  2273. - Most instructions which accept d16 can relax to d8 1 or 2 bytes
  2274. We don't handle imm16->imm8 or d16->d8 as they're very rare
  2275. and somewhat more difficult to support. */
  2276. static bool
  2277. mn10300_elf_relax_section (bfd *abfd,
  2278. asection *sec,
  2279. struct bfd_link_info *link_info,
  2280. bool *again)
  2281. {
  2282. Elf_Internal_Shdr *symtab_hdr;
  2283. Elf_Internal_Rela *internal_relocs = NULL;
  2284. Elf_Internal_Rela *irel, *irelend;
  2285. bfd_byte *contents = NULL;
  2286. Elf_Internal_Sym *isymbuf = NULL;
  2287. struct elf32_mn10300_link_hash_table *hash_table;
  2288. asection *section = sec;
  2289. bfd_vma align_gap_adjustment;
  2290. if (bfd_link_relocatable (link_info))
  2291. (*link_info->callbacks->einfo)
  2292. (_("%P%F: --relax and -r may not be used together\n"));
  2293. /* Assume nothing changes. */
  2294. *again = false;
  2295. /* We need a pointer to the mn10300 specific hash table. */
  2296. hash_table = elf32_mn10300_hash_table (link_info);
  2297. if (hash_table == NULL)
  2298. return false;
  2299. /* Initialize fields in each hash table entry the first time through. */
  2300. if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
  2301. {
  2302. bfd *input_bfd;
  2303. /* Iterate over all the input bfds. */
  2304. for (input_bfd = link_info->input_bfds;
  2305. input_bfd != NULL;
  2306. input_bfd = input_bfd->link.next)
  2307. {
  2308. /* We're going to need all the symbols for each bfd. */
  2309. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2310. if (symtab_hdr->sh_info != 0)
  2311. {
  2312. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2313. if (isymbuf == NULL)
  2314. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  2315. symtab_hdr->sh_info, 0,
  2316. NULL, NULL, NULL);
  2317. if (isymbuf == NULL)
  2318. goto error_return;
  2319. }
  2320. /* Iterate over each section in this bfd. */
  2321. for (section = input_bfd->sections;
  2322. section != NULL;
  2323. section = section->next)
  2324. {
  2325. struct elf32_mn10300_link_hash_entry *hash;
  2326. asection *sym_sec = NULL;
  2327. const char *sym_name;
  2328. char *new_name;
  2329. /* If there's nothing to do in this section, skip it. */
  2330. if (! ((section->flags & SEC_RELOC) != 0
  2331. && section->reloc_count != 0))
  2332. continue;
  2333. if ((section->flags & SEC_ALLOC) == 0)
  2334. continue;
  2335. /* Get cached copy of section contents if it exists. */
  2336. if (elf_section_data (section)->this_hdr.contents != NULL)
  2337. contents = elf_section_data (section)->this_hdr.contents;
  2338. else if (section->size != 0)
  2339. {
  2340. /* Go get them off disk. */
  2341. if (!bfd_malloc_and_get_section (input_bfd, section,
  2342. &contents))
  2343. goto error_return;
  2344. }
  2345. else
  2346. contents = NULL;
  2347. /* If there aren't any relocs, then there's nothing to do. */
  2348. if ((section->flags & SEC_RELOC) != 0
  2349. && section->reloc_count != 0)
  2350. {
  2351. /* Get a copy of the native relocations. */
  2352. internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
  2353. NULL, NULL,
  2354. link_info->keep_memory);
  2355. if (internal_relocs == NULL)
  2356. goto error_return;
  2357. /* Now examine each relocation. */
  2358. irel = internal_relocs;
  2359. irelend = irel + section->reloc_count;
  2360. for (; irel < irelend; irel++)
  2361. {
  2362. long r_type;
  2363. unsigned long r_index;
  2364. unsigned char code;
  2365. r_type = ELF32_R_TYPE (irel->r_info);
  2366. r_index = ELF32_R_SYM (irel->r_info);
  2367. if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
  2368. goto error_return;
  2369. /* We need the name and hash table entry of the target
  2370. symbol! */
  2371. hash = NULL;
  2372. sym_sec = NULL;
  2373. if (r_index < symtab_hdr->sh_info)
  2374. {
  2375. /* A local symbol. */
  2376. Elf_Internal_Sym *isym;
  2377. struct elf_link_hash_table *elftab;
  2378. size_t amt;
  2379. isym = isymbuf + r_index;
  2380. if (isym->st_shndx == SHN_UNDEF)
  2381. sym_sec = bfd_und_section_ptr;
  2382. else if (isym->st_shndx == SHN_ABS)
  2383. sym_sec = bfd_abs_section_ptr;
  2384. else if (isym->st_shndx == SHN_COMMON)
  2385. sym_sec = bfd_com_section_ptr;
  2386. else
  2387. sym_sec
  2388. = bfd_section_from_elf_index (input_bfd,
  2389. isym->st_shndx);
  2390. sym_name
  2391. = bfd_elf_string_from_elf_section (input_bfd,
  2392. (symtab_hdr
  2393. ->sh_link),
  2394. isym->st_name);
  2395. /* If it isn't a function, then we don't care
  2396. about it. */
  2397. if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
  2398. continue;
  2399. /* Tack on an ID so we can uniquely identify this
  2400. local symbol in the global hash table. */
  2401. amt = strlen (sym_name) + 10;
  2402. new_name = bfd_malloc (amt);
  2403. if (new_name == NULL)
  2404. goto error_return;
  2405. sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
  2406. sym_name = new_name;
  2407. elftab = &hash_table->static_hash_table->root;
  2408. hash = ((struct elf32_mn10300_link_hash_entry *)
  2409. elf_link_hash_lookup (elftab, sym_name,
  2410. true, true, false));
  2411. free (new_name);
  2412. }
  2413. else
  2414. {
  2415. r_index -= symtab_hdr->sh_info;
  2416. hash = (struct elf32_mn10300_link_hash_entry *)
  2417. elf_sym_hashes (input_bfd)[r_index];
  2418. }
  2419. sym_name = hash->root.root.root.string;
  2420. if ((section->flags & SEC_CODE) != 0)
  2421. {
  2422. /* If this is not a "call" instruction, then we
  2423. should convert "call" instructions to "calls"
  2424. instructions. */
  2425. code = bfd_get_8 (input_bfd,
  2426. contents + irel->r_offset - 1);
  2427. if (code != 0xdd && code != 0xcd)
  2428. hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
  2429. }
  2430. /* If this is a jump/call, then bump the
  2431. direct_calls counter. Else force "call" to
  2432. "calls" conversions. */
  2433. if (r_type == R_MN10300_PCREL32
  2434. || r_type == R_MN10300_PLT32
  2435. || r_type == R_MN10300_PLT16
  2436. || r_type == R_MN10300_PCREL16)
  2437. hash->direct_calls++;
  2438. else
  2439. hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
  2440. }
  2441. }
  2442. /* Now look at the actual contents to get the stack size,
  2443. and a list of what registers were saved in the prologue
  2444. (ie movm_args). */
  2445. if ((section->flags & SEC_CODE) != 0)
  2446. {
  2447. Elf_Internal_Sym *isym, *isymend;
  2448. unsigned int sec_shndx;
  2449. struct elf_link_hash_entry **hashes;
  2450. struct elf_link_hash_entry **end_hashes;
  2451. unsigned int symcount;
  2452. sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
  2453. section);
  2454. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  2455. - symtab_hdr->sh_info);
  2456. hashes = elf_sym_hashes (input_bfd);
  2457. end_hashes = hashes + symcount;
  2458. /* Look at each function defined in this section and
  2459. update info for that function. */
  2460. isymend = isymbuf + symtab_hdr->sh_info;
  2461. for (isym = isymbuf; isym < isymend; isym++)
  2462. {
  2463. if (isym->st_shndx == sec_shndx
  2464. && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
  2465. {
  2466. struct elf_link_hash_table *elftab;
  2467. size_t amt;
  2468. struct elf_link_hash_entry **lhashes = hashes;
  2469. /* Skip a local symbol if it aliases a
  2470. global one. */
  2471. for (; lhashes < end_hashes; lhashes++)
  2472. {
  2473. hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
  2474. if ((hash->root.root.type == bfd_link_hash_defined
  2475. || hash->root.root.type == bfd_link_hash_defweak)
  2476. && hash->root.root.u.def.section == section
  2477. && hash->root.type == STT_FUNC
  2478. && hash->root.root.u.def.value == isym->st_value)
  2479. break;
  2480. }
  2481. if (lhashes != end_hashes)
  2482. continue;
  2483. if (isym->st_shndx == SHN_UNDEF)
  2484. sym_sec = bfd_und_section_ptr;
  2485. else if (isym->st_shndx == SHN_ABS)
  2486. sym_sec = bfd_abs_section_ptr;
  2487. else if (isym->st_shndx == SHN_COMMON)
  2488. sym_sec = bfd_com_section_ptr;
  2489. else
  2490. sym_sec
  2491. = bfd_section_from_elf_index (input_bfd,
  2492. isym->st_shndx);
  2493. sym_name = (bfd_elf_string_from_elf_section
  2494. (input_bfd, symtab_hdr->sh_link,
  2495. isym->st_name));
  2496. /* Tack on an ID so we can uniquely identify this
  2497. local symbol in the global hash table. */
  2498. amt = strlen (sym_name) + 10;
  2499. new_name = bfd_malloc (amt);
  2500. if (new_name == NULL)
  2501. goto error_return;
  2502. sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
  2503. sym_name = new_name;
  2504. elftab = &hash_table->static_hash_table->root;
  2505. hash = ((struct elf32_mn10300_link_hash_entry *)
  2506. elf_link_hash_lookup (elftab, sym_name,
  2507. true, true, false));
  2508. free (new_name);
  2509. compute_function_info (input_bfd, hash,
  2510. isym->st_value, contents);
  2511. hash->value = isym->st_value;
  2512. }
  2513. }
  2514. for (; hashes < end_hashes; hashes++)
  2515. {
  2516. hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
  2517. if ((hash->root.root.type == bfd_link_hash_defined
  2518. || hash->root.root.type == bfd_link_hash_defweak)
  2519. && hash->root.root.u.def.section == section
  2520. && hash->root.type == STT_FUNC)
  2521. compute_function_info (input_bfd, hash,
  2522. (hash)->root.root.u.def.value,
  2523. contents);
  2524. }
  2525. }
  2526. /* Cache or free any memory we allocated for the relocs. */
  2527. if (elf_section_data (section)->relocs != internal_relocs)
  2528. free (internal_relocs);
  2529. internal_relocs = NULL;
  2530. /* Cache or free any memory we allocated for the contents. */
  2531. if (contents != NULL
  2532. && elf_section_data (section)->this_hdr.contents != contents)
  2533. {
  2534. if (! link_info->keep_memory)
  2535. free (contents);
  2536. else
  2537. {
  2538. /* Cache the section contents for elf_link_input_bfd. */
  2539. elf_section_data (section)->this_hdr.contents = contents;
  2540. }
  2541. }
  2542. contents = NULL;
  2543. }
  2544. /* Cache or free any memory we allocated for the symbols. */
  2545. if (isymbuf != NULL
  2546. && symtab_hdr->contents != (unsigned char *) isymbuf)
  2547. {
  2548. if (! link_info->keep_memory)
  2549. free (isymbuf);
  2550. else
  2551. {
  2552. /* Cache the symbols for elf_link_input_bfd. */
  2553. symtab_hdr->contents = (unsigned char *) isymbuf;
  2554. }
  2555. }
  2556. isymbuf = NULL;
  2557. }
  2558. /* Now iterate on each symbol in the hash table and perform
  2559. the final initialization steps on each. */
  2560. elf32_mn10300_link_hash_traverse (hash_table,
  2561. elf32_mn10300_finish_hash_table_entry,
  2562. link_info);
  2563. elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
  2564. elf32_mn10300_finish_hash_table_entry,
  2565. link_info);
  2566. {
  2567. /* This section of code collects all our local symbols, sorts
  2568. them by value, and looks for multiple symbols referring to
  2569. the same address. For those symbols, the flags are merged.
  2570. At this point, the only flag that can be set is
  2571. MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
  2572. together. */
  2573. int static_count = 0, i;
  2574. struct elf32_mn10300_link_hash_entry **entries;
  2575. struct elf32_mn10300_link_hash_entry **ptr;
  2576. elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
  2577. elf32_mn10300_count_hash_table_entries,
  2578. &static_count);
  2579. entries = bfd_malloc (static_count * sizeof (* ptr));
  2580. ptr = entries;
  2581. elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
  2582. elf32_mn10300_list_hash_table_entries,
  2583. & ptr);
  2584. qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
  2585. for (i = 0; i < static_count - 1; i++)
  2586. if (entries[i]->value && entries[i]->value == entries[i+1]->value)
  2587. {
  2588. int v = entries[i]->flags;
  2589. int j;
  2590. for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
  2591. v |= entries[j]->flags;
  2592. for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
  2593. entries[j]->flags = v;
  2594. i = j - 1;
  2595. }
  2596. }
  2597. /* All entries in the hash table are fully initialized. */
  2598. hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
  2599. /* Now that everything has been initialized, go through each
  2600. code section and delete any prologue insns which will be
  2601. redundant because their operations will be performed by
  2602. a "call" instruction. */
  2603. for (input_bfd = link_info->input_bfds;
  2604. input_bfd != NULL;
  2605. input_bfd = input_bfd->link.next)
  2606. {
  2607. /* We're going to need all the local symbols for each bfd. */
  2608. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2609. if (symtab_hdr->sh_info != 0)
  2610. {
  2611. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2612. if (isymbuf == NULL)
  2613. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  2614. symtab_hdr->sh_info, 0,
  2615. NULL, NULL, NULL);
  2616. if (isymbuf == NULL)
  2617. goto error_return;
  2618. }
  2619. /* Walk over each section in this bfd. */
  2620. for (section = input_bfd->sections;
  2621. section != NULL;
  2622. section = section->next)
  2623. {
  2624. unsigned int sec_shndx;
  2625. Elf_Internal_Sym *isym, *isymend;
  2626. struct elf_link_hash_entry **hashes;
  2627. struct elf_link_hash_entry **end_hashes;
  2628. unsigned int symcount;
  2629. /* Skip non-code sections and empty sections. */
  2630. if ((section->flags & SEC_CODE) == 0 || section->size == 0)
  2631. continue;
  2632. if (section->reloc_count != 0)
  2633. {
  2634. /* Get a copy of the native relocations. */
  2635. internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
  2636. NULL, NULL,
  2637. link_info->keep_memory);
  2638. if (internal_relocs == NULL)
  2639. goto error_return;
  2640. }
  2641. /* Get cached copy of section contents if it exists. */
  2642. if (elf_section_data (section)->this_hdr.contents != NULL)
  2643. contents = elf_section_data (section)->this_hdr.contents;
  2644. else
  2645. {
  2646. /* Go get them off disk. */
  2647. if (!bfd_malloc_and_get_section (input_bfd, section,
  2648. &contents))
  2649. goto error_return;
  2650. }
  2651. sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
  2652. section);
  2653. /* Now look for any function in this section which needs
  2654. insns deleted from its prologue. */
  2655. isymend = isymbuf + symtab_hdr->sh_info;
  2656. for (isym = isymbuf; isym < isymend; isym++)
  2657. {
  2658. struct elf32_mn10300_link_hash_entry *sym_hash;
  2659. asection *sym_sec = NULL;
  2660. const char *sym_name;
  2661. char *new_name;
  2662. struct elf_link_hash_table *elftab;
  2663. size_t amt;
  2664. if (isym->st_shndx != sec_shndx)
  2665. continue;
  2666. if (isym->st_shndx == SHN_UNDEF)
  2667. sym_sec = bfd_und_section_ptr;
  2668. else if (isym->st_shndx == SHN_ABS)
  2669. sym_sec = bfd_abs_section_ptr;
  2670. else if (isym->st_shndx == SHN_COMMON)
  2671. sym_sec = bfd_com_section_ptr;
  2672. else
  2673. sym_sec
  2674. = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  2675. sym_name
  2676. = bfd_elf_string_from_elf_section (input_bfd,
  2677. symtab_hdr->sh_link,
  2678. isym->st_name);
  2679. /* Tack on an ID so we can uniquely identify this
  2680. local symbol in the global hash table. */
  2681. amt = strlen (sym_name) + 10;
  2682. new_name = bfd_malloc (amt);
  2683. if (new_name == NULL)
  2684. goto error_return;
  2685. sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
  2686. sym_name = new_name;
  2687. elftab = & hash_table->static_hash_table->root;
  2688. sym_hash = (struct elf32_mn10300_link_hash_entry *)
  2689. elf_link_hash_lookup (elftab, sym_name,
  2690. false, false, false);
  2691. free (new_name);
  2692. if (sym_hash == NULL)
  2693. continue;
  2694. if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
  2695. && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
  2696. {
  2697. int bytes = 0;
  2698. /* Note that we've changed things. */
  2699. elf_section_data (section)->relocs = internal_relocs;
  2700. elf_section_data (section)->this_hdr.contents = contents;
  2701. symtab_hdr->contents = (unsigned char *) isymbuf;
  2702. /* Count how many bytes we're going to delete. */
  2703. if (sym_hash->movm_args)
  2704. bytes += 2;
  2705. if (sym_hash->stack_size > 0)
  2706. {
  2707. if (sym_hash->stack_size <= 128)
  2708. bytes += 3;
  2709. else
  2710. bytes += 4;
  2711. }
  2712. /* Note that we've deleted prologue bytes for this
  2713. function. */
  2714. sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
  2715. /* Actually delete the bytes. */
  2716. if (!mn10300_elf_relax_delete_bytes (input_bfd,
  2717. section,
  2718. isym->st_value,
  2719. bytes))
  2720. goto error_return;
  2721. /* Something changed. Not strictly necessary, but
  2722. may lead to more relaxing opportunities. */
  2723. *again = true;
  2724. }
  2725. }
  2726. /* Look for any global functions in this section which
  2727. need insns deleted from their prologues. */
  2728. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  2729. - symtab_hdr->sh_info);
  2730. hashes = elf_sym_hashes (input_bfd);
  2731. end_hashes = hashes + symcount;
  2732. for (; hashes < end_hashes; hashes++)
  2733. {
  2734. struct elf32_mn10300_link_hash_entry *sym_hash;
  2735. sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
  2736. if ((sym_hash->root.root.type == bfd_link_hash_defined
  2737. || sym_hash->root.root.type == bfd_link_hash_defweak)
  2738. && sym_hash->root.root.u.def.section == section
  2739. && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
  2740. && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
  2741. {
  2742. int bytes = 0;
  2743. bfd_vma symval;
  2744. struct elf_link_hash_entry **hh;
  2745. /* Note that we've changed things. */
  2746. elf_section_data (section)->relocs = internal_relocs;
  2747. elf_section_data (section)->this_hdr.contents = contents;
  2748. symtab_hdr->contents = (unsigned char *) isymbuf;
  2749. /* Count how many bytes we're going to delete. */
  2750. if (sym_hash->movm_args)
  2751. bytes += 2;
  2752. if (sym_hash->stack_size > 0)
  2753. {
  2754. if (sym_hash->stack_size <= 128)
  2755. bytes += 3;
  2756. else
  2757. bytes += 4;
  2758. }
  2759. /* Note that we've deleted prologue bytes for this
  2760. function. */
  2761. sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
  2762. /* Actually delete the bytes. */
  2763. symval = sym_hash->root.root.u.def.value;
  2764. if (!mn10300_elf_relax_delete_bytes (input_bfd,
  2765. section,
  2766. symval,
  2767. bytes))
  2768. goto error_return;
  2769. /* There may be other C++ functions symbols with the same
  2770. address. If so then mark these as having had their
  2771. prologue bytes deleted as well. */
  2772. for (hh = elf_sym_hashes (input_bfd); hh < end_hashes; hh++)
  2773. {
  2774. struct elf32_mn10300_link_hash_entry *h;
  2775. h = (struct elf32_mn10300_link_hash_entry *) * hh;
  2776. if (h != sym_hash
  2777. && (h->root.root.type == bfd_link_hash_defined
  2778. || h->root.root.type == bfd_link_hash_defweak)
  2779. && h->root.root.u.def.section == section
  2780. && ! (h->flags & MN10300_CONVERT_CALL_TO_CALLS)
  2781. && h->root.root.u.def.value == symval
  2782. && h->root.type == STT_FUNC)
  2783. h->flags |= MN10300_DELETED_PROLOGUE_BYTES;
  2784. }
  2785. /* Something changed. Not strictly necessary, but
  2786. may lead to more relaxing opportunities. */
  2787. *again = true;
  2788. }
  2789. }
  2790. /* Cache or free any memory we allocated for the relocs. */
  2791. if (elf_section_data (section)->relocs != internal_relocs)
  2792. free (internal_relocs);
  2793. internal_relocs = NULL;
  2794. /* Cache or free any memory we allocated for the contents. */
  2795. if (contents != NULL
  2796. && elf_section_data (section)->this_hdr.contents != contents)
  2797. {
  2798. if (! link_info->keep_memory)
  2799. free (contents);
  2800. else
  2801. /* Cache the section contents for elf_link_input_bfd. */
  2802. elf_section_data (section)->this_hdr.contents = contents;
  2803. }
  2804. contents = NULL;
  2805. }
  2806. /* Cache or free any memory we allocated for the symbols. */
  2807. if (isymbuf != NULL
  2808. && symtab_hdr->contents != (unsigned char *) isymbuf)
  2809. {
  2810. if (! link_info->keep_memory)
  2811. free (isymbuf);
  2812. else
  2813. /* Cache the symbols for elf_link_input_bfd. */
  2814. symtab_hdr->contents = (unsigned char *) isymbuf;
  2815. }
  2816. isymbuf = NULL;
  2817. }
  2818. }
  2819. /* (Re)initialize for the basic instruction shortening/relaxing pass. */
  2820. contents = NULL;
  2821. internal_relocs = NULL;
  2822. isymbuf = NULL;
  2823. /* For error_return. */
  2824. section = sec;
  2825. /* We don't have to do anything for a relocatable link, if
  2826. this section does not have relocs, or if this is not a
  2827. code section. */
  2828. if (bfd_link_relocatable (link_info)
  2829. || (sec->flags & SEC_RELOC) == 0
  2830. || sec->reloc_count == 0
  2831. || (sec->flags & SEC_CODE) == 0)
  2832. return true;
  2833. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2834. /* Get a copy of the native relocations. */
  2835. internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  2836. link_info->keep_memory);
  2837. if (internal_relocs == NULL)
  2838. goto error_return;
  2839. /* Scan for worst case alignment gap changes. Note that this logic
  2840. is not ideal; what we should do is run this scan for every
  2841. opcode/address range and adjust accordingly, but that's
  2842. expensive. Worst case is that for an alignment of N bytes, we
  2843. move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
  2844. all before it. Plus, this still doesn't cover cross-section
  2845. jumps with section alignment. */
  2846. irelend = internal_relocs + sec->reloc_count;
  2847. align_gap_adjustment = 0;
  2848. for (irel = internal_relocs; irel < irelend; irel++)
  2849. {
  2850. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
  2851. {
  2852. bfd_vma adj = 1 << irel->r_addend;
  2853. bfd_vma aend = irel->r_offset;
  2854. aend = BFD_ALIGN (aend, 1 << irel->r_addend);
  2855. adj = 2 * adj - adj - 1;
  2856. /* Record the biggest adjustmnet. Skip any alignment at the
  2857. end of our section. */
  2858. if (align_gap_adjustment < adj
  2859. && aend < sec->output_section->vma + sec->output_offset + sec->size)
  2860. align_gap_adjustment = adj;
  2861. }
  2862. }
  2863. /* Walk through them looking for relaxing opportunities. */
  2864. irelend = internal_relocs + sec->reloc_count;
  2865. for (irel = internal_relocs; irel < irelend; irel++)
  2866. {
  2867. bfd_vma symval;
  2868. bfd_signed_vma jump_offset;
  2869. asection *sym_sec = NULL;
  2870. struct elf32_mn10300_link_hash_entry *h = NULL;
  2871. /* If this isn't something that can be relaxed, then ignore
  2872. this reloc. */
  2873. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
  2874. || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
  2875. || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
  2876. continue;
  2877. /* Get the section contents if we haven't done so already. */
  2878. if (contents == NULL)
  2879. {
  2880. /* Get cached copy if it exists. */
  2881. if (elf_section_data (sec)->this_hdr.contents != NULL)
  2882. contents = elf_section_data (sec)->this_hdr.contents;
  2883. else
  2884. {
  2885. /* Go get them off disk. */
  2886. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  2887. goto error_return;
  2888. }
  2889. }
  2890. /* Read this BFD's symbols if we haven't done so already. */
  2891. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  2892. {
  2893. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2894. if (isymbuf == NULL)
  2895. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  2896. symtab_hdr->sh_info, 0,
  2897. NULL, NULL, NULL);
  2898. if (isymbuf == NULL)
  2899. goto error_return;
  2900. }
  2901. /* Get the value of the symbol referred to by the reloc. */
  2902. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  2903. {
  2904. Elf_Internal_Sym *isym;
  2905. const char *sym_name;
  2906. char *new_name;
  2907. /* A local symbol. */
  2908. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  2909. if (isym->st_shndx == SHN_UNDEF)
  2910. sym_sec = bfd_und_section_ptr;
  2911. else if (isym->st_shndx == SHN_ABS)
  2912. sym_sec = bfd_abs_section_ptr;
  2913. else if (isym->st_shndx == SHN_COMMON)
  2914. sym_sec = bfd_com_section_ptr;
  2915. else
  2916. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  2917. sym_name = bfd_elf_string_from_elf_section (abfd,
  2918. symtab_hdr->sh_link,
  2919. isym->st_name);
  2920. if ((sym_sec->flags & SEC_MERGE)
  2921. && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
  2922. {
  2923. symval = isym->st_value;
  2924. /* GAS may reduce relocations against symbols in SEC_MERGE
  2925. sections to a relocation against the section symbol when
  2926. the original addend was zero. When the reloc is against
  2927. a section symbol we should include the addend in the
  2928. offset passed to _bfd_merged_section_offset, since the
  2929. location of interest is the original symbol. On the
  2930. other hand, an access to "sym+addend" where "sym" is not
  2931. a section symbol should not include the addend; Such an
  2932. access is presumed to be an offset from "sym"; The
  2933. location of interest is just "sym". */
  2934. if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  2935. symval += irel->r_addend;
  2936. symval = _bfd_merged_section_offset (abfd, & sym_sec,
  2937. elf_section_data (sym_sec)->sec_info,
  2938. symval);
  2939. if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
  2940. symval += irel->r_addend;
  2941. symval += sym_sec->output_section->vma
  2942. + sym_sec->output_offset - irel->r_addend;
  2943. }
  2944. else
  2945. symval = (isym->st_value
  2946. + sym_sec->output_section->vma
  2947. + sym_sec->output_offset);
  2948. /* Tack on an ID so we can uniquely identify this
  2949. local symbol in the global hash table. */
  2950. new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
  2951. if (new_name == NULL)
  2952. goto error_return;
  2953. sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
  2954. sym_name = new_name;
  2955. h = (struct elf32_mn10300_link_hash_entry *)
  2956. elf_link_hash_lookup (&hash_table->static_hash_table->root,
  2957. sym_name, false, false, false);
  2958. free (new_name);
  2959. }
  2960. else
  2961. {
  2962. unsigned long indx;
  2963. /* An external symbol. */
  2964. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  2965. h = (struct elf32_mn10300_link_hash_entry *)
  2966. (elf_sym_hashes (abfd)[indx]);
  2967. BFD_ASSERT (h != NULL);
  2968. if (h->root.root.type != bfd_link_hash_defined
  2969. && h->root.root.type != bfd_link_hash_defweak)
  2970. /* This appears to be a reference to an undefined
  2971. symbol. Just ignore it--it will be caught by the
  2972. regular reloc processing. */
  2973. continue;
  2974. /* Check for a reference to a discarded symbol and ignore it. */
  2975. if (h->root.root.u.def.section->output_section == NULL)
  2976. continue;
  2977. sym_sec = h->root.root.u.def.section->output_section;
  2978. symval = (h->root.root.u.def.value
  2979. + h->root.root.u.def.section->output_section->vma
  2980. + h->root.root.u.def.section->output_offset);
  2981. }
  2982. /* For simplicity of coding, we are going to modify the section
  2983. contents, the section relocs, and the BFD symbol table. We
  2984. must tell the rest of the code not to free up this
  2985. information. It would be possible to instead create a table
  2986. of changes which have to be made, as is done in coff-mips.c;
  2987. that would be more work, but would require less memory when
  2988. the linker is run. */
  2989. /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
  2990. branch/call, also deal with "call" -> "calls" conversions and
  2991. insertion of prologue data into "call" instructions. */
  2992. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
  2993. || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
  2994. {
  2995. bfd_vma value = symval;
  2996. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
  2997. && h != NULL
  2998. && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
  2999. && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
  3000. && h->root.plt.offset != (bfd_vma) -1)
  3001. {
  3002. asection * splt;
  3003. splt = hash_table->root.splt;
  3004. value = ((splt->output_section->vma
  3005. + splt->output_offset
  3006. + h->root.plt.offset)
  3007. - (sec->output_section->vma
  3008. + sec->output_offset
  3009. + irel->r_offset));
  3010. }
  3011. /* If we've got a "call" instruction that needs to be turned
  3012. into a "calls" instruction, do so now. It saves a byte. */
  3013. if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
  3014. {
  3015. unsigned char code;
  3016. /* Get the opcode. */
  3017. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  3018. /* Make sure we're working with a "call" instruction! */
  3019. if (code == 0xdd)
  3020. {
  3021. /* Note that we've changed the relocs, section contents,
  3022. etc. */
  3023. elf_section_data (sec)->relocs = internal_relocs;
  3024. elf_section_data (sec)->this_hdr.contents = contents;
  3025. symtab_hdr->contents = (unsigned char *) isymbuf;
  3026. /* Fix the opcode. */
  3027. bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
  3028. bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
  3029. /* Fix irel->r_offset and irel->r_addend. */
  3030. irel->r_offset += 1;
  3031. irel->r_addend += 1;
  3032. /* Delete one byte of data. */
  3033. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3034. irel->r_offset + 3, 1))
  3035. goto error_return;
  3036. /* That will change things, so, we should relax again.
  3037. Note that this is not required, and it may be slow. */
  3038. *again = true;
  3039. }
  3040. }
  3041. else if (h)
  3042. {
  3043. /* We've got a "call" instruction which needs some data
  3044. from target function filled in. */
  3045. unsigned char code;
  3046. /* Get the opcode. */
  3047. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  3048. /* Insert data from the target function into the "call"
  3049. instruction if needed. */
  3050. if (code == 0xdd)
  3051. {
  3052. bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
  3053. bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
  3054. contents + irel->r_offset + 5);
  3055. }
  3056. }
  3057. /* Deal with pc-relative gunk. */
  3058. value -= (sec->output_section->vma + sec->output_offset);
  3059. value -= irel->r_offset;
  3060. value += irel->r_addend;
  3061. /* See if the value will fit in 16 bits, note the high value is
  3062. 0x7fff + 2 as the target will be two bytes closer if we are
  3063. able to relax, if it's in the same section. */
  3064. if (sec->output_section == sym_sec->output_section)
  3065. jump_offset = 0x8001;
  3066. else
  3067. jump_offset = 0x7fff;
  3068. /* Account for jumps across alignment boundaries using
  3069. align_gap_adjustment. */
  3070. if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
  3071. && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
  3072. {
  3073. unsigned char code;
  3074. /* Get the opcode. */
  3075. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  3076. if (code != 0xdc && code != 0xdd && code != 0xff)
  3077. continue;
  3078. /* Note that we've changed the relocs, section contents, etc. */
  3079. elf_section_data (sec)->relocs = internal_relocs;
  3080. elf_section_data (sec)->this_hdr.contents = contents;
  3081. symtab_hdr->contents = (unsigned char *) isymbuf;
  3082. /* Fix the opcode. */
  3083. if (code == 0xdc)
  3084. bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
  3085. else if (code == 0xdd)
  3086. bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
  3087. else if (code == 0xff)
  3088. bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
  3089. /* Fix the relocation's type. */
  3090. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3091. (ELF32_R_TYPE (irel->r_info)
  3092. == (int) R_MN10300_PLT32)
  3093. ? R_MN10300_PLT16 :
  3094. R_MN10300_PCREL16);
  3095. /* Delete two bytes of data. */
  3096. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3097. irel->r_offset + 1, 2))
  3098. goto error_return;
  3099. /* That will change things, so, we should relax again.
  3100. Note that this is not required, and it may be slow. */
  3101. *again = true;
  3102. }
  3103. }
  3104. /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
  3105. branch. */
  3106. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
  3107. {
  3108. bfd_vma value = symval;
  3109. /* If we've got a "call" instruction that needs to be turned
  3110. into a "calls" instruction, do so now. It saves a byte. */
  3111. if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
  3112. {
  3113. unsigned char code;
  3114. /* Get the opcode. */
  3115. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  3116. /* Make sure we're working with a "call" instruction! */
  3117. if (code == 0xcd)
  3118. {
  3119. /* Note that we've changed the relocs, section contents,
  3120. etc. */
  3121. elf_section_data (sec)->relocs = internal_relocs;
  3122. elf_section_data (sec)->this_hdr.contents = contents;
  3123. symtab_hdr->contents = (unsigned char *) isymbuf;
  3124. /* Fix the opcode. */
  3125. bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
  3126. bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
  3127. /* Fix irel->r_offset and irel->r_addend. */
  3128. irel->r_offset += 1;
  3129. irel->r_addend += 1;
  3130. /* Delete one byte of data. */
  3131. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3132. irel->r_offset + 1, 1))
  3133. goto error_return;
  3134. /* That will change things, so, we should relax again.
  3135. Note that this is not required, and it may be slow. */
  3136. *again = true;
  3137. }
  3138. }
  3139. else if (h)
  3140. {
  3141. unsigned char code;
  3142. /* Get the opcode. */
  3143. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  3144. /* Insert data from the target function into the "call"
  3145. instruction if needed. */
  3146. if (code == 0xcd)
  3147. {
  3148. bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
  3149. bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
  3150. contents + irel->r_offset + 3);
  3151. }
  3152. }
  3153. /* Deal with pc-relative gunk. */
  3154. value -= (sec->output_section->vma + sec->output_offset);
  3155. value -= irel->r_offset;
  3156. value += irel->r_addend;
  3157. /* See if the value will fit in 8 bits, note the high value is
  3158. 0x7f + 1 as the target will be one bytes closer if we are
  3159. able to relax. */
  3160. if ((long) value < 0x80 && (long) value > -0x80)
  3161. {
  3162. unsigned char code;
  3163. /* Get the opcode. */
  3164. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  3165. if (code != 0xcc)
  3166. continue;
  3167. /* Note that we've changed the relocs, section contents, etc. */
  3168. elf_section_data (sec)->relocs = internal_relocs;
  3169. elf_section_data (sec)->this_hdr.contents = contents;
  3170. symtab_hdr->contents = (unsigned char *) isymbuf;
  3171. /* Fix the opcode. */
  3172. bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
  3173. /* Fix the relocation's type. */
  3174. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3175. R_MN10300_PCREL8);
  3176. /* Delete one byte of data. */
  3177. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3178. irel->r_offset + 1, 1))
  3179. goto error_return;
  3180. /* That will change things, so, we should relax again.
  3181. Note that this is not required, and it may be slow. */
  3182. *again = true;
  3183. }
  3184. }
  3185. /* Try to eliminate an unconditional 8 bit pc-relative branch
  3186. which immediately follows a conditional 8 bit pc-relative
  3187. branch around the unconditional branch.
  3188. original: new:
  3189. bCC lab1 bCC' lab2
  3190. bra lab2
  3191. lab1: lab1:
  3192. This happens when the bCC can't reach lab2 at assembly time,
  3193. but due to other relaxations it can reach at link time. */
  3194. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
  3195. {
  3196. Elf_Internal_Rela *nrel;
  3197. unsigned char code;
  3198. /* Do nothing if this reloc is the last byte in the section. */
  3199. if (irel->r_offset == sec->size)
  3200. continue;
  3201. /* See if the next instruction is an unconditional pc-relative
  3202. branch, more often than not this test will fail, so we
  3203. test it first to speed things up. */
  3204. code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
  3205. if (code != 0xca)
  3206. continue;
  3207. /* Also make sure the next relocation applies to the next
  3208. instruction and that it's a pc-relative 8 bit branch. */
  3209. nrel = irel + 1;
  3210. if (nrel == irelend
  3211. || irel->r_offset + 2 != nrel->r_offset
  3212. || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
  3213. continue;
  3214. /* Make sure our destination immediately follows the
  3215. unconditional branch. */
  3216. if (symval != (sec->output_section->vma + sec->output_offset
  3217. + irel->r_offset + 3))
  3218. continue;
  3219. /* Now make sure we are a conditional branch. This may not
  3220. be necessary, but why take the chance.
  3221. Note these checks assume that R_MN10300_PCREL8 relocs
  3222. only occur on bCC and bCCx insns. If they occured
  3223. elsewhere, we'd need to know the start of this insn
  3224. for this check to be accurate. */
  3225. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  3226. if (code != 0xc0 && code != 0xc1 && code != 0xc2
  3227. && code != 0xc3 && code != 0xc4 && code != 0xc5
  3228. && code != 0xc6 && code != 0xc7 && code != 0xc8
  3229. && code != 0xc9 && code != 0xe8 && code != 0xe9
  3230. && code != 0xea && code != 0xeb)
  3231. continue;
  3232. /* We also have to be sure there is no symbol/label
  3233. at the unconditional branch. */
  3234. if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
  3235. irel->r_offset + 1))
  3236. continue;
  3237. /* Note that we've changed the relocs, section contents, etc. */
  3238. elf_section_data (sec)->relocs = internal_relocs;
  3239. elf_section_data (sec)->this_hdr.contents = contents;
  3240. symtab_hdr->contents = (unsigned char *) isymbuf;
  3241. /* Reverse the condition of the first branch. */
  3242. switch (code)
  3243. {
  3244. case 0xc8:
  3245. code = 0xc9;
  3246. break;
  3247. case 0xc9:
  3248. code = 0xc8;
  3249. break;
  3250. case 0xc0:
  3251. code = 0xc2;
  3252. break;
  3253. case 0xc2:
  3254. code = 0xc0;
  3255. break;
  3256. case 0xc3:
  3257. code = 0xc1;
  3258. break;
  3259. case 0xc1:
  3260. code = 0xc3;
  3261. break;
  3262. case 0xc4:
  3263. code = 0xc6;
  3264. break;
  3265. case 0xc6:
  3266. code = 0xc4;
  3267. break;
  3268. case 0xc7:
  3269. code = 0xc5;
  3270. break;
  3271. case 0xc5:
  3272. code = 0xc7;
  3273. break;
  3274. case 0xe8:
  3275. code = 0xe9;
  3276. break;
  3277. case 0x9d:
  3278. code = 0xe8;
  3279. break;
  3280. case 0xea:
  3281. code = 0xeb;
  3282. break;
  3283. case 0xeb:
  3284. code = 0xea;
  3285. break;
  3286. }
  3287. bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
  3288. /* Set the reloc type and symbol for the first branch
  3289. from the second branch. */
  3290. irel->r_info = nrel->r_info;
  3291. /* Make the reloc for the second branch a null reloc. */
  3292. nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
  3293. R_MN10300_NONE);
  3294. /* Delete two bytes of data. */
  3295. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3296. irel->r_offset + 1, 2))
  3297. goto error_return;
  3298. /* That will change things, so, we should relax again.
  3299. Note that this is not required, and it may be slow. */
  3300. *again = true;
  3301. }
  3302. /* Try to turn a 24 immediate, displacement or absolute address
  3303. into a 8 immediate, displacement or absolute address. */
  3304. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
  3305. {
  3306. bfd_vma value = symval;
  3307. value += irel->r_addend;
  3308. /* See if the value will fit in 8 bits. */
  3309. if ((long) value < 0x7f && (long) value > -0x80)
  3310. {
  3311. unsigned char code;
  3312. /* AM33 insns which have 24 operands are 6 bytes long and
  3313. will have 0xfd as the first byte. */
  3314. /* Get the first opcode. */
  3315. code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
  3316. if (code == 0xfd)
  3317. {
  3318. /* Get the second opcode. */
  3319. code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
  3320. /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
  3321. equivalent instructions exists. */
  3322. if (code != 0x6b && code != 0x7b
  3323. && code != 0x8b && code != 0x9b
  3324. && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
  3325. || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
  3326. || (code & 0x0f) == 0x0e))
  3327. {
  3328. /* Not safe if the high bit is on as relaxing may
  3329. move the value out of high mem and thus not fit
  3330. in a signed 8bit value. This is currently over
  3331. conservative. */
  3332. if ((value & 0x80) == 0)
  3333. {
  3334. /* Note that we've changed the relocation contents,
  3335. etc. */
  3336. elf_section_data (sec)->relocs = internal_relocs;
  3337. elf_section_data (sec)->this_hdr.contents = contents;
  3338. symtab_hdr->contents = (unsigned char *) isymbuf;
  3339. /* Fix the opcode. */
  3340. bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
  3341. bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
  3342. /* Fix the relocation's type. */
  3343. irel->r_info =
  3344. ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3345. R_MN10300_8);
  3346. /* Delete two bytes of data. */
  3347. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3348. irel->r_offset + 1, 2))
  3349. goto error_return;
  3350. /* That will change things, so, we should relax
  3351. again. Note that this is not required, and it
  3352. may be slow. */
  3353. *again = true;
  3354. break;
  3355. }
  3356. }
  3357. }
  3358. }
  3359. }
  3360. /* Try to turn a 32bit immediate, displacement or absolute address
  3361. into a 16bit immediate, displacement or absolute address. */
  3362. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
  3363. || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
  3364. || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
  3365. {
  3366. bfd_vma value = symval;
  3367. if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
  3368. {
  3369. asection * sgot;
  3370. sgot = hash_table->root.sgot;
  3371. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
  3372. {
  3373. value = sgot->output_offset;
  3374. if (h)
  3375. value += h->root.got.offset;
  3376. else
  3377. value += (elf_local_got_offsets
  3378. (abfd)[ELF32_R_SYM (irel->r_info)]);
  3379. }
  3380. else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
  3381. value -= sgot->output_section->vma;
  3382. else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
  3383. value = (sgot->output_section->vma
  3384. - (sec->output_section->vma
  3385. + sec->output_offset
  3386. + irel->r_offset));
  3387. else
  3388. abort ();
  3389. }
  3390. value += irel->r_addend;
  3391. /* See if the value will fit in 24 bits.
  3392. We allow any 16bit match here. We prune those we can't
  3393. handle below. */
  3394. if (value + 0x800000 < 0x1000000 && irel->r_offset >= 3)
  3395. {
  3396. unsigned char code;
  3397. /* AM33 insns which have 32bit operands are 7 bytes long and
  3398. will have 0xfe as the first byte. */
  3399. /* Get the first opcode. */
  3400. code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
  3401. if (code == 0xfe)
  3402. {
  3403. /* Get the second opcode. */
  3404. code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
  3405. /* All the am33 32 -> 24 relaxing possibilities. */
  3406. /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
  3407. equivalent instructions exists. */
  3408. if (code != 0x6b && code != 0x7b
  3409. && code != 0x8b && code != 0x9b
  3410. && (ELF32_R_TYPE (irel->r_info)
  3411. != (int) R_MN10300_GOTPC32)
  3412. && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
  3413. || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
  3414. || (code & 0x0f) == 0x0e))
  3415. {
  3416. /* Not safe if the high bit is on as relaxing may
  3417. move the value out of high mem and thus not fit
  3418. in a signed 16bit value. This is currently over
  3419. conservative. */
  3420. if ((value & 0x8000) == 0)
  3421. {
  3422. /* Note that we've changed the relocation contents,
  3423. etc. */
  3424. elf_section_data (sec)->relocs = internal_relocs;
  3425. elf_section_data (sec)->this_hdr.contents = contents;
  3426. symtab_hdr->contents = (unsigned char *) isymbuf;
  3427. /* Fix the opcode. */
  3428. bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
  3429. bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
  3430. /* Fix the relocation's type. */
  3431. irel->r_info =
  3432. ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3433. (ELF32_R_TYPE (irel->r_info)
  3434. == (int) R_MN10300_GOTOFF32)
  3435. ? R_MN10300_GOTOFF24
  3436. : (ELF32_R_TYPE (irel->r_info)
  3437. == (int) R_MN10300_GOT32)
  3438. ? R_MN10300_GOT24 :
  3439. R_MN10300_24);
  3440. /* Delete one byte of data. */
  3441. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3442. irel->r_offset + 3, 1))
  3443. goto error_return;
  3444. /* That will change things, so, we should relax
  3445. again. Note that this is not required, and it
  3446. may be slow. */
  3447. *again = true;
  3448. break;
  3449. }
  3450. }
  3451. }
  3452. }
  3453. /* See if the value will fit in 16 bits.
  3454. We allow any 16bit match here. We prune those we can't
  3455. handle below. */
  3456. if (value + 0x8000 < 0x10000 && irel->r_offset >= 2)
  3457. {
  3458. unsigned char code;
  3459. /* Most insns which have 32bit operands are 6 bytes long;
  3460. exceptions are pcrel insns and bit insns.
  3461. We handle pcrel insns above. We don't bother trying
  3462. to handle the bit insns here.
  3463. The first byte of the remaining insns will be 0xfc. */
  3464. /* Get the first opcode. */
  3465. code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
  3466. if (code != 0xfc)
  3467. continue;
  3468. /* Get the second opcode. */
  3469. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  3470. if ((code & 0xf0) < 0x80)
  3471. switch (code & 0xf0)
  3472. {
  3473. /* mov (d32,am),dn -> mov (d32,am),dn
  3474. mov dm,(d32,am) -> mov dn,(d32,am)
  3475. mov (d32,am),an -> mov (d32,am),an
  3476. mov dm,(d32,am) -> mov dn,(d32,am)
  3477. movbu (d32,am),dn -> movbu (d32,am),dn
  3478. movbu dm,(d32,am) -> movbu dn,(d32,am)
  3479. movhu (d32,am),dn -> movhu (d32,am),dn
  3480. movhu dm,(d32,am) -> movhu dn,(d32,am) */
  3481. case 0x00:
  3482. case 0x10:
  3483. case 0x20:
  3484. case 0x30:
  3485. case 0x40:
  3486. case 0x50:
  3487. case 0x60:
  3488. case 0x70:
  3489. /* Not safe if the high bit is on as relaxing may
  3490. move the value out of high mem and thus not fit
  3491. in a signed 16bit value. */
  3492. if (code == 0xcc
  3493. && (value & 0x8000))
  3494. continue;
  3495. /* Note that we've changed the relocation contents, etc. */
  3496. elf_section_data (sec)->relocs = internal_relocs;
  3497. elf_section_data (sec)->this_hdr.contents = contents;
  3498. symtab_hdr->contents = (unsigned char *) isymbuf;
  3499. /* Fix the opcode. */
  3500. bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
  3501. bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
  3502. /* Fix the relocation's type. */
  3503. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3504. (ELF32_R_TYPE (irel->r_info)
  3505. == (int) R_MN10300_GOTOFF32)
  3506. ? R_MN10300_GOTOFF16
  3507. : (ELF32_R_TYPE (irel->r_info)
  3508. == (int) R_MN10300_GOT32)
  3509. ? R_MN10300_GOT16
  3510. : (ELF32_R_TYPE (irel->r_info)
  3511. == (int) R_MN10300_GOTPC32)
  3512. ? R_MN10300_GOTPC16 :
  3513. R_MN10300_16);
  3514. /* Delete two bytes of data. */
  3515. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3516. irel->r_offset + 2, 2))
  3517. goto error_return;
  3518. /* That will change things, so, we should relax again.
  3519. Note that this is not required, and it may be slow. */
  3520. *again = true;
  3521. break;
  3522. }
  3523. else if ((code & 0xf0) == 0x80
  3524. || (code & 0xf0) == 0x90)
  3525. switch (code & 0xf3)
  3526. {
  3527. /* mov dn,(abs32) -> mov dn,(abs16)
  3528. movbu dn,(abs32) -> movbu dn,(abs16)
  3529. movhu dn,(abs32) -> movhu dn,(abs16) */
  3530. case 0x81:
  3531. case 0x82:
  3532. case 0x83:
  3533. /* Note that we've changed the relocation contents, etc. */
  3534. elf_section_data (sec)->relocs = internal_relocs;
  3535. elf_section_data (sec)->this_hdr.contents = contents;
  3536. symtab_hdr->contents = (unsigned char *) isymbuf;
  3537. if ((code & 0xf3) == 0x81)
  3538. code = 0x01 + (code & 0x0c);
  3539. else if ((code & 0xf3) == 0x82)
  3540. code = 0x02 + (code & 0x0c);
  3541. else if ((code & 0xf3) == 0x83)
  3542. code = 0x03 + (code & 0x0c);
  3543. else
  3544. abort ();
  3545. /* Fix the opcode. */
  3546. bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
  3547. /* Fix the relocation's type. */
  3548. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3549. (ELF32_R_TYPE (irel->r_info)
  3550. == (int) R_MN10300_GOTOFF32)
  3551. ? R_MN10300_GOTOFF16
  3552. : (ELF32_R_TYPE (irel->r_info)
  3553. == (int) R_MN10300_GOT32)
  3554. ? R_MN10300_GOT16
  3555. : (ELF32_R_TYPE (irel->r_info)
  3556. == (int) R_MN10300_GOTPC32)
  3557. ? R_MN10300_GOTPC16 :
  3558. R_MN10300_16);
  3559. /* The opcode got shorter too, so we have to fix the
  3560. addend and offset too! */
  3561. irel->r_offset -= 1;
  3562. /* Delete three bytes of data. */
  3563. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3564. irel->r_offset + 1, 3))
  3565. goto error_return;
  3566. /* That will change things, so, we should relax again.
  3567. Note that this is not required, and it may be slow. */
  3568. *again = true;
  3569. break;
  3570. /* mov am,(abs32) -> mov am,(abs16)
  3571. mov am,(d32,sp) -> mov am,(d16,sp)
  3572. mov dm,(d32,sp) -> mov dm,(d32,sp)
  3573. movbu dm,(d32,sp) -> movbu dm,(d32,sp)
  3574. movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
  3575. case 0x80:
  3576. case 0x90:
  3577. case 0x91:
  3578. case 0x92:
  3579. case 0x93:
  3580. /* sp-based offsets are zero-extended. */
  3581. if (code >= 0x90 && code <= 0x93
  3582. && (long) value < 0)
  3583. continue;
  3584. /* Note that we've changed the relocation contents, etc. */
  3585. elf_section_data (sec)->relocs = internal_relocs;
  3586. elf_section_data (sec)->this_hdr.contents = contents;
  3587. symtab_hdr->contents = (unsigned char *) isymbuf;
  3588. /* Fix the opcode. */
  3589. bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
  3590. bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
  3591. /* Fix the relocation's type. */
  3592. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3593. (ELF32_R_TYPE (irel->r_info)
  3594. == (int) R_MN10300_GOTOFF32)
  3595. ? R_MN10300_GOTOFF16
  3596. : (ELF32_R_TYPE (irel->r_info)
  3597. == (int) R_MN10300_GOT32)
  3598. ? R_MN10300_GOT16
  3599. : (ELF32_R_TYPE (irel->r_info)
  3600. == (int) R_MN10300_GOTPC32)
  3601. ? R_MN10300_GOTPC16 :
  3602. R_MN10300_16);
  3603. /* Delete two bytes of data. */
  3604. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3605. irel->r_offset + 2, 2))
  3606. goto error_return;
  3607. /* That will change things, so, we should relax again.
  3608. Note that this is not required, and it may be slow. */
  3609. *again = true;
  3610. break;
  3611. }
  3612. else if ((code & 0xf0) < 0xf0)
  3613. switch (code & 0xfc)
  3614. {
  3615. /* mov imm32,dn -> mov imm16,dn
  3616. mov imm32,an -> mov imm16,an
  3617. mov (abs32),dn -> mov (abs16),dn
  3618. movbu (abs32),dn -> movbu (abs16),dn
  3619. movhu (abs32),dn -> movhu (abs16),dn */
  3620. case 0xcc:
  3621. case 0xdc:
  3622. case 0xa4:
  3623. case 0xa8:
  3624. case 0xac:
  3625. /* Not safe if the high bit is on as relaxing may
  3626. move the value out of high mem and thus not fit
  3627. in a signed 16bit value. */
  3628. if (code == 0xcc
  3629. && (value & 0x8000))
  3630. continue;
  3631. /* "mov imm16, an" zero-extends the immediate. */
  3632. if ((code & 0xfc) == 0xdc
  3633. && (long) value < 0)
  3634. continue;
  3635. /* Note that we've changed the relocation contents, etc. */
  3636. elf_section_data (sec)->relocs = internal_relocs;
  3637. elf_section_data (sec)->this_hdr.contents = contents;
  3638. symtab_hdr->contents = (unsigned char *) isymbuf;
  3639. if ((code & 0xfc) == 0xcc)
  3640. code = 0x2c + (code & 0x03);
  3641. else if ((code & 0xfc) == 0xdc)
  3642. code = 0x24 + (code & 0x03);
  3643. else if ((code & 0xfc) == 0xa4)
  3644. code = 0x30 + (code & 0x03);
  3645. else if ((code & 0xfc) == 0xa8)
  3646. code = 0x34 + (code & 0x03);
  3647. else if ((code & 0xfc) == 0xac)
  3648. code = 0x38 + (code & 0x03);
  3649. else
  3650. abort ();
  3651. /* Fix the opcode. */
  3652. bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
  3653. /* Fix the relocation's type. */
  3654. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3655. (ELF32_R_TYPE (irel->r_info)
  3656. == (int) R_MN10300_GOTOFF32)
  3657. ? R_MN10300_GOTOFF16
  3658. : (ELF32_R_TYPE (irel->r_info)
  3659. == (int) R_MN10300_GOT32)
  3660. ? R_MN10300_GOT16
  3661. : (ELF32_R_TYPE (irel->r_info)
  3662. == (int) R_MN10300_GOTPC32)
  3663. ? R_MN10300_GOTPC16 :
  3664. R_MN10300_16);
  3665. /* The opcode got shorter too, so we have to fix the
  3666. addend and offset too! */
  3667. irel->r_offset -= 1;
  3668. /* Delete three bytes of data. */
  3669. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3670. irel->r_offset + 1, 3))
  3671. goto error_return;
  3672. /* That will change things, so, we should relax again.
  3673. Note that this is not required, and it may be slow. */
  3674. *again = true;
  3675. break;
  3676. /* mov (abs32),an -> mov (abs16),an
  3677. mov (d32,sp),an -> mov (d16,sp),an
  3678. mov (d32,sp),dn -> mov (d16,sp),dn
  3679. movbu (d32,sp),dn -> movbu (d16,sp),dn
  3680. movhu (d32,sp),dn -> movhu (d16,sp),dn
  3681. add imm32,dn -> add imm16,dn
  3682. cmp imm32,dn -> cmp imm16,dn
  3683. add imm32,an -> add imm16,an
  3684. cmp imm32,an -> cmp imm16,an
  3685. and imm32,dn -> and imm16,dn
  3686. or imm32,dn -> or imm16,dn
  3687. xor imm32,dn -> xor imm16,dn
  3688. btst imm32,dn -> btst imm16,dn */
  3689. case 0xa0:
  3690. case 0xb0:
  3691. case 0xb1:
  3692. case 0xb2:
  3693. case 0xb3:
  3694. case 0xc0:
  3695. case 0xc8:
  3696. case 0xd0:
  3697. case 0xd8:
  3698. case 0xe0:
  3699. case 0xe1:
  3700. case 0xe2:
  3701. case 0xe3:
  3702. /* cmp imm16, an zero-extends the immediate. */
  3703. if (code == 0xdc
  3704. && (long) value < 0)
  3705. continue;
  3706. /* So do sp-based offsets. */
  3707. if (code >= 0xb0 && code <= 0xb3
  3708. && (long) value < 0)
  3709. continue;
  3710. /* Note that we've changed the relocation contents, etc. */
  3711. elf_section_data (sec)->relocs = internal_relocs;
  3712. elf_section_data (sec)->this_hdr.contents = contents;
  3713. symtab_hdr->contents = (unsigned char *) isymbuf;
  3714. /* Fix the opcode. */
  3715. bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
  3716. bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
  3717. /* Fix the relocation's type. */
  3718. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3719. (ELF32_R_TYPE (irel->r_info)
  3720. == (int) R_MN10300_GOTOFF32)
  3721. ? R_MN10300_GOTOFF16
  3722. : (ELF32_R_TYPE (irel->r_info)
  3723. == (int) R_MN10300_GOT32)
  3724. ? R_MN10300_GOT16
  3725. : (ELF32_R_TYPE (irel->r_info)
  3726. == (int) R_MN10300_GOTPC32)
  3727. ? R_MN10300_GOTPC16 :
  3728. R_MN10300_16);
  3729. /* Delete two bytes of data. */
  3730. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3731. irel->r_offset + 2, 2))
  3732. goto error_return;
  3733. /* That will change things, so, we should relax again.
  3734. Note that this is not required, and it may be slow. */
  3735. *again = true;
  3736. break;
  3737. }
  3738. else if (code == 0xfe)
  3739. {
  3740. /* add imm32,sp -> add imm16,sp */
  3741. /* Note that we've changed the relocation contents, etc. */
  3742. elf_section_data (sec)->relocs = internal_relocs;
  3743. elf_section_data (sec)->this_hdr.contents = contents;
  3744. symtab_hdr->contents = (unsigned char *) isymbuf;
  3745. /* Fix the opcode. */
  3746. bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
  3747. bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
  3748. /* Fix the relocation's type. */
  3749. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  3750. (ELF32_R_TYPE (irel->r_info)
  3751. == (int) R_MN10300_GOT32)
  3752. ? R_MN10300_GOT16
  3753. : (ELF32_R_TYPE (irel->r_info)
  3754. == (int) R_MN10300_GOTOFF32)
  3755. ? R_MN10300_GOTOFF16
  3756. : (ELF32_R_TYPE (irel->r_info)
  3757. == (int) R_MN10300_GOTPC32)
  3758. ? R_MN10300_GOTPC16 :
  3759. R_MN10300_16);
  3760. /* Delete two bytes of data. */
  3761. if (!mn10300_elf_relax_delete_bytes (abfd, sec,
  3762. irel->r_offset + 2, 2))
  3763. goto error_return;
  3764. /* That will change things, so, we should relax again.
  3765. Note that this is not required, and it may be slow. */
  3766. *again = true;
  3767. break;
  3768. }
  3769. }
  3770. }
  3771. }
  3772. if (isymbuf != NULL
  3773. && symtab_hdr->contents != (unsigned char *) isymbuf)
  3774. {
  3775. if (! link_info->keep_memory)
  3776. free (isymbuf);
  3777. else
  3778. {
  3779. /* Cache the symbols for elf_link_input_bfd. */
  3780. symtab_hdr->contents = (unsigned char *) isymbuf;
  3781. }
  3782. }
  3783. if (contents != NULL
  3784. && elf_section_data (sec)->this_hdr.contents != contents)
  3785. {
  3786. if (! link_info->keep_memory)
  3787. free (contents);
  3788. else
  3789. {
  3790. /* Cache the section contents for elf_link_input_bfd. */
  3791. elf_section_data (sec)->this_hdr.contents = contents;
  3792. }
  3793. }
  3794. if (elf_section_data (sec)->relocs != internal_relocs)
  3795. free (internal_relocs);
  3796. return true;
  3797. error_return:
  3798. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  3799. free (isymbuf);
  3800. if (elf_section_data (section)->this_hdr.contents != contents)
  3801. free (contents);
  3802. if (elf_section_data (section)->relocs != internal_relocs)
  3803. free (internal_relocs);
  3804. return false;
  3805. }
  3806. /* This is a version of bfd_generic_get_relocated_section_contents
  3807. which uses mn10300_elf_relocate_section. */
  3808. static bfd_byte *
  3809. mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
  3810. struct bfd_link_info *link_info,
  3811. struct bfd_link_order *link_order,
  3812. bfd_byte *data,
  3813. bool relocatable,
  3814. asymbol **symbols)
  3815. {
  3816. Elf_Internal_Shdr *symtab_hdr;
  3817. asection *input_section = link_order->u.indirect.section;
  3818. bfd *input_bfd = input_section->owner;
  3819. asection **sections = NULL;
  3820. Elf_Internal_Rela *internal_relocs = NULL;
  3821. Elf_Internal_Sym *isymbuf = NULL;
  3822. /* We only need to handle the case of relaxing, or of having a
  3823. particular set of section contents, specially. */
  3824. if (relocatable
  3825. || elf_section_data (input_section)->this_hdr.contents == NULL)
  3826. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  3827. link_order, data,
  3828. relocatable,
  3829. symbols);
  3830. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  3831. memcpy (data, elf_section_data (input_section)->this_hdr.contents,
  3832. (size_t) input_section->size);
  3833. if ((input_section->flags & SEC_RELOC) != 0
  3834. && input_section->reloc_count > 0)
  3835. {
  3836. asection **secpp;
  3837. Elf_Internal_Sym *isym, *isymend;
  3838. bfd_size_type amt;
  3839. internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
  3840. NULL, NULL, false);
  3841. if (internal_relocs == NULL)
  3842. goto error_return;
  3843. if (symtab_hdr->sh_info != 0)
  3844. {
  3845. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  3846. if (isymbuf == NULL)
  3847. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  3848. symtab_hdr->sh_info, 0,
  3849. NULL, NULL, NULL);
  3850. if (isymbuf == NULL)
  3851. goto error_return;
  3852. }
  3853. amt = symtab_hdr->sh_info;
  3854. amt *= sizeof (asection *);
  3855. sections = bfd_malloc (amt);
  3856. if (sections == NULL && amt != 0)
  3857. goto error_return;
  3858. isymend = isymbuf + symtab_hdr->sh_info;
  3859. for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
  3860. {
  3861. asection *isec;
  3862. if (isym->st_shndx == SHN_UNDEF)
  3863. isec = bfd_und_section_ptr;
  3864. else if (isym->st_shndx == SHN_ABS)
  3865. isec = bfd_abs_section_ptr;
  3866. else if (isym->st_shndx == SHN_COMMON)
  3867. isec = bfd_com_section_ptr;
  3868. else
  3869. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  3870. *secpp = isec;
  3871. }
  3872. if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
  3873. input_section, data, internal_relocs,
  3874. isymbuf, sections))
  3875. goto error_return;
  3876. free (sections);
  3877. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  3878. free (isymbuf);
  3879. if (internal_relocs != elf_section_data (input_section)->relocs)
  3880. free (internal_relocs);
  3881. }
  3882. return data;
  3883. error_return:
  3884. free (sections);
  3885. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  3886. free (isymbuf);
  3887. if (internal_relocs != elf_section_data (input_section)->relocs)
  3888. free (internal_relocs);
  3889. return NULL;
  3890. }
  3891. /* Assorted hash table functions. */
  3892. /* Initialize an entry in the link hash table. */
  3893. /* Create an entry in an MN10300 ELF linker hash table. */
  3894. static struct bfd_hash_entry *
  3895. elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
  3896. struct bfd_hash_table *table,
  3897. const char *string)
  3898. {
  3899. struct elf32_mn10300_link_hash_entry *ret =
  3900. (struct elf32_mn10300_link_hash_entry *) entry;
  3901. /* Allocate the structure if it has not already been allocated by a
  3902. subclass. */
  3903. if (ret == NULL)
  3904. ret = (struct elf32_mn10300_link_hash_entry *)
  3905. bfd_hash_allocate (table, sizeof (* ret));
  3906. if (ret == NULL)
  3907. return (struct bfd_hash_entry *) ret;
  3908. /* Call the allocation method of the superclass. */
  3909. ret = (struct elf32_mn10300_link_hash_entry *)
  3910. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  3911. table, string);
  3912. if (ret != NULL)
  3913. {
  3914. ret->direct_calls = 0;
  3915. ret->stack_size = 0;
  3916. ret->movm_args = 0;
  3917. ret->movm_stack_size = 0;
  3918. ret->flags = 0;
  3919. ret->value = 0;
  3920. ret->tls_type = GOT_UNKNOWN;
  3921. }
  3922. return (struct bfd_hash_entry *) ret;
  3923. }
  3924. static void
  3925. _bfd_mn10300_copy_indirect_symbol (struct bfd_link_info * info,
  3926. struct elf_link_hash_entry * dir,
  3927. struct elf_link_hash_entry * ind)
  3928. {
  3929. struct elf32_mn10300_link_hash_entry * edir;
  3930. struct elf32_mn10300_link_hash_entry * eind;
  3931. edir = elf_mn10300_hash_entry (dir);
  3932. eind = elf_mn10300_hash_entry (ind);
  3933. if (ind->root.type == bfd_link_hash_indirect
  3934. && dir->got.refcount <= 0)
  3935. {
  3936. edir->tls_type = eind->tls_type;
  3937. eind->tls_type = GOT_UNKNOWN;
  3938. }
  3939. edir->direct_calls = eind->direct_calls;
  3940. edir->stack_size = eind->stack_size;
  3941. edir->movm_args = eind->movm_args;
  3942. edir->movm_stack_size = eind->movm_stack_size;
  3943. edir->flags = eind->flags;
  3944. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  3945. }
  3946. /* Destroy an mn10300 ELF linker hash table. */
  3947. static void
  3948. elf32_mn10300_link_hash_table_free (bfd *obfd)
  3949. {
  3950. struct elf32_mn10300_link_hash_table *ret
  3951. = (struct elf32_mn10300_link_hash_table *) obfd->link.hash;
  3952. obfd->link.hash = &ret->static_hash_table->root.root;
  3953. _bfd_elf_link_hash_table_free (obfd);
  3954. obfd->is_linker_output = true;
  3955. obfd->link.hash = &ret->root.root;
  3956. _bfd_elf_link_hash_table_free (obfd);
  3957. }
  3958. /* Create an mn10300 ELF linker hash table. */
  3959. static struct bfd_link_hash_table *
  3960. elf32_mn10300_link_hash_table_create (bfd *abfd)
  3961. {
  3962. struct elf32_mn10300_link_hash_table *ret;
  3963. size_t amt = sizeof (* ret);
  3964. ret = bfd_zmalloc (amt);
  3965. if (ret == NULL)
  3966. return NULL;
  3967. amt = sizeof (struct elf_link_hash_table);
  3968. ret->static_hash_table = bfd_zmalloc (amt);
  3969. if (ret->static_hash_table == NULL)
  3970. {
  3971. free (ret);
  3972. return NULL;
  3973. }
  3974. if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
  3975. elf32_mn10300_link_hash_newfunc,
  3976. sizeof (struct elf32_mn10300_link_hash_entry),
  3977. MN10300_ELF_DATA))
  3978. {
  3979. free (ret->static_hash_table);
  3980. free (ret);
  3981. return NULL;
  3982. }
  3983. abfd->is_linker_output = false;
  3984. abfd->link.hash = NULL;
  3985. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  3986. elf32_mn10300_link_hash_newfunc,
  3987. sizeof (struct elf32_mn10300_link_hash_entry),
  3988. MN10300_ELF_DATA))
  3989. {
  3990. abfd->is_linker_output = true;
  3991. abfd->link.hash = &ret->static_hash_table->root.root;
  3992. _bfd_elf_link_hash_table_free (abfd);
  3993. free (ret);
  3994. return NULL;
  3995. }
  3996. ret->root.root.hash_table_free = elf32_mn10300_link_hash_table_free;
  3997. ret->tls_ldm_got.offset = -1;
  3998. return & ret->root.root;
  3999. }
  4000. static unsigned long
  4001. elf_mn10300_mach (flagword flags)
  4002. {
  4003. switch (flags & EF_MN10300_MACH)
  4004. {
  4005. case E_MN10300_MACH_MN10300:
  4006. default:
  4007. return bfd_mach_mn10300;
  4008. case E_MN10300_MACH_AM33:
  4009. return bfd_mach_am33;
  4010. case E_MN10300_MACH_AM33_2:
  4011. return bfd_mach_am33_2;
  4012. }
  4013. }
  4014. /* The final processing done just before writing out a MN10300 ELF object
  4015. file. This gets the MN10300 architecture right based on the machine
  4016. number. */
  4017. static bool
  4018. _bfd_mn10300_elf_final_write_processing (bfd *abfd)
  4019. {
  4020. unsigned long val;
  4021. switch (bfd_get_mach (abfd))
  4022. {
  4023. default:
  4024. case bfd_mach_mn10300:
  4025. val = E_MN10300_MACH_MN10300;
  4026. break;
  4027. case bfd_mach_am33:
  4028. val = E_MN10300_MACH_AM33;
  4029. break;
  4030. case bfd_mach_am33_2:
  4031. val = E_MN10300_MACH_AM33_2;
  4032. break;
  4033. }
  4034. elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
  4035. elf_elfheader (abfd)->e_flags |= val;
  4036. return _bfd_elf_final_write_processing (abfd);
  4037. }
  4038. static bool
  4039. _bfd_mn10300_elf_object_p (bfd *abfd)
  4040. {
  4041. bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
  4042. elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
  4043. return true;
  4044. }
  4045. /* Merge backend specific data from an object file to the output
  4046. object file when linking. */
  4047. static bool
  4048. _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  4049. {
  4050. bfd *obfd = info->output_bfd;
  4051. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  4052. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  4053. return true;
  4054. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  4055. && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
  4056. {
  4057. if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  4058. bfd_get_mach (ibfd)))
  4059. return false;
  4060. }
  4061. return true;
  4062. }
  4063. #define PLT0_ENTRY_SIZE 15
  4064. #define PLT_ENTRY_SIZE 20
  4065. #define PIC_PLT_ENTRY_SIZE 24
  4066. static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
  4067. {
  4068. 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
  4069. 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
  4070. 0xf0, 0xf4, /* jmp (a0) */
  4071. };
  4072. static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
  4073. {
  4074. 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
  4075. 0xf0, 0xf4, /* jmp (a0) */
  4076. 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
  4077. 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
  4078. };
  4079. static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
  4080. {
  4081. 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
  4082. 0xf0, 0xf4, /* jmp (a0) */
  4083. 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
  4084. 0xf8, 0x22, 8, /* mov (8,a2),a0 */
  4085. 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
  4086. 0xf0, 0xf4, /* jmp (a0) */
  4087. };
  4088. /* Return size of the first PLT entry. */
  4089. #define elf_mn10300_sizeof_plt0(info) \
  4090. (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
  4091. /* Return size of a PLT entry. */
  4092. #define elf_mn10300_sizeof_plt(info) \
  4093. (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
  4094. /* Return offset of the PLT0 address in an absolute PLT entry. */
  4095. #define elf_mn10300_plt_plt0_offset(info) 16
  4096. /* Return offset of the linker in PLT0 entry. */
  4097. #define elf_mn10300_plt0_linker_offset(info) 2
  4098. /* Return offset of the GOT id in PLT0 entry. */
  4099. #define elf_mn10300_plt0_gotid_offset(info) 9
  4100. /* Return offset of the temporary in PLT entry. */
  4101. #define elf_mn10300_plt_temp_offset(info) 8
  4102. /* Return offset of the symbol in PLT entry. */
  4103. #define elf_mn10300_plt_symbol_offset(info) 2
  4104. /* Return offset of the relocation in PLT entry. */
  4105. #define elf_mn10300_plt_reloc_offset(info) 11
  4106. /* The name of the dynamic interpreter. This is put in the .interp
  4107. section. */
  4108. #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
  4109. /* Create dynamic sections when linking against a dynamic object. */
  4110. static bool
  4111. _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  4112. {
  4113. flagword flags;
  4114. asection * s;
  4115. const struct elf_backend_data * bed = get_elf_backend_data (abfd);
  4116. struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
  4117. int ptralign = 0;
  4118. switch (bed->s->arch_size)
  4119. {
  4120. case 32:
  4121. ptralign = 2;
  4122. break;
  4123. case 64:
  4124. ptralign = 3;
  4125. break;
  4126. default:
  4127. bfd_set_error (bfd_error_bad_value);
  4128. return false;
  4129. }
  4130. /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  4131. .rel[a].bss sections. */
  4132. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  4133. | SEC_LINKER_CREATED);
  4134. s = bfd_make_section_anyway_with_flags (abfd,
  4135. (bed->default_use_rela_p
  4136. ? ".rela.plt" : ".rel.plt"),
  4137. flags | SEC_READONLY);
  4138. htab->root.srelplt = s;
  4139. if (s == NULL
  4140. || !bfd_set_section_alignment (s, ptralign))
  4141. return false;
  4142. if (! _bfd_mn10300_elf_create_got_section (abfd, info))
  4143. return false;
  4144. if (bed->want_dynbss)
  4145. {
  4146. /* The .dynbss section is a place to put symbols which are defined
  4147. by dynamic objects, are referenced by regular objects, and are
  4148. not functions. We must allocate space for them in the process
  4149. image and use a R_*_COPY reloc to tell the dynamic linker to
  4150. initialize them at run time. The linker script puts the .dynbss
  4151. section into the .bss section of the final image. */
  4152. s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
  4153. SEC_ALLOC | SEC_LINKER_CREATED);
  4154. if (s == NULL)
  4155. return false;
  4156. /* The .rel[a].bss section holds copy relocs. This section is not
  4157. normally needed. We need to create it here, though, so that the
  4158. linker will map it to an output section. We can't just create it
  4159. only if we need it, because we will not know whether we need it
  4160. until we have seen all the input files, and the first time the
  4161. main linker code calls BFD after examining all the input files
  4162. (size_dynamic_sections) the input sections have already been
  4163. mapped to the output sections. If the section turns out not to
  4164. be needed, we can discard it later. We will never need this
  4165. section when generating a shared object, since they do not use
  4166. copy relocs. */
  4167. if (! bfd_link_pic (info))
  4168. {
  4169. s = bfd_make_section_anyway_with_flags (abfd,
  4170. (bed->default_use_rela_p
  4171. ? ".rela.bss" : ".rel.bss"),
  4172. flags | SEC_READONLY);
  4173. if (s == NULL
  4174. || !bfd_set_section_alignment (s, ptralign))
  4175. return false;
  4176. }
  4177. }
  4178. return true;
  4179. }
  4180. /* Adjust a symbol defined by a dynamic object and referenced by a
  4181. regular object. The current definition is in some section of the
  4182. dynamic object, but we're not including those sections. We have to
  4183. change the definition to something the rest of the link can
  4184. understand. */
  4185. static bool
  4186. _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
  4187. struct elf_link_hash_entry * h)
  4188. {
  4189. struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
  4190. bfd * dynobj;
  4191. asection * s;
  4192. dynobj = htab->root.dynobj;
  4193. /* Make sure we know what is going on here. */
  4194. BFD_ASSERT (dynobj != NULL
  4195. && (h->needs_plt
  4196. || h->is_weakalias
  4197. || (h->def_dynamic
  4198. && h->ref_regular
  4199. && !h->def_regular)));
  4200. /* If this is a function, put it in the procedure linkage table. We
  4201. will fill in the contents of the procedure linkage table later,
  4202. when we know the address of the .got section. */
  4203. if (h->type == STT_FUNC
  4204. || h->needs_plt)
  4205. {
  4206. if (! bfd_link_pic (info)
  4207. && !h->def_dynamic
  4208. && !h->ref_dynamic)
  4209. {
  4210. /* This case can occur if we saw a PLT reloc in an input
  4211. file, but the symbol was never referred to by a dynamic
  4212. object. In such a case, we don't actually need to build
  4213. a procedure linkage table, and we can just do a REL32
  4214. reloc instead. */
  4215. BFD_ASSERT (h->needs_plt);
  4216. return true;
  4217. }
  4218. /* Make sure this symbol is output as a dynamic symbol. */
  4219. if (h->dynindx == -1)
  4220. {
  4221. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  4222. return false;
  4223. }
  4224. s = htab->root.splt;
  4225. BFD_ASSERT (s != NULL);
  4226. /* If this is the first .plt entry, make room for the special
  4227. first entry. */
  4228. if (s->size == 0)
  4229. s->size += elf_mn10300_sizeof_plt0 (info);
  4230. /* If this symbol is not defined in a regular file, and we are
  4231. not generating a shared library, then set the symbol to this
  4232. location in the .plt. This is required to make function
  4233. pointers compare as equal between the normal executable and
  4234. the shared library. */
  4235. if (! bfd_link_pic (info)
  4236. && !h->def_regular)
  4237. {
  4238. h->root.u.def.section = s;
  4239. h->root.u.def.value = s->size;
  4240. }
  4241. h->plt.offset = s->size;
  4242. /* Make room for this entry. */
  4243. s->size += elf_mn10300_sizeof_plt (info);
  4244. /* We also need to make an entry in the .got.plt section, which
  4245. will be placed in the .got section by the linker script. */
  4246. s = htab->root.sgotplt;
  4247. BFD_ASSERT (s != NULL);
  4248. s->size += 4;
  4249. /* We also need to make an entry in the .rela.plt section. */
  4250. s = htab->root.srelplt;
  4251. BFD_ASSERT (s != NULL);
  4252. s->size += sizeof (Elf32_External_Rela);
  4253. return true;
  4254. }
  4255. /* If this is a weak symbol, and there is a real definition, the
  4256. processor independent code will have arranged for us to see the
  4257. real definition first, and we can just use the same value. */
  4258. if (h->is_weakalias)
  4259. {
  4260. struct elf_link_hash_entry *def = weakdef (h);
  4261. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  4262. h->root.u.def.section = def->root.u.def.section;
  4263. h->root.u.def.value = def->root.u.def.value;
  4264. return true;
  4265. }
  4266. /* This is a reference to a symbol defined by a dynamic object which
  4267. is not a function. */
  4268. /* If we are creating a shared library, we must presume that the
  4269. only references to the symbol are via the global offset table.
  4270. For such cases we need not do anything here; the relocations will
  4271. be handled correctly by relocate_section. */
  4272. if (bfd_link_pic (info))
  4273. return true;
  4274. /* If there are no references to this symbol that do not use the
  4275. GOT, we don't need to generate a copy reloc. */
  4276. if (!h->non_got_ref)
  4277. return true;
  4278. /* We must allocate the symbol in our .dynbss section, which will
  4279. become part of the .bss section of the executable. There will be
  4280. an entry for this symbol in the .dynsym section. The dynamic
  4281. object will contain position independent code, so all references
  4282. from the dynamic object to this symbol will go through the global
  4283. offset table. The dynamic linker will use the .dynsym entry to
  4284. determine the address it must put in the global offset table, so
  4285. both the dynamic object and the regular object will refer to the
  4286. same memory location for the variable. */
  4287. s = bfd_get_linker_section (dynobj, ".dynbss");
  4288. BFD_ASSERT (s != NULL);
  4289. /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
  4290. copy the initial value out of the dynamic object and into the
  4291. runtime process image. We need to remember the offset into the
  4292. .rela.bss section we are going to use. */
  4293. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  4294. {
  4295. asection * srel;
  4296. srel = bfd_get_linker_section (dynobj, ".rela.bss");
  4297. BFD_ASSERT (srel != NULL);
  4298. srel->size += sizeof (Elf32_External_Rela);
  4299. h->needs_copy = 1;
  4300. }
  4301. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  4302. }
  4303. /* Set the sizes of the dynamic sections. */
  4304. static bool
  4305. _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
  4306. struct bfd_link_info * info)
  4307. {
  4308. struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
  4309. bfd * dynobj;
  4310. asection * s;
  4311. bool relocs;
  4312. dynobj = htab->root.dynobj;
  4313. BFD_ASSERT (dynobj != NULL);
  4314. if (elf_hash_table (info)->dynamic_sections_created)
  4315. {
  4316. /* Set the contents of the .interp section to the interpreter. */
  4317. if (bfd_link_executable (info) && !info->nointerp)
  4318. {
  4319. s = bfd_get_linker_section (dynobj, ".interp");
  4320. BFD_ASSERT (s != NULL);
  4321. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  4322. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  4323. }
  4324. }
  4325. else
  4326. {
  4327. /* We may have created entries in the .rela.got section.
  4328. However, if we are not creating the dynamic sections, we will
  4329. not actually use these entries. Reset the size of .rela.got,
  4330. which will cause it to get stripped from the output file
  4331. below. */
  4332. s = htab->root.sgot;
  4333. if (s != NULL)
  4334. s->size = 0;
  4335. }
  4336. if (htab->tls_ldm_got.refcount > 0)
  4337. {
  4338. s = htab->root.srelgot;
  4339. BFD_ASSERT (s != NULL);
  4340. s->size += sizeof (Elf32_External_Rela);
  4341. }
  4342. /* The check_relocs and adjust_dynamic_symbol entry points have
  4343. determined the sizes of the various dynamic sections. Allocate
  4344. memory for them. */
  4345. relocs = false;
  4346. for (s = dynobj->sections; s != NULL; s = s->next)
  4347. {
  4348. const char * name;
  4349. if ((s->flags & SEC_LINKER_CREATED) == 0)
  4350. continue;
  4351. /* It's OK to base decisions on the section name, because none
  4352. of the dynobj section names depend upon the input files. */
  4353. name = bfd_section_name (s);
  4354. if (streq (name, ".plt"))
  4355. {
  4356. /* Remember whether there is a PLT. */
  4357. ;
  4358. }
  4359. else if (startswith (name, ".rela"))
  4360. {
  4361. if (s->size != 0)
  4362. {
  4363. /* Remember whether there are any reloc sections other
  4364. than .rela.plt. */
  4365. if (! streq (name, ".rela.plt"))
  4366. relocs = true;
  4367. /* We use the reloc_count field as a counter if we need
  4368. to copy relocs into the output file. */
  4369. s->reloc_count = 0;
  4370. }
  4371. }
  4372. else if (! startswith (name, ".got")
  4373. && ! streq (name, ".dynbss"))
  4374. /* It's not one of our sections, so don't allocate space. */
  4375. continue;
  4376. if (s->size == 0)
  4377. {
  4378. /* If we don't need this section, strip it from the
  4379. output file. This is mostly to handle .rela.bss and
  4380. .rela.plt. We must create both sections in
  4381. create_dynamic_sections, because they must be created
  4382. before the linker maps input sections to output
  4383. sections. The linker does that before
  4384. adjust_dynamic_symbol is called, and it is that
  4385. function which decides whether anything needs to go
  4386. into these sections. */
  4387. s->flags |= SEC_EXCLUDE;
  4388. continue;
  4389. }
  4390. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  4391. continue;
  4392. /* Allocate memory for the section contents. We use bfd_zalloc
  4393. here in case unused entries are not reclaimed before the
  4394. section's contents are written out. This should not happen,
  4395. but this way if it does, we get a R_MN10300_NONE reloc
  4396. instead of garbage. */
  4397. s->contents = bfd_zalloc (dynobj, s->size);
  4398. if (s->contents == NULL)
  4399. return false;
  4400. }
  4401. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
  4402. }
  4403. /* Finish up dynamic symbol handling. We set the contents of various
  4404. dynamic sections here. */
  4405. static bool
  4406. _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
  4407. struct bfd_link_info * info,
  4408. struct elf_link_hash_entry * h,
  4409. Elf_Internal_Sym * sym)
  4410. {
  4411. struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
  4412. bfd * dynobj;
  4413. dynobj = htab->root.dynobj;
  4414. if (h->plt.offset != (bfd_vma) -1)
  4415. {
  4416. asection * splt;
  4417. asection * sgot;
  4418. asection * srel;
  4419. bfd_vma plt_index;
  4420. bfd_vma got_offset;
  4421. Elf_Internal_Rela rel;
  4422. /* This symbol has an entry in the procedure linkage table. Set
  4423. it up. */
  4424. BFD_ASSERT (h->dynindx != -1);
  4425. splt = htab->root.splt;
  4426. sgot = htab->root.sgotplt;
  4427. srel = htab->root.srelplt;
  4428. BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
  4429. /* Get the index in the procedure linkage table which
  4430. corresponds to this symbol. This is the index of this symbol
  4431. in all the symbols for which we are making plt entries. The
  4432. first entry in the procedure linkage table is reserved. */
  4433. plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
  4434. / elf_mn10300_sizeof_plt (info));
  4435. /* Get the offset into the .got table of the entry that
  4436. corresponds to this function. Each .got entry is 4 bytes.
  4437. The first three are reserved. */
  4438. got_offset = (plt_index + 3) * 4;
  4439. /* Fill in the entry in the procedure linkage table. */
  4440. if (! bfd_link_pic (info))
  4441. {
  4442. memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
  4443. elf_mn10300_sizeof_plt (info));
  4444. bfd_put_32 (output_bfd,
  4445. (sgot->output_section->vma
  4446. + sgot->output_offset
  4447. + got_offset),
  4448. (splt->contents + h->plt.offset
  4449. + elf_mn10300_plt_symbol_offset (info)));
  4450. bfd_put_32 (output_bfd,
  4451. (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
  4452. (splt->contents + h->plt.offset
  4453. + elf_mn10300_plt_plt0_offset (info)));
  4454. }
  4455. else
  4456. {
  4457. memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
  4458. elf_mn10300_sizeof_plt (info));
  4459. bfd_put_32 (output_bfd, got_offset,
  4460. (splt->contents + h->plt.offset
  4461. + elf_mn10300_plt_symbol_offset (info)));
  4462. }
  4463. bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
  4464. (splt->contents + h->plt.offset
  4465. + elf_mn10300_plt_reloc_offset (info)));
  4466. /* Fill in the entry in the global offset table. */
  4467. bfd_put_32 (output_bfd,
  4468. (splt->output_section->vma
  4469. + splt->output_offset
  4470. + h->plt.offset
  4471. + elf_mn10300_plt_temp_offset (info)),
  4472. sgot->contents + got_offset);
  4473. /* Fill in the entry in the .rela.plt section. */
  4474. rel.r_offset = (sgot->output_section->vma
  4475. + sgot->output_offset
  4476. + got_offset);
  4477. rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
  4478. rel.r_addend = 0;
  4479. bfd_elf32_swap_reloca_out (output_bfd, &rel,
  4480. (bfd_byte *) ((Elf32_External_Rela *) srel->contents
  4481. + plt_index));
  4482. if (!h->def_regular)
  4483. /* Mark the symbol as undefined, rather than as defined in
  4484. the .plt section. Leave the value alone. */
  4485. sym->st_shndx = SHN_UNDEF;
  4486. }
  4487. if (h->got.offset != (bfd_vma) -1)
  4488. {
  4489. asection * sgot;
  4490. asection * srel;
  4491. Elf_Internal_Rela rel;
  4492. /* This symbol has an entry in the global offset table. Set it up. */
  4493. sgot = htab->root.sgot;
  4494. srel = htab->root.srelgot;
  4495. BFD_ASSERT (sgot != NULL && srel != NULL);
  4496. rel.r_offset = (sgot->output_section->vma
  4497. + sgot->output_offset
  4498. + (h->got.offset & ~1));
  4499. switch (elf_mn10300_hash_entry (h)->tls_type)
  4500. {
  4501. case GOT_TLS_GD:
  4502. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
  4503. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset + 4);
  4504. rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPMOD);
  4505. rel.r_addend = 0;
  4506. bfd_elf32_swap_reloca_out (output_bfd, & rel,
  4507. (bfd_byte *) ((Elf32_External_Rela *) srel->contents
  4508. + srel->reloc_count));
  4509. ++ srel->reloc_count;
  4510. rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPOFF);
  4511. rel.r_offset += 4;
  4512. rel.r_addend = 0;
  4513. break;
  4514. case GOT_TLS_IE:
  4515. /* We originally stored the addend in the GOT, but at this
  4516. point, we want to move it to the reloc instead as that's
  4517. where the dynamic linker wants it. */
  4518. rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + h->got.offset);
  4519. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
  4520. if (h->dynindx == -1)
  4521. rel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_TPOFF);
  4522. else
  4523. rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_TPOFF);
  4524. break;
  4525. default:
  4526. /* If this is a -Bsymbolic link, and the symbol is defined
  4527. locally, we just want to emit a RELATIVE reloc. Likewise if
  4528. the symbol was forced to be local because of a version file.
  4529. The entry in the global offset table will already have been
  4530. initialized in the relocate_section function. */
  4531. if (bfd_link_pic (info)
  4532. && (info->symbolic || h->dynindx == -1)
  4533. && h->def_regular)
  4534. {
  4535. rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
  4536. rel.r_addend = (h->root.u.def.value
  4537. + h->root.u.def.section->output_section->vma
  4538. + h->root.u.def.section->output_offset);
  4539. }
  4540. else
  4541. {
  4542. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
  4543. rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
  4544. rel.r_addend = 0;
  4545. }
  4546. }
  4547. if (ELF32_R_TYPE (rel.r_info) != R_MN10300_NONE)
  4548. {
  4549. bfd_elf32_swap_reloca_out (output_bfd, &rel,
  4550. (bfd_byte *) ((Elf32_External_Rela *) srel->contents
  4551. + srel->reloc_count));
  4552. ++ srel->reloc_count;
  4553. }
  4554. }
  4555. if (h->needs_copy)
  4556. {
  4557. asection * s;
  4558. Elf_Internal_Rela rel;
  4559. /* This symbol needs a copy reloc. Set it up. */
  4560. BFD_ASSERT (h->dynindx != -1
  4561. && (h->root.type == bfd_link_hash_defined
  4562. || h->root.type == bfd_link_hash_defweak));
  4563. s = bfd_get_linker_section (dynobj, ".rela.bss");
  4564. BFD_ASSERT (s != NULL);
  4565. rel.r_offset = (h->root.u.def.value
  4566. + h->root.u.def.section->output_section->vma
  4567. + h->root.u.def.section->output_offset);
  4568. rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
  4569. rel.r_addend = 0;
  4570. bfd_elf32_swap_reloca_out (output_bfd, & rel,
  4571. (bfd_byte *) ((Elf32_External_Rela *) s->contents
  4572. + s->reloc_count));
  4573. ++ s->reloc_count;
  4574. }
  4575. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  4576. if (h == elf_hash_table (info)->hdynamic
  4577. || h == elf_hash_table (info)->hgot)
  4578. sym->st_shndx = SHN_ABS;
  4579. return true;
  4580. }
  4581. /* Finish up the dynamic sections. */
  4582. static bool
  4583. _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
  4584. struct bfd_link_info * info)
  4585. {
  4586. bfd * dynobj;
  4587. asection * sgot;
  4588. asection * sdyn;
  4589. struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
  4590. dynobj = htab->root.dynobj;
  4591. sgot = htab->root.sgotplt;
  4592. BFD_ASSERT (sgot != NULL);
  4593. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  4594. if (elf_hash_table (info)->dynamic_sections_created)
  4595. {
  4596. asection * splt;
  4597. Elf32_External_Dyn * dyncon;
  4598. Elf32_External_Dyn * dynconend;
  4599. BFD_ASSERT (sdyn != NULL);
  4600. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  4601. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  4602. for (; dyncon < dynconend; dyncon++)
  4603. {
  4604. Elf_Internal_Dyn dyn;
  4605. asection * s;
  4606. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  4607. switch (dyn.d_tag)
  4608. {
  4609. default:
  4610. break;
  4611. case DT_PLTGOT:
  4612. s = htab->root.sgot;
  4613. goto get_vma;
  4614. case DT_JMPREL:
  4615. s = htab->root.srelplt;
  4616. get_vma:
  4617. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  4618. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4619. break;
  4620. case DT_PLTRELSZ:
  4621. s = htab->root.srelplt;
  4622. dyn.d_un.d_val = s->size;
  4623. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4624. break;
  4625. }
  4626. }
  4627. /* Fill in the first entry in the procedure linkage table. */
  4628. splt = htab->root.splt;
  4629. if (splt && splt->size > 0)
  4630. {
  4631. if (bfd_link_pic (info))
  4632. {
  4633. memcpy (splt->contents, elf_mn10300_pic_plt_entry,
  4634. elf_mn10300_sizeof_plt (info));
  4635. }
  4636. else
  4637. {
  4638. memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
  4639. bfd_put_32 (output_bfd,
  4640. sgot->output_section->vma + sgot->output_offset + 4,
  4641. splt->contents + elf_mn10300_plt0_gotid_offset (info));
  4642. bfd_put_32 (output_bfd,
  4643. sgot->output_section->vma + sgot->output_offset + 8,
  4644. splt->contents + elf_mn10300_plt0_linker_offset (info));
  4645. }
  4646. /* UnixWare sets the entsize of .plt to 4, although that doesn't
  4647. really seem like the right value. */
  4648. elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
  4649. /* UnixWare sets the entsize of .plt to 4, but this is incorrect
  4650. as it means that the size of the PLT0 section (15 bytes) is not
  4651. a multiple of the sh_entsize. Some ELF tools flag this as an
  4652. error. We could pad PLT0 to 16 bytes, but that would introduce
  4653. compatibilty issues with previous toolchains, so instead we
  4654. just set the entry size to 1. */
  4655. elf_section_data (splt->output_section)->this_hdr.sh_entsize = 1;
  4656. }
  4657. }
  4658. /* Fill in the first three entries in the global offset table. */
  4659. if (sgot->size > 0)
  4660. {
  4661. if (sdyn == NULL)
  4662. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
  4663. else
  4664. bfd_put_32 (output_bfd,
  4665. sdyn->output_section->vma + sdyn->output_offset,
  4666. sgot->contents);
  4667. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
  4668. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
  4669. }
  4670. elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  4671. return true;
  4672. }
  4673. /* Classify relocation types, such that combreloc can sort them
  4674. properly. */
  4675. static enum elf_reloc_type_class
  4676. _bfd_mn10300_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  4677. const asection *rel_sec ATTRIBUTE_UNUSED,
  4678. const Elf_Internal_Rela *rela)
  4679. {
  4680. switch ((int) ELF32_R_TYPE (rela->r_info))
  4681. {
  4682. case R_MN10300_RELATIVE: return reloc_class_relative;
  4683. case R_MN10300_JMP_SLOT: return reloc_class_plt;
  4684. case R_MN10300_COPY: return reloc_class_copy;
  4685. default: return reloc_class_normal;
  4686. }
  4687. }
  4688. /* Allocate space for an MN10300 extension to the bfd elf data structure. */
  4689. static bool
  4690. mn10300_elf_mkobject (bfd *abfd)
  4691. {
  4692. return bfd_elf_allocate_object (abfd, sizeof (struct elf_mn10300_obj_tdata),
  4693. MN10300_ELF_DATA);
  4694. }
  4695. #define bfd_elf32_mkobject mn10300_elf_mkobject
  4696. #ifndef ELF_ARCH
  4697. #define TARGET_LITTLE_SYM mn10300_elf32_vec
  4698. #define TARGET_LITTLE_NAME "elf32-mn10300"
  4699. #define ELF_ARCH bfd_arch_mn10300
  4700. #define ELF_TARGET_ID MN10300_ELF_DATA
  4701. #define ELF_MACHINE_CODE EM_MN10300
  4702. #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
  4703. #define ELF_MAXPAGESIZE 0x1000
  4704. #endif
  4705. #define elf_info_to_howto mn10300_info_to_howto
  4706. #define elf_info_to_howto_rel NULL
  4707. #define elf_backend_can_gc_sections 1
  4708. #define elf_backend_rela_normal 1
  4709. #define elf_backend_check_relocs mn10300_elf_check_relocs
  4710. #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
  4711. #define elf_backend_relocate_section mn10300_elf_relocate_section
  4712. #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
  4713. #define bfd_elf32_bfd_get_relocated_section_contents \
  4714. mn10300_elf_get_relocated_section_contents
  4715. #define bfd_elf32_bfd_link_hash_table_create \
  4716. elf32_mn10300_link_hash_table_create
  4717. #ifndef elf_symbol_leading_char
  4718. #define elf_symbol_leading_char '_'
  4719. #endif
  4720. /* So we can set bits in e_flags. */
  4721. #define elf_backend_final_write_processing \
  4722. _bfd_mn10300_elf_final_write_processing
  4723. #define elf_backend_object_p _bfd_mn10300_elf_object_p
  4724. #define bfd_elf32_bfd_merge_private_bfd_data \
  4725. _bfd_mn10300_elf_merge_private_bfd_data
  4726. #define elf_backend_can_gc_sections 1
  4727. #define elf_backend_create_dynamic_sections \
  4728. _bfd_mn10300_elf_create_dynamic_sections
  4729. #define elf_backend_adjust_dynamic_symbol \
  4730. _bfd_mn10300_elf_adjust_dynamic_symbol
  4731. #define elf_backend_size_dynamic_sections \
  4732. _bfd_mn10300_elf_size_dynamic_sections
  4733. #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
  4734. #define elf_backend_finish_dynamic_symbol \
  4735. _bfd_mn10300_elf_finish_dynamic_symbol
  4736. #define elf_backend_finish_dynamic_sections \
  4737. _bfd_mn10300_elf_finish_dynamic_sections
  4738. #define elf_backend_copy_indirect_symbol \
  4739. _bfd_mn10300_copy_indirect_symbol
  4740. #define elf_backend_reloc_type_class \
  4741. _bfd_mn10300_elf_reloc_type_class
  4742. #define elf_backend_want_got_plt 1
  4743. #define elf_backend_plt_readonly 1
  4744. #define elf_backend_want_plt_sym 0
  4745. #define elf_backend_got_header_size 12
  4746. #define elf_backend_dtrel_excludes_plt 1
  4747. #include "elf32-target.h"