elf32-i386.c 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817
  1. /* Intel 80386/80486-specific support for 32-bit ELF
  2. Copyright (C) 1993-2015 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 "bfdlink.h"
  19. #include "libbfd.h"
  20. #include "elf-bfd.h"
  21. #include "elf-nacl.h"
  22. #include "elf-vxworks.h"
  23. #include "bfd_stdint.h"
  24. #include "objalloc.h"
  25. #include "hashtab.h"
  26. #include "dwarf2.h"
  27. /* 386 uses REL relocations instead of RELA. */
  28. #define USE_REL 1
  29. #include "elf/i386.h"
  30. static reloc_howto_type elf_howto_table[]=
  31. {
  32. HOWTO(R_386_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
  33. bfd_elf_generic_reloc, "R_386_NONE",
  34. TRUE, 0x00000000, 0x00000000, FALSE),
  35. HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  36. bfd_elf_generic_reloc, "R_386_32",
  37. TRUE, 0xffffffff, 0xffffffff, FALSE),
  38. HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
  39. bfd_elf_generic_reloc, "R_386_PC32",
  40. TRUE, 0xffffffff, 0xffffffff, TRUE),
  41. HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  42. bfd_elf_generic_reloc, "R_386_GOT32",
  43. TRUE, 0xffffffff, 0xffffffff, FALSE),
  44. HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
  45. bfd_elf_generic_reloc, "R_386_PLT32",
  46. TRUE, 0xffffffff, 0xffffffff, TRUE),
  47. HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  48. bfd_elf_generic_reloc, "R_386_COPY",
  49. TRUE, 0xffffffff, 0xffffffff, FALSE),
  50. HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  51. bfd_elf_generic_reloc, "R_386_GLOB_DAT",
  52. TRUE, 0xffffffff, 0xffffffff, FALSE),
  53. HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  54. bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
  55. TRUE, 0xffffffff, 0xffffffff, FALSE),
  56. HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  57. bfd_elf_generic_reloc, "R_386_RELATIVE",
  58. TRUE, 0xffffffff, 0xffffffff, FALSE),
  59. HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  60. bfd_elf_generic_reloc, "R_386_GOTOFF",
  61. TRUE, 0xffffffff, 0xffffffff, FALSE),
  62. HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
  63. bfd_elf_generic_reloc, "R_386_GOTPC",
  64. TRUE, 0xffffffff, 0xffffffff, TRUE),
  65. /* We have a gap in the reloc numbers here.
  66. R_386_standard counts the number up to this point, and
  67. R_386_ext_offset is the value to subtract from a reloc type of
  68. R_386_16 thru R_386_PC8 to form an index into this table. */
  69. #define R_386_standard (R_386_GOTPC + 1)
  70. #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
  71. /* These relocs are a GNU extension. */
  72. HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  73. bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
  74. TRUE, 0xffffffff, 0xffffffff, FALSE),
  75. HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  76. bfd_elf_generic_reloc, "R_386_TLS_IE",
  77. TRUE, 0xffffffff, 0xffffffff, FALSE),
  78. HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  79. bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
  80. TRUE, 0xffffffff, 0xffffffff, FALSE),
  81. HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  82. bfd_elf_generic_reloc, "R_386_TLS_LE",
  83. TRUE, 0xffffffff, 0xffffffff, FALSE),
  84. HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  85. bfd_elf_generic_reloc, "R_386_TLS_GD",
  86. TRUE, 0xffffffff, 0xffffffff, FALSE),
  87. HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  88. bfd_elf_generic_reloc, "R_386_TLS_LDM",
  89. TRUE, 0xffffffff, 0xffffffff, FALSE),
  90. HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
  91. bfd_elf_generic_reloc, "R_386_16",
  92. TRUE, 0xffff, 0xffff, FALSE),
  93. HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
  94. bfd_elf_generic_reloc, "R_386_PC16",
  95. TRUE, 0xffff, 0xffff, TRUE),
  96. HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
  97. bfd_elf_generic_reloc, "R_386_8",
  98. TRUE, 0xff, 0xff, FALSE),
  99. HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
  100. bfd_elf_generic_reloc, "R_386_PC8",
  101. TRUE, 0xff, 0xff, TRUE),
  102. #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
  103. #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
  104. /* These are common with Solaris TLS implementation. */
  105. HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  106. bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
  107. TRUE, 0xffffffff, 0xffffffff, FALSE),
  108. HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  109. bfd_elf_generic_reloc, "R_386_TLS_IE_32",
  110. TRUE, 0xffffffff, 0xffffffff, FALSE),
  111. HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  112. bfd_elf_generic_reloc, "R_386_TLS_LE_32",
  113. TRUE, 0xffffffff, 0xffffffff, FALSE),
  114. HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  115. bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
  116. TRUE, 0xffffffff, 0xffffffff, FALSE),
  117. HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  118. bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
  119. TRUE, 0xffffffff, 0xffffffff, FALSE),
  120. HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  121. bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
  122. TRUE, 0xffffffff, 0xffffffff, FALSE),
  123. HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
  124. bfd_elf_generic_reloc, "R_386_SIZE32",
  125. TRUE, 0xffffffff, 0xffffffff, FALSE),
  126. HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  127. bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
  128. TRUE, 0xffffffff, 0xffffffff, FALSE),
  129. HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
  130. bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
  131. FALSE, 0, 0, FALSE),
  132. HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  133. bfd_elf_generic_reloc, "R_386_TLS_DESC",
  134. TRUE, 0xffffffff, 0xffffffff, FALSE),
  135. HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  136. bfd_elf_generic_reloc, "R_386_IRELATIVE",
  137. TRUE, 0xffffffff, 0xffffffff, FALSE),
  138. /* Another gap. */
  139. #define R_386_ext2 (R_386_IRELATIVE + 1 - R_386_tls_offset)
  140. #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
  141. /* GNU extension to record C++ vtable hierarchy. */
  142. HOWTO (R_386_GNU_VTINHERIT, /* type */
  143. 0, /* rightshift */
  144. 2, /* size (0 = byte, 1 = short, 2 = long) */
  145. 0, /* bitsize */
  146. FALSE, /* pc_relative */
  147. 0, /* bitpos */
  148. complain_overflow_dont, /* complain_on_overflow */
  149. NULL, /* special_function */
  150. "R_386_GNU_VTINHERIT", /* name */
  151. FALSE, /* partial_inplace */
  152. 0, /* src_mask */
  153. 0, /* dst_mask */
  154. FALSE), /* pcrel_offset */
  155. /* GNU extension to record C++ vtable member usage. */
  156. HOWTO (R_386_GNU_VTENTRY, /* type */
  157. 0, /* rightshift */
  158. 2, /* size (0 = byte, 1 = short, 2 = long) */
  159. 0, /* bitsize */
  160. FALSE, /* pc_relative */
  161. 0, /* bitpos */
  162. complain_overflow_dont, /* complain_on_overflow */
  163. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  164. "R_386_GNU_VTENTRY", /* name */
  165. FALSE, /* partial_inplace */
  166. 0, /* src_mask */
  167. 0, /* dst_mask */
  168. FALSE) /* pcrel_offset */
  169. #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
  170. };
  171. #ifdef DEBUG_GEN_RELOC
  172. #define TRACE(str) \
  173. fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
  174. #else
  175. #define TRACE(str)
  176. #endif
  177. static reloc_howto_type *
  178. elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  179. bfd_reloc_code_real_type code)
  180. {
  181. switch (code)
  182. {
  183. case BFD_RELOC_NONE:
  184. TRACE ("BFD_RELOC_NONE");
  185. return &elf_howto_table[R_386_NONE];
  186. case BFD_RELOC_32:
  187. TRACE ("BFD_RELOC_32");
  188. return &elf_howto_table[R_386_32];
  189. case BFD_RELOC_CTOR:
  190. TRACE ("BFD_RELOC_CTOR");
  191. return &elf_howto_table[R_386_32];
  192. case BFD_RELOC_32_PCREL:
  193. TRACE ("BFD_RELOC_PC32");
  194. return &elf_howto_table[R_386_PC32];
  195. case BFD_RELOC_386_GOT32:
  196. TRACE ("BFD_RELOC_386_GOT32");
  197. return &elf_howto_table[R_386_GOT32];
  198. case BFD_RELOC_386_PLT32:
  199. TRACE ("BFD_RELOC_386_PLT32");
  200. return &elf_howto_table[R_386_PLT32];
  201. case BFD_RELOC_386_COPY:
  202. TRACE ("BFD_RELOC_386_COPY");
  203. return &elf_howto_table[R_386_COPY];
  204. case BFD_RELOC_386_GLOB_DAT:
  205. TRACE ("BFD_RELOC_386_GLOB_DAT");
  206. return &elf_howto_table[R_386_GLOB_DAT];
  207. case BFD_RELOC_386_JUMP_SLOT:
  208. TRACE ("BFD_RELOC_386_JUMP_SLOT");
  209. return &elf_howto_table[R_386_JUMP_SLOT];
  210. case BFD_RELOC_386_RELATIVE:
  211. TRACE ("BFD_RELOC_386_RELATIVE");
  212. return &elf_howto_table[R_386_RELATIVE];
  213. case BFD_RELOC_386_GOTOFF:
  214. TRACE ("BFD_RELOC_386_GOTOFF");
  215. return &elf_howto_table[R_386_GOTOFF];
  216. case BFD_RELOC_386_GOTPC:
  217. TRACE ("BFD_RELOC_386_GOTPC");
  218. return &elf_howto_table[R_386_GOTPC];
  219. /* These relocs are a GNU extension. */
  220. case BFD_RELOC_386_TLS_TPOFF:
  221. TRACE ("BFD_RELOC_386_TLS_TPOFF");
  222. return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
  223. case BFD_RELOC_386_TLS_IE:
  224. TRACE ("BFD_RELOC_386_TLS_IE");
  225. return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
  226. case BFD_RELOC_386_TLS_GOTIE:
  227. TRACE ("BFD_RELOC_386_TLS_GOTIE");
  228. return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
  229. case BFD_RELOC_386_TLS_LE:
  230. TRACE ("BFD_RELOC_386_TLS_LE");
  231. return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
  232. case BFD_RELOC_386_TLS_GD:
  233. TRACE ("BFD_RELOC_386_TLS_GD");
  234. return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
  235. case BFD_RELOC_386_TLS_LDM:
  236. TRACE ("BFD_RELOC_386_TLS_LDM");
  237. return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
  238. case BFD_RELOC_16:
  239. TRACE ("BFD_RELOC_16");
  240. return &elf_howto_table[R_386_16 - R_386_ext_offset];
  241. case BFD_RELOC_16_PCREL:
  242. TRACE ("BFD_RELOC_16_PCREL");
  243. return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
  244. case BFD_RELOC_8:
  245. TRACE ("BFD_RELOC_8");
  246. return &elf_howto_table[R_386_8 - R_386_ext_offset];
  247. case BFD_RELOC_8_PCREL:
  248. TRACE ("BFD_RELOC_8_PCREL");
  249. return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
  250. /* Common with Sun TLS implementation. */
  251. case BFD_RELOC_386_TLS_LDO_32:
  252. TRACE ("BFD_RELOC_386_TLS_LDO_32");
  253. return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
  254. case BFD_RELOC_386_TLS_IE_32:
  255. TRACE ("BFD_RELOC_386_TLS_IE_32");
  256. return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
  257. case BFD_RELOC_386_TLS_LE_32:
  258. TRACE ("BFD_RELOC_386_TLS_LE_32");
  259. return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
  260. case BFD_RELOC_386_TLS_DTPMOD32:
  261. TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
  262. return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
  263. case BFD_RELOC_386_TLS_DTPOFF32:
  264. TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
  265. return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
  266. case BFD_RELOC_386_TLS_TPOFF32:
  267. TRACE ("BFD_RELOC_386_TLS_TPOFF32");
  268. return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
  269. case BFD_RELOC_SIZE32:
  270. TRACE ("BFD_RELOC_SIZE32");
  271. return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
  272. case BFD_RELOC_386_TLS_GOTDESC:
  273. TRACE ("BFD_RELOC_386_TLS_GOTDESC");
  274. return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
  275. case BFD_RELOC_386_TLS_DESC_CALL:
  276. TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
  277. return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
  278. case BFD_RELOC_386_TLS_DESC:
  279. TRACE ("BFD_RELOC_386_TLS_DESC");
  280. return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
  281. case BFD_RELOC_386_IRELATIVE:
  282. TRACE ("BFD_RELOC_386_IRELATIVE");
  283. return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
  284. case BFD_RELOC_VTABLE_INHERIT:
  285. TRACE ("BFD_RELOC_VTABLE_INHERIT");
  286. return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
  287. case BFD_RELOC_VTABLE_ENTRY:
  288. TRACE ("BFD_RELOC_VTABLE_ENTRY");
  289. return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
  290. default:
  291. break;
  292. }
  293. TRACE ("Unknown");
  294. return 0;
  295. }
  296. static reloc_howto_type *
  297. elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  298. const char *r_name)
  299. {
  300. unsigned int i;
  301. for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
  302. if (elf_howto_table[i].name != NULL
  303. && strcasecmp (elf_howto_table[i].name, r_name) == 0)
  304. return &elf_howto_table[i];
  305. return NULL;
  306. }
  307. static reloc_howto_type *
  308. elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
  309. {
  310. unsigned int indx;
  311. if ((indx = r_type) >= R_386_standard
  312. && ((indx = r_type - R_386_ext_offset) - R_386_standard
  313. >= R_386_ext - R_386_standard)
  314. && ((indx = r_type - R_386_tls_offset) - R_386_ext
  315. >= R_386_ext2 - R_386_ext)
  316. && ((indx = r_type - R_386_vt_offset) - R_386_ext2
  317. >= R_386_vt - R_386_ext2))
  318. {
  319. (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
  320. abfd, (int) r_type);
  321. indx = R_386_NONE;
  322. }
  323. /* PR 17512: file: 0f67f69d. */
  324. if (elf_howto_table [indx].type != r_type)
  325. return NULL;
  326. return &elf_howto_table[indx];
  327. }
  328. static void
  329. elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
  330. arelent *cache_ptr,
  331. Elf_Internal_Rela *dst)
  332. {
  333. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  334. cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
  335. }
  336. /* Return whether a symbol name implies a local label. The UnixWare
  337. 2.1 cc generates temporary symbols that start with .X, so we
  338. recognize them here. FIXME: do other SVR4 compilers also use .X?.
  339. If so, we should move the .X recognition into
  340. _bfd_elf_is_local_label_name. */
  341. static bfd_boolean
  342. elf_i386_is_local_label_name (bfd *abfd, const char *name)
  343. {
  344. if (name[0] == '.' && name[1] == 'X')
  345. return TRUE;
  346. return _bfd_elf_is_local_label_name (abfd, name);
  347. }
  348. /* Support for core dump NOTE sections. */
  349. static bfd_boolean
  350. elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  351. {
  352. int offset;
  353. size_t size;
  354. if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
  355. {
  356. int pr_version = bfd_get_32 (abfd, note->descdata);
  357. if (pr_version != 1)
  358. return FALSE;
  359. /* pr_cursig */
  360. elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
  361. /* pr_pid */
  362. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  363. /* pr_reg */
  364. offset = 28;
  365. size = bfd_get_32 (abfd, note->descdata + 8);
  366. }
  367. else
  368. {
  369. switch (note->descsz)
  370. {
  371. default:
  372. return FALSE;
  373. case 144: /* Linux/i386 */
  374. /* pr_cursig */
  375. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  376. /* pr_pid */
  377. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  378. /* pr_reg */
  379. offset = 72;
  380. size = 68;
  381. break;
  382. }
  383. }
  384. /* Make a ".reg/999" section. */
  385. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  386. size, note->descpos + offset);
  387. }
  388. static bfd_boolean
  389. elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  390. {
  391. if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
  392. {
  393. int pr_version = bfd_get_32 (abfd, note->descdata);
  394. if (pr_version != 1)
  395. return FALSE;
  396. elf_tdata (abfd)->core->program
  397. = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
  398. elf_tdata (abfd)->core->command
  399. = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
  400. }
  401. else
  402. {
  403. switch (note->descsz)
  404. {
  405. default:
  406. return FALSE;
  407. case 124: /* Linux/i386 elf_prpsinfo. */
  408. elf_tdata (abfd)->core->pid
  409. = bfd_get_32 (abfd, note->descdata + 12);
  410. elf_tdata (abfd)->core->program
  411. = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  412. elf_tdata (abfd)->core->command
  413. = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  414. }
  415. }
  416. /* Note that for some reason, a spurious space is tacked
  417. onto the end of the args in some (at least one anyway)
  418. implementations, so strip it off if it exists. */
  419. {
  420. char *command = elf_tdata (abfd)->core->command;
  421. int n = strlen (command);
  422. if (0 < n && command[n - 1] == ' ')
  423. command[n - 1] = '\0';
  424. }
  425. return TRUE;
  426. }
  427. /* Functions for the i386 ELF linker.
  428. In order to gain some understanding of code in this file without
  429. knowing all the intricate details of the linker, note the
  430. following:
  431. Functions named elf_i386_* are called by external routines, other
  432. functions are only called locally. elf_i386_* functions appear
  433. in this file more or less in the order in which they are called
  434. from external routines. eg. elf_i386_check_relocs is called
  435. early in the link process, elf_i386_finish_dynamic_sections is
  436. one of the last functions. */
  437. /* The name of the dynamic interpreter. This is put in the .interp
  438. section. */
  439. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  440. /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
  441. copying dynamic variables from a shared lib into an app's dynbss
  442. section, and instead use a dynamic relocation to point into the
  443. shared lib. */
  444. #define ELIMINATE_COPY_RELOCS 1
  445. /* The size in bytes of an entry in the procedure linkage table. */
  446. #define PLT_ENTRY_SIZE 16
  447. /* The first entry in an absolute procedure linkage table looks like
  448. this. See the SVR4 ABI i386 supplement to see how this works.
  449. Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
  450. static const bfd_byte elf_i386_plt0_entry[12] =
  451. {
  452. 0xff, 0x35, /* pushl contents of address */
  453. 0, 0, 0, 0, /* replaced with address of .got + 4. */
  454. 0xff, 0x25, /* jmp indirect */
  455. 0, 0, 0, 0 /* replaced with address of .got + 8. */
  456. };
  457. /* Subsequent entries in an absolute procedure linkage table look like
  458. this. */
  459. static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
  460. {
  461. 0xff, 0x25, /* jmp indirect */
  462. 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
  463. 0x68, /* pushl immediate */
  464. 0, 0, 0, 0, /* replaced with offset into relocation table. */
  465. 0xe9, /* jmp relative */
  466. 0, 0, 0, 0 /* replaced with offset to start of .plt. */
  467. };
  468. /* The first entry in a PIC procedure linkage table look like this.
  469. Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
  470. static const bfd_byte elf_i386_pic_plt0_entry[12] =
  471. {
  472. 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
  473. 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
  474. };
  475. /* Subsequent entries in a PIC procedure linkage table look like this. */
  476. static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
  477. {
  478. 0xff, 0xa3, /* jmp *offset(%ebx) */
  479. 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
  480. 0x68, /* pushl immediate */
  481. 0, 0, 0, 0, /* replaced with offset into relocation table. */
  482. 0xe9, /* jmp relative */
  483. 0, 0, 0, 0 /* replaced with offset to start of .plt. */
  484. };
  485. /* Entries in the GOT procedure linkage table look like this. */
  486. static const bfd_byte elf_i386_got_plt_entry[8] =
  487. {
  488. 0xff, 0x25, /* jmp indirect */
  489. 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
  490. 0x66, 0x90 /* xchg %ax,%ax */
  491. };
  492. /* Entries in the PIC GOT procedure linkage table look like this. */
  493. static const bfd_byte elf_i386_pic_got_plt_entry[8] =
  494. {
  495. 0xff, 0xa3, /* jmp *offset(%ebx) */
  496. 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
  497. 0x66, 0x90 /* xchg %ax,%ax */
  498. };
  499. /* .eh_frame covering the .plt section. */
  500. static const bfd_byte elf_i386_eh_frame_plt[] =
  501. {
  502. #define PLT_CIE_LENGTH 20
  503. #define PLT_FDE_LENGTH 36
  504. #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
  505. #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
  506. PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
  507. 0, 0, 0, 0, /* CIE ID */
  508. 1, /* CIE version */
  509. 'z', 'R', 0, /* Augmentation string */
  510. 1, /* Code alignment factor */
  511. 0x7c, /* Data alignment factor */
  512. 8, /* Return address column */
  513. 1, /* Augmentation size */
  514. DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
  515. DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
  516. DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
  517. DW_CFA_nop, DW_CFA_nop,
  518. PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
  519. PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
  520. 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
  521. 0, 0, 0, 0, /* .plt size goes here */
  522. 0, /* Augmentation size */
  523. DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
  524. DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
  525. DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
  526. DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
  527. DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
  528. 11, /* Block length */
  529. DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
  530. DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
  531. DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
  532. DW_OP_lit2, DW_OP_shl, DW_OP_plus,
  533. DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
  534. };
  535. struct elf_i386_plt_layout
  536. {
  537. /* The first entry in an absolute procedure linkage table looks like this. */
  538. const bfd_byte *plt0_entry;
  539. unsigned int plt0_entry_size;
  540. /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
  541. unsigned int plt0_got1_offset;
  542. unsigned int plt0_got2_offset;
  543. /* Later entries in an absolute procedure linkage table look like this. */
  544. const bfd_byte *plt_entry;
  545. unsigned int plt_entry_size;
  546. /* Offsets into plt_entry that are to be replaced with... */
  547. unsigned int plt_got_offset; /* ... address of this symbol in .got. */
  548. unsigned int plt_reloc_offset; /* ... offset into relocation table. */
  549. unsigned int plt_plt_offset; /* ... offset to start of .plt. */
  550. /* Offset into plt_entry where the initial value of the GOT entry points. */
  551. unsigned int plt_lazy_offset;
  552. /* The first entry in a PIC procedure linkage table looks like this. */
  553. const bfd_byte *pic_plt0_entry;
  554. /* Subsequent entries in a PIC procedure linkage table look like this. */
  555. const bfd_byte *pic_plt_entry;
  556. /* .eh_frame covering the .plt section. */
  557. const bfd_byte *eh_frame_plt;
  558. unsigned int eh_frame_plt_size;
  559. };
  560. #define GET_PLT_ENTRY_SIZE(abfd) \
  561. get_elf_i386_backend_data (abfd)->plt->plt_entry_size
  562. /* These are the standard parameters. */
  563. static const struct elf_i386_plt_layout elf_i386_plt =
  564. {
  565. elf_i386_plt0_entry, /* plt0_entry */
  566. sizeof (elf_i386_plt0_entry), /* plt0_entry_size */
  567. 2, /* plt0_got1_offset */
  568. 8, /* plt0_got2_offset */
  569. elf_i386_plt_entry, /* plt_entry */
  570. PLT_ENTRY_SIZE, /* plt_entry_size */
  571. 2, /* plt_got_offset */
  572. 7, /* plt_reloc_offset */
  573. 12, /* plt_plt_offset */
  574. 6, /* plt_lazy_offset */
  575. elf_i386_pic_plt0_entry, /* pic_plt0_entry */
  576. elf_i386_pic_plt_entry, /* pic_plt_entry */
  577. elf_i386_eh_frame_plt, /* eh_frame_plt */
  578. sizeof (elf_i386_eh_frame_plt), /* eh_frame_plt_size */
  579. };
  580. /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
  581. for the PLTResolve stub and then for each PLT entry. */
  582. #define PLTRESOLVE_RELOCS_SHLIB 0
  583. #define PLTRESOLVE_RELOCS 2
  584. #define PLT_NON_JUMP_SLOT_RELOCS 2
  585. /* Architecture-specific backend data for i386. */
  586. struct elf_i386_backend_data
  587. {
  588. /* Parameters describing PLT generation. */
  589. const struct elf_i386_plt_layout *plt;
  590. /* Value used to fill the unused bytes of the first PLT entry. */
  591. bfd_byte plt0_pad_byte;
  592. /* True if the target system is VxWorks. */
  593. int is_vxworks;
  594. };
  595. #define get_elf_i386_backend_data(abfd) \
  596. ((const struct elf_i386_backend_data *) \
  597. get_elf_backend_data (abfd)->arch_data)
  598. /* These are the standard parameters. */
  599. static const struct elf_i386_backend_data elf_i386_arch_bed =
  600. {
  601. &elf_i386_plt, /* plt */
  602. 0, /* plt0_pad_byte */
  603. 0, /* is_vxworks */
  604. };
  605. #define elf_backend_arch_data &elf_i386_arch_bed
  606. /* i386 ELF linker hash entry. */
  607. struct elf_i386_link_hash_entry
  608. {
  609. struct elf_link_hash_entry elf;
  610. /* Track dynamic relocs copied for this symbol. */
  611. struct elf_dyn_relocs *dyn_relocs;
  612. #define GOT_UNKNOWN 0
  613. #define GOT_NORMAL 1
  614. #define GOT_TLS_GD 2
  615. #define GOT_TLS_IE 4
  616. #define GOT_TLS_IE_POS 5
  617. #define GOT_TLS_IE_NEG 6
  618. #define GOT_TLS_IE_BOTH 7
  619. #define GOT_TLS_GDESC 8
  620. #define GOT_TLS_GD_BOTH_P(type) \
  621. ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
  622. #define GOT_TLS_GD_P(type) \
  623. ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
  624. #define GOT_TLS_GDESC_P(type) \
  625. ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
  626. #define GOT_TLS_GD_ANY_P(type) \
  627. (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
  628. unsigned char tls_type;
  629. /* Symbol is referenced by R_386_GOTOFF relocation. */
  630. unsigned int gotoff_ref : 1;
  631. /* Reference count of C/C++ function pointer relocations in read-write
  632. section which can be resolved at run-time. */
  633. bfd_signed_vma func_pointer_refcount;
  634. /* Information about the GOT PLT entry. Filled when there are both
  635. GOT and PLT relocations against the same function. */
  636. union gotplt_union plt_got;
  637. /* Offset of the GOTPLT entry reserved for the TLS descriptor,
  638. starting at the end of the jump table. */
  639. bfd_vma tlsdesc_got;
  640. };
  641. #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
  642. struct elf_i386_obj_tdata
  643. {
  644. struct elf_obj_tdata root;
  645. /* tls_type for each local got entry. */
  646. char *local_got_tls_type;
  647. /* GOTPLT entries for TLS descriptors. */
  648. bfd_vma *local_tlsdesc_gotent;
  649. };
  650. #define elf_i386_tdata(abfd) \
  651. ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
  652. #define elf_i386_local_got_tls_type(abfd) \
  653. (elf_i386_tdata (abfd)->local_got_tls_type)
  654. #define elf_i386_local_tlsdesc_gotent(abfd) \
  655. (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
  656. #define is_i386_elf(bfd) \
  657. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  658. && elf_tdata (bfd) != NULL \
  659. && elf_object_id (bfd) == I386_ELF_DATA)
  660. static bfd_boolean
  661. elf_i386_mkobject (bfd *abfd)
  662. {
  663. return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
  664. I386_ELF_DATA);
  665. }
  666. /* i386 ELF linker hash table. */
  667. struct elf_i386_link_hash_table
  668. {
  669. struct elf_link_hash_table elf;
  670. /* Short-cuts to get to dynamic linker sections. */
  671. asection *sdynbss;
  672. asection *srelbss;
  673. asection *plt_eh_frame;
  674. asection *plt_got;
  675. union
  676. {
  677. bfd_signed_vma refcount;
  678. bfd_vma offset;
  679. } tls_ldm_got;
  680. /* The amount of space used by the reserved portion of the sgotplt
  681. section, plus whatever space is used by the jump slots. */
  682. bfd_vma sgotplt_jump_table_size;
  683. /* Small local sym cache. */
  684. struct sym_cache sym_cache;
  685. /* _TLS_MODULE_BASE_ symbol. */
  686. struct bfd_link_hash_entry *tls_module_base;
  687. /* Used by local STT_GNU_IFUNC symbols. */
  688. htab_t loc_hash_table;
  689. void * loc_hash_memory;
  690. /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
  691. asection *srelplt2;
  692. /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
  693. bfd_vma next_tls_desc_index;
  694. /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt. */
  695. bfd_vma next_jump_slot_index;
  696. /* The index of the next unused R_386_IRELATIVE slot in .rel.plt. */
  697. bfd_vma next_irelative_index;
  698. };
  699. /* Get the i386 ELF linker hash table from a link_info structure. */
  700. #define elf_i386_hash_table(p) \
  701. (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
  702. == I386_ELF_DATA ? ((struct elf_i386_link_hash_table *) ((p)->hash)) : NULL)
  703. #define elf_i386_compute_jump_table_size(htab) \
  704. ((htab)->elf.srelplt->reloc_count * 4)
  705. /* Create an entry in an i386 ELF linker hash table. */
  706. static struct bfd_hash_entry *
  707. elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
  708. struct bfd_hash_table *table,
  709. const char *string)
  710. {
  711. /* Allocate the structure if it has not already been allocated by a
  712. subclass. */
  713. if (entry == NULL)
  714. {
  715. entry = (struct bfd_hash_entry *)
  716. bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry));
  717. if (entry == NULL)
  718. return entry;
  719. }
  720. /* Call the allocation method of the superclass. */
  721. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  722. if (entry != NULL)
  723. {
  724. struct elf_i386_link_hash_entry *eh;
  725. eh = (struct elf_i386_link_hash_entry *) entry;
  726. eh->dyn_relocs = NULL;
  727. eh->tls_type = GOT_UNKNOWN;
  728. eh->gotoff_ref = 0;
  729. eh->func_pointer_refcount = 0;
  730. eh->plt_got.offset = (bfd_vma) -1;
  731. eh->tlsdesc_got = (bfd_vma) -1;
  732. }
  733. return entry;
  734. }
  735. /* Compute a hash of a local hash entry. We use elf_link_hash_entry
  736. for local symbol so that we can handle local STT_GNU_IFUNC symbols
  737. as global symbol. We reuse indx and dynstr_index for local symbol
  738. hash since they aren't used by global symbols in this backend. */
  739. static hashval_t
  740. elf_i386_local_htab_hash (const void *ptr)
  741. {
  742. struct elf_link_hash_entry *h
  743. = (struct elf_link_hash_entry *) ptr;
  744. return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
  745. }
  746. /* Compare local hash entries. */
  747. static int
  748. elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
  749. {
  750. struct elf_link_hash_entry *h1
  751. = (struct elf_link_hash_entry *) ptr1;
  752. struct elf_link_hash_entry *h2
  753. = (struct elf_link_hash_entry *) ptr2;
  754. return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
  755. }
  756. /* Find and/or create a hash entry for local symbol. */
  757. static struct elf_link_hash_entry *
  758. elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
  759. bfd *abfd, const Elf_Internal_Rela *rel,
  760. bfd_boolean create)
  761. {
  762. struct elf_i386_link_hash_entry e, *ret;
  763. asection *sec = abfd->sections;
  764. hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
  765. ELF32_R_SYM (rel->r_info));
  766. void **slot;
  767. e.elf.indx = sec->id;
  768. e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
  769. slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
  770. create ? INSERT : NO_INSERT);
  771. if (!slot)
  772. return NULL;
  773. if (*slot)
  774. {
  775. ret = (struct elf_i386_link_hash_entry *) *slot;
  776. return &ret->elf;
  777. }
  778. ret = (struct elf_i386_link_hash_entry *)
  779. objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
  780. sizeof (struct elf_i386_link_hash_entry));
  781. if (ret)
  782. {
  783. memset (ret, 0, sizeof (*ret));
  784. ret->elf.indx = sec->id;
  785. ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
  786. ret->elf.dynindx = -1;
  787. ret->func_pointer_refcount = 0;
  788. ret->plt_got.offset = (bfd_vma) -1;
  789. *slot = ret;
  790. }
  791. return &ret->elf;
  792. }
  793. /* Destroy an i386 ELF linker hash table. */
  794. static void
  795. elf_i386_link_hash_table_free (bfd *obfd)
  796. {
  797. struct elf_i386_link_hash_table *htab
  798. = (struct elf_i386_link_hash_table *) obfd->link.hash;
  799. if (htab->loc_hash_table)
  800. htab_delete (htab->loc_hash_table);
  801. if (htab->loc_hash_memory)
  802. objalloc_free ((struct objalloc *) htab->loc_hash_memory);
  803. _bfd_elf_link_hash_table_free (obfd);
  804. }
  805. /* Create an i386 ELF linker hash table. */
  806. static struct bfd_link_hash_table *
  807. elf_i386_link_hash_table_create (bfd *abfd)
  808. {
  809. struct elf_i386_link_hash_table *ret;
  810. bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
  811. ret = (struct elf_i386_link_hash_table *) bfd_zmalloc (amt);
  812. if (ret == NULL)
  813. return NULL;
  814. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
  815. elf_i386_link_hash_newfunc,
  816. sizeof (struct elf_i386_link_hash_entry),
  817. I386_ELF_DATA))
  818. {
  819. free (ret);
  820. return NULL;
  821. }
  822. ret->loc_hash_table = htab_try_create (1024,
  823. elf_i386_local_htab_hash,
  824. elf_i386_local_htab_eq,
  825. NULL);
  826. ret->loc_hash_memory = objalloc_create ();
  827. if (!ret->loc_hash_table || !ret->loc_hash_memory)
  828. {
  829. elf_i386_link_hash_table_free (abfd);
  830. return NULL;
  831. }
  832. ret->elf.root.hash_table_free = elf_i386_link_hash_table_free;
  833. return &ret->elf.root;
  834. }
  835. /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
  836. .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
  837. hash table. */
  838. static bfd_boolean
  839. elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
  840. {
  841. struct elf_i386_link_hash_table *htab;
  842. if (!_bfd_elf_create_dynamic_sections (dynobj, info))
  843. return FALSE;
  844. htab = elf_i386_hash_table (info);
  845. if (htab == NULL)
  846. return FALSE;
  847. htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
  848. if (!htab->sdynbss)
  849. abort ();
  850. if (bfd_link_executable (info))
  851. {
  852. /* Always allow copy relocs for building executables. */
  853. asection *s = bfd_get_linker_section (dynobj, ".rel.bss");
  854. if (s == NULL)
  855. {
  856. const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
  857. s = bfd_make_section_anyway_with_flags (dynobj,
  858. ".rel.bss",
  859. (bed->dynamic_sec_flags
  860. | SEC_READONLY));
  861. if (s == NULL
  862. || ! bfd_set_section_alignment (dynobj, s,
  863. bed->s->log_file_align))
  864. return FALSE;
  865. }
  866. htab->srelbss = s;
  867. }
  868. if (get_elf_i386_backend_data (dynobj)->is_vxworks
  869. && !elf_vxworks_create_dynamic_sections (dynobj, info,
  870. &htab->srelplt2))
  871. return FALSE;
  872. if (!info->no_ld_generated_unwind_info
  873. && htab->plt_eh_frame == NULL
  874. && htab->elf.splt != NULL)
  875. {
  876. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
  877. | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  878. | SEC_LINKER_CREATED);
  879. htab->plt_eh_frame
  880. = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
  881. if (htab->plt_eh_frame == NULL
  882. || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 2))
  883. return FALSE;
  884. }
  885. return TRUE;
  886. }
  887. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  888. static void
  889. elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
  890. struct elf_link_hash_entry *dir,
  891. struct elf_link_hash_entry *ind)
  892. {
  893. struct elf_i386_link_hash_entry *edir, *eind;
  894. edir = (struct elf_i386_link_hash_entry *) dir;
  895. eind = (struct elf_i386_link_hash_entry *) ind;
  896. if (eind->dyn_relocs != NULL)
  897. {
  898. if (edir->dyn_relocs != NULL)
  899. {
  900. struct elf_dyn_relocs **pp;
  901. struct elf_dyn_relocs *p;
  902. /* Add reloc counts against the indirect sym to the direct sym
  903. list. Merge any entries against the same section. */
  904. for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
  905. {
  906. struct elf_dyn_relocs *q;
  907. for (q = edir->dyn_relocs; q != NULL; q = q->next)
  908. if (q->sec == p->sec)
  909. {
  910. q->pc_count += p->pc_count;
  911. q->count += p->count;
  912. *pp = p->next;
  913. break;
  914. }
  915. if (q == NULL)
  916. pp = &p->next;
  917. }
  918. *pp = edir->dyn_relocs;
  919. }
  920. edir->dyn_relocs = eind->dyn_relocs;
  921. eind->dyn_relocs = NULL;
  922. }
  923. if (ind->root.type == bfd_link_hash_indirect
  924. && dir->got.refcount <= 0)
  925. {
  926. edir->tls_type = eind->tls_type;
  927. eind->tls_type = GOT_UNKNOWN;
  928. }
  929. /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
  930. generate a R_386_COPY reloc. */
  931. edir->gotoff_ref |= eind->gotoff_ref;
  932. if (ELIMINATE_COPY_RELOCS
  933. && ind->root.type != bfd_link_hash_indirect
  934. && dir->dynamic_adjusted)
  935. {
  936. /* If called to transfer flags for a weakdef during processing
  937. of elf_adjust_dynamic_symbol, don't copy non_got_ref.
  938. We clear it ourselves for ELIMINATE_COPY_RELOCS. */
  939. dir->ref_dynamic |= ind->ref_dynamic;
  940. dir->ref_regular |= ind->ref_regular;
  941. dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
  942. dir->needs_plt |= ind->needs_plt;
  943. dir->pointer_equality_needed |= ind->pointer_equality_needed;
  944. }
  945. else
  946. {
  947. if (eind->func_pointer_refcount > 0)
  948. {
  949. edir->func_pointer_refcount += eind->func_pointer_refcount;
  950. eind->func_pointer_refcount = 0;
  951. }
  952. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  953. }
  954. }
  955. /* Return TRUE if the TLS access code sequence support transition
  956. from R_TYPE. */
  957. static bfd_boolean
  958. elf_i386_check_tls_transition (bfd *abfd, asection *sec,
  959. bfd_byte *contents,
  960. Elf_Internal_Shdr *symtab_hdr,
  961. struct elf_link_hash_entry **sym_hashes,
  962. unsigned int r_type,
  963. const Elf_Internal_Rela *rel,
  964. const Elf_Internal_Rela *relend)
  965. {
  966. unsigned int val, type;
  967. unsigned long r_symndx;
  968. struct elf_link_hash_entry *h;
  969. bfd_vma offset;
  970. /* Get the section contents. */
  971. if (contents == NULL)
  972. {
  973. if (elf_section_data (sec)->this_hdr.contents != NULL)
  974. contents = elf_section_data (sec)->this_hdr.contents;
  975. else
  976. {
  977. /* FIXME: How to better handle error condition? */
  978. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  979. return FALSE;
  980. /* Cache the section contents for elf_link_input_bfd. */
  981. elf_section_data (sec)->this_hdr.contents = contents;
  982. }
  983. }
  984. offset = rel->r_offset;
  985. switch (r_type)
  986. {
  987. case R_386_TLS_GD:
  988. case R_386_TLS_LDM:
  989. if (offset < 2 || (rel + 1) >= relend)
  990. return FALSE;
  991. type = bfd_get_8 (abfd, contents + offset - 2);
  992. if (r_type == R_386_TLS_GD)
  993. {
  994. /* Check transition from GD access model. Only
  995. leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
  996. leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
  997. can transit to different access model. */
  998. if ((offset + 10) > sec->size ||
  999. (type != 0x8d && type != 0x04))
  1000. return FALSE;
  1001. val = bfd_get_8 (abfd, contents + offset - 1);
  1002. if (type == 0x04)
  1003. {
  1004. /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
  1005. if (offset < 3)
  1006. return FALSE;
  1007. if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
  1008. return FALSE;
  1009. if ((val & 0xc7) != 0x05 || val == (4 << 3))
  1010. return FALSE;
  1011. }
  1012. else
  1013. {
  1014. /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
  1015. if ((val & 0xf8) != 0x80 || (val & 7) == 4)
  1016. return FALSE;
  1017. if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
  1018. return FALSE;
  1019. }
  1020. }
  1021. else
  1022. {
  1023. /* Check transition from LD access model. Only
  1024. leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
  1025. can transit to different access model. */
  1026. if (type != 0x8d || (offset + 9) > sec->size)
  1027. return FALSE;
  1028. val = bfd_get_8 (abfd, contents + offset - 1);
  1029. if ((val & 0xf8) != 0x80 || (val & 7) == 4)
  1030. return FALSE;
  1031. }
  1032. if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
  1033. return FALSE;
  1034. r_symndx = ELF32_R_SYM (rel[1].r_info);
  1035. if (r_symndx < symtab_hdr->sh_info)
  1036. return FALSE;
  1037. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1038. /* Use strncmp to check ___tls_get_addr since ___tls_get_addr
  1039. may be versioned. */
  1040. return (h != NULL
  1041. && h->root.root.string != NULL
  1042. && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
  1043. || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
  1044. && (strncmp (h->root.root.string, "___tls_get_addr",
  1045. 15) == 0));
  1046. case R_386_TLS_IE:
  1047. /* Check transition from IE access model:
  1048. movl foo@indntpoff(%rip), %eax
  1049. movl foo@indntpoff(%rip), %reg
  1050. addl foo@indntpoff(%rip), %reg
  1051. */
  1052. if (offset < 1 || (offset + 4) > sec->size)
  1053. return FALSE;
  1054. /* Check "movl foo@tpoff(%rip), %eax" first. */
  1055. val = bfd_get_8 (abfd, contents + offset - 1);
  1056. if (val == 0xa1)
  1057. return TRUE;
  1058. if (offset < 2)
  1059. return FALSE;
  1060. /* Check movl|addl foo@tpoff(%rip), %reg. */
  1061. type = bfd_get_8 (abfd, contents + offset - 2);
  1062. return ((type == 0x8b || type == 0x03)
  1063. && (val & 0xc7) == 0x05);
  1064. case R_386_TLS_GOTIE:
  1065. case R_386_TLS_IE_32:
  1066. /* Check transition from {IE_32,GOTIE} access model:
  1067. subl foo@{tpoff,gontoff}(%reg1), %reg2
  1068. movl foo@{tpoff,gontoff}(%reg1), %reg2
  1069. addl foo@{tpoff,gontoff}(%reg1), %reg2
  1070. */
  1071. if (offset < 2 || (offset + 4) > sec->size)
  1072. return FALSE;
  1073. val = bfd_get_8 (abfd, contents + offset - 1);
  1074. if ((val & 0xc0) != 0x80 || (val & 7) == 4)
  1075. return FALSE;
  1076. type = bfd_get_8 (abfd, contents + offset - 2);
  1077. return type == 0x8b || type == 0x2b || type == 0x03;
  1078. case R_386_TLS_GOTDESC:
  1079. /* Check transition from GDesc access model:
  1080. leal x@tlsdesc(%ebx), %eax
  1081. Make sure it's a leal adding ebx to a 32-bit offset
  1082. into any register, although it's probably almost always
  1083. going to be eax. */
  1084. if (offset < 2 || (offset + 4) > sec->size)
  1085. return FALSE;
  1086. if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
  1087. return FALSE;
  1088. val = bfd_get_8 (abfd, contents + offset - 1);
  1089. return (val & 0xc7) == 0x83;
  1090. case R_386_TLS_DESC_CALL:
  1091. /* Check transition from GDesc access model:
  1092. call *x@tlsdesc(%rax)
  1093. */
  1094. if (offset + 2 <= sec->size)
  1095. {
  1096. /* Make sure that it's a call *x@tlsdesc(%rax). */
  1097. static const unsigned char call[] = { 0xff, 0x10 };
  1098. return memcmp (contents + offset, call, 2) == 0;
  1099. }
  1100. return FALSE;
  1101. default:
  1102. abort ();
  1103. }
  1104. }
  1105. /* Return TRUE if the TLS access transition is OK or no transition
  1106. will be performed. Update R_TYPE if there is a transition. */
  1107. static bfd_boolean
  1108. elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
  1109. asection *sec, bfd_byte *contents,
  1110. Elf_Internal_Shdr *symtab_hdr,
  1111. struct elf_link_hash_entry **sym_hashes,
  1112. unsigned int *r_type, int tls_type,
  1113. const Elf_Internal_Rela *rel,
  1114. const Elf_Internal_Rela *relend,
  1115. struct elf_link_hash_entry *h,
  1116. unsigned long r_symndx)
  1117. {
  1118. unsigned int from_type = *r_type;
  1119. unsigned int to_type = from_type;
  1120. bfd_boolean check = TRUE;
  1121. /* Skip TLS transition for functions. */
  1122. if (h != NULL
  1123. && (h->type == STT_FUNC
  1124. || h->type == STT_GNU_IFUNC))
  1125. return TRUE;
  1126. switch (from_type)
  1127. {
  1128. case R_386_TLS_GD:
  1129. case R_386_TLS_GOTDESC:
  1130. case R_386_TLS_DESC_CALL:
  1131. case R_386_TLS_IE_32:
  1132. case R_386_TLS_IE:
  1133. case R_386_TLS_GOTIE:
  1134. if (bfd_link_executable (info))
  1135. {
  1136. if (h == NULL)
  1137. to_type = R_386_TLS_LE_32;
  1138. else if (from_type != R_386_TLS_IE
  1139. && from_type != R_386_TLS_GOTIE)
  1140. to_type = R_386_TLS_IE_32;
  1141. }
  1142. /* When we are called from elf_i386_relocate_section, CONTENTS
  1143. isn't NULL and there may be additional transitions based on
  1144. TLS_TYPE. */
  1145. if (contents != NULL)
  1146. {
  1147. unsigned int new_to_type = to_type;
  1148. if (bfd_link_executable (info)
  1149. && h != NULL
  1150. && h->dynindx == -1
  1151. && (tls_type & GOT_TLS_IE))
  1152. new_to_type = R_386_TLS_LE_32;
  1153. if (to_type == R_386_TLS_GD
  1154. || to_type == R_386_TLS_GOTDESC
  1155. || to_type == R_386_TLS_DESC_CALL)
  1156. {
  1157. if (tls_type == GOT_TLS_IE_POS)
  1158. new_to_type = R_386_TLS_GOTIE;
  1159. else if (tls_type & GOT_TLS_IE)
  1160. new_to_type = R_386_TLS_IE_32;
  1161. }
  1162. /* We checked the transition before when we were called from
  1163. elf_i386_check_relocs. We only want to check the new
  1164. transition which hasn't been checked before. */
  1165. check = new_to_type != to_type && from_type == to_type;
  1166. to_type = new_to_type;
  1167. }
  1168. break;
  1169. case R_386_TLS_LDM:
  1170. if (bfd_link_executable (info))
  1171. to_type = R_386_TLS_LE_32;
  1172. break;
  1173. default:
  1174. return TRUE;
  1175. }
  1176. /* Return TRUE if there is no transition. */
  1177. if (from_type == to_type)
  1178. return TRUE;
  1179. /* Check if the transition can be performed. */
  1180. if (check
  1181. && ! elf_i386_check_tls_transition (abfd, sec, contents,
  1182. symtab_hdr, sym_hashes,
  1183. from_type, rel, relend))
  1184. {
  1185. reloc_howto_type *from, *to;
  1186. const char *name;
  1187. from = elf_i386_rtype_to_howto (abfd, from_type);
  1188. to = elf_i386_rtype_to_howto (abfd, to_type);
  1189. if (h)
  1190. name = h->root.root.string;
  1191. else
  1192. {
  1193. struct elf_i386_link_hash_table *htab;
  1194. htab = elf_i386_hash_table (info);
  1195. if (htab == NULL)
  1196. name = "*unknown*";
  1197. else
  1198. {
  1199. Elf_Internal_Sym *isym;
  1200. isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  1201. abfd, r_symndx);
  1202. name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
  1203. }
  1204. }
  1205. (*_bfd_error_handler)
  1206. (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
  1207. "in section `%A' failed"),
  1208. abfd, sec, from->name, to->name, name,
  1209. (unsigned long) rel->r_offset);
  1210. bfd_set_error (bfd_error_bad_value);
  1211. return FALSE;
  1212. }
  1213. *r_type = to_type;
  1214. return TRUE;
  1215. }
  1216. /* Rename some of the generic section flags to better document how they
  1217. are used here. */
  1218. #define need_convert_mov_to_lea sec_flg0
  1219. /* Look through the relocs for a section during the first phase, and
  1220. calculate needed space in the global offset table, procedure linkage
  1221. table, and dynamic reloc sections. */
  1222. static bfd_boolean
  1223. elf_i386_check_relocs (bfd *abfd,
  1224. struct bfd_link_info *info,
  1225. asection *sec,
  1226. const Elf_Internal_Rela *relocs)
  1227. {
  1228. struct elf_i386_link_hash_table *htab;
  1229. Elf_Internal_Shdr *symtab_hdr;
  1230. struct elf_link_hash_entry **sym_hashes;
  1231. const Elf_Internal_Rela *rel;
  1232. const Elf_Internal_Rela *rel_end;
  1233. asection *sreloc;
  1234. bfd_boolean use_plt_got;
  1235. if (bfd_link_relocatable (info))
  1236. return TRUE;
  1237. BFD_ASSERT (is_i386_elf (abfd));
  1238. htab = elf_i386_hash_table (info);
  1239. if (htab == NULL)
  1240. return FALSE;
  1241. use_plt_got = (!get_elf_i386_backend_data (abfd)->is_vxworks
  1242. && (get_elf_i386_backend_data (abfd)
  1243. == &elf_i386_arch_bed));
  1244. symtab_hdr = &elf_symtab_hdr (abfd);
  1245. sym_hashes = elf_sym_hashes (abfd);
  1246. sreloc = NULL;
  1247. rel_end = relocs + sec->reloc_count;
  1248. for (rel = relocs; rel < rel_end; rel++)
  1249. {
  1250. unsigned int r_type;
  1251. unsigned long r_symndx;
  1252. struct elf_link_hash_entry *h;
  1253. struct elf_i386_link_hash_entry *eh;
  1254. Elf_Internal_Sym *isym;
  1255. const char *name;
  1256. bfd_boolean size_reloc;
  1257. r_symndx = ELF32_R_SYM (rel->r_info);
  1258. r_type = ELF32_R_TYPE (rel->r_info);
  1259. if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
  1260. {
  1261. (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
  1262. abfd,
  1263. r_symndx);
  1264. return FALSE;
  1265. }
  1266. if (r_symndx < symtab_hdr->sh_info)
  1267. {
  1268. /* A local symbol. */
  1269. isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  1270. abfd, r_symndx);
  1271. if (isym == NULL)
  1272. return FALSE;
  1273. /* Check relocation against local STT_GNU_IFUNC symbol. */
  1274. if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  1275. {
  1276. h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE);
  1277. if (h == NULL)
  1278. return FALSE;
  1279. /* Fake a STT_GNU_IFUNC symbol. */
  1280. h->type = STT_GNU_IFUNC;
  1281. h->def_regular = 1;
  1282. h->ref_regular = 1;
  1283. h->forced_local = 1;
  1284. h->root.type = bfd_link_hash_defined;
  1285. }
  1286. else
  1287. h = NULL;
  1288. }
  1289. else
  1290. {
  1291. isym = NULL;
  1292. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1293. while (h->root.type == bfd_link_hash_indirect
  1294. || h->root.type == bfd_link_hash_warning)
  1295. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1296. }
  1297. eh = (struct elf_i386_link_hash_entry *) h;
  1298. if (h != NULL)
  1299. {
  1300. /* Create the ifunc sections for static executables. If we
  1301. never see an indirect function symbol nor we are building
  1302. a static executable, those sections will be empty and
  1303. won't appear in output. */
  1304. switch (r_type)
  1305. {
  1306. default:
  1307. break;
  1308. case R_386_GOTOFF:
  1309. eh->gotoff_ref = 1;
  1310. case R_386_32:
  1311. case R_386_PC32:
  1312. case R_386_PLT32:
  1313. case R_386_GOT32:
  1314. if (htab->elf.dynobj == NULL)
  1315. htab->elf.dynobj = abfd;
  1316. if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
  1317. return FALSE;
  1318. break;
  1319. }
  1320. /* It is referenced by a non-shared object. */
  1321. h->ref_regular = 1;
  1322. h->root.non_ir_ref = 1;
  1323. if (h->type == STT_GNU_IFUNC)
  1324. elf_tdata (info->output_bfd)->has_gnu_symbols
  1325. |= elf_gnu_symbol_ifunc;
  1326. }
  1327. if (! elf_i386_tls_transition (info, abfd, sec, NULL,
  1328. symtab_hdr, sym_hashes,
  1329. &r_type, GOT_UNKNOWN,
  1330. rel, rel_end, h, r_symndx))
  1331. return FALSE;
  1332. switch (r_type)
  1333. {
  1334. case R_386_TLS_LDM:
  1335. htab->tls_ldm_got.refcount += 1;
  1336. goto create_got;
  1337. case R_386_PLT32:
  1338. /* This symbol requires a procedure linkage table entry. We
  1339. actually build the entry in adjust_dynamic_symbol,
  1340. because this might be a case of linking PIC code which is
  1341. never referenced by a dynamic object, in which case we
  1342. don't need to generate a procedure linkage table entry
  1343. after all. */
  1344. /* If this is a local symbol, we resolve it directly without
  1345. creating a procedure linkage table entry. */
  1346. if (h == NULL)
  1347. continue;
  1348. h->needs_plt = 1;
  1349. h->plt.refcount += 1;
  1350. break;
  1351. case R_386_SIZE32:
  1352. size_reloc = TRUE;
  1353. goto do_size;
  1354. case R_386_TLS_IE_32:
  1355. case R_386_TLS_IE:
  1356. case R_386_TLS_GOTIE:
  1357. if (!bfd_link_executable (info))
  1358. info->flags |= DF_STATIC_TLS;
  1359. /* Fall through */
  1360. case R_386_GOT32:
  1361. case R_386_TLS_GD:
  1362. case R_386_TLS_GOTDESC:
  1363. case R_386_TLS_DESC_CALL:
  1364. /* This symbol requires a global offset table entry. */
  1365. {
  1366. int tls_type, old_tls_type;
  1367. switch (r_type)
  1368. {
  1369. default:
  1370. case R_386_GOT32: tls_type = GOT_NORMAL; break;
  1371. case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
  1372. case R_386_TLS_GOTDESC:
  1373. case R_386_TLS_DESC_CALL:
  1374. tls_type = GOT_TLS_GDESC; break;
  1375. case R_386_TLS_IE_32:
  1376. if (ELF32_R_TYPE (rel->r_info) == r_type)
  1377. tls_type = GOT_TLS_IE_NEG;
  1378. else
  1379. /* If this is a GD->IE transition, we may use either of
  1380. R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
  1381. tls_type = GOT_TLS_IE;
  1382. break;
  1383. case R_386_TLS_IE:
  1384. case R_386_TLS_GOTIE:
  1385. tls_type = GOT_TLS_IE_POS; break;
  1386. }
  1387. if (h != NULL)
  1388. {
  1389. h->got.refcount += 1;
  1390. old_tls_type = elf_i386_hash_entry(h)->tls_type;
  1391. }
  1392. else
  1393. {
  1394. bfd_signed_vma *local_got_refcounts;
  1395. /* This is a global offset table entry for a local symbol. */
  1396. local_got_refcounts = elf_local_got_refcounts (abfd);
  1397. if (local_got_refcounts == NULL)
  1398. {
  1399. bfd_size_type size;
  1400. size = symtab_hdr->sh_info;
  1401. size *= (sizeof (bfd_signed_vma)
  1402. + sizeof (bfd_vma) + sizeof(char));
  1403. local_got_refcounts = (bfd_signed_vma *)
  1404. bfd_zalloc (abfd, size);
  1405. if (local_got_refcounts == NULL)
  1406. return FALSE;
  1407. elf_local_got_refcounts (abfd) = local_got_refcounts;
  1408. elf_i386_local_tlsdesc_gotent (abfd)
  1409. = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
  1410. elf_i386_local_got_tls_type (abfd)
  1411. = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
  1412. }
  1413. local_got_refcounts[r_symndx] += 1;
  1414. old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
  1415. }
  1416. if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
  1417. tls_type |= old_tls_type;
  1418. /* If a TLS symbol is accessed using IE at least once,
  1419. there is no point to use dynamic model for it. */
  1420. else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
  1421. && (! GOT_TLS_GD_ANY_P (old_tls_type)
  1422. || (tls_type & GOT_TLS_IE) == 0))
  1423. {
  1424. if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
  1425. tls_type = old_tls_type;
  1426. else if (GOT_TLS_GD_ANY_P (old_tls_type)
  1427. && GOT_TLS_GD_ANY_P (tls_type))
  1428. tls_type |= old_tls_type;
  1429. else
  1430. {
  1431. if (h)
  1432. name = h->root.root.string;
  1433. else
  1434. name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
  1435. NULL);
  1436. (*_bfd_error_handler)
  1437. (_("%B: `%s' accessed both as normal and "
  1438. "thread local symbol"),
  1439. abfd, name);
  1440. bfd_set_error (bfd_error_bad_value);
  1441. return FALSE;
  1442. }
  1443. }
  1444. if (old_tls_type != tls_type)
  1445. {
  1446. if (h != NULL)
  1447. elf_i386_hash_entry (h)->tls_type = tls_type;
  1448. else
  1449. elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
  1450. }
  1451. }
  1452. /* Fall through */
  1453. case R_386_GOTOFF:
  1454. case R_386_GOTPC:
  1455. create_got:
  1456. if (htab->elf.sgot == NULL)
  1457. {
  1458. if (htab->elf.dynobj == NULL)
  1459. htab->elf.dynobj = abfd;
  1460. if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
  1461. return FALSE;
  1462. }
  1463. if (r_type != R_386_TLS_IE)
  1464. break;
  1465. /* Fall through */
  1466. case R_386_TLS_LE_32:
  1467. case R_386_TLS_LE:
  1468. if (bfd_link_executable (info))
  1469. break;
  1470. info->flags |= DF_STATIC_TLS;
  1471. /* Fall through */
  1472. case R_386_32:
  1473. case R_386_PC32:
  1474. if (h != NULL && bfd_link_executable (info))
  1475. {
  1476. /* If this reloc is in a read-only section, we might
  1477. need a copy reloc. We can't check reliably at this
  1478. stage whether the section is read-only, as input
  1479. sections have not yet been mapped to output sections.
  1480. Tentatively set the flag for now, and correct in
  1481. adjust_dynamic_symbol. */
  1482. h->non_got_ref = 1;
  1483. /* We may need a .plt entry if the function this reloc
  1484. refers to is in a shared lib. */
  1485. h->plt.refcount += 1;
  1486. if (r_type == R_386_PC32)
  1487. {
  1488. /* Since something like ".long foo - ." may be used
  1489. as pointer, make sure that PLT is used if foo is
  1490. a function defined in a shared library. */
  1491. if ((sec->flags & SEC_CODE) == 0)
  1492. h->pointer_equality_needed = 1;
  1493. }
  1494. else
  1495. {
  1496. h->pointer_equality_needed = 1;
  1497. /* R_386_32 can be resolved at run-time. */
  1498. if (r_type == R_386_32
  1499. && (sec->flags & SEC_READONLY) == 0)
  1500. eh->func_pointer_refcount += 1;
  1501. }
  1502. }
  1503. size_reloc = FALSE;
  1504. do_size:
  1505. /* If we are creating a shared library, and this is a reloc
  1506. against a global symbol, or a non PC relative reloc
  1507. against a local symbol, then we need to copy the reloc
  1508. into the shared library. However, if we are linking with
  1509. -Bsymbolic, we do not need to copy a reloc against a
  1510. global symbol which is defined in an object we are
  1511. including in the link (i.e., DEF_REGULAR is set). At
  1512. this point we have not seen all the input files, so it is
  1513. possible that DEF_REGULAR is not set now but will be set
  1514. later (it is never cleared). In case of a weak definition,
  1515. DEF_REGULAR may be cleared later by a strong definition in
  1516. a shared library. We account for that possibility below by
  1517. storing information in the relocs_copied field of the hash
  1518. table entry. A similar situation occurs when creating
  1519. shared libraries and symbol visibility changes render the
  1520. symbol local.
  1521. If on the other hand, we are creating an executable, we
  1522. may need to keep relocations for symbols satisfied by a
  1523. dynamic library if we manage to avoid copy relocs for the
  1524. symbol. */
  1525. if ((bfd_link_pic (info)
  1526. && (sec->flags & SEC_ALLOC) != 0
  1527. && (r_type != R_386_PC32
  1528. || (h != NULL
  1529. && (! SYMBOLIC_BIND (info, h)
  1530. || h->root.type == bfd_link_hash_defweak
  1531. || !h->def_regular))))
  1532. || (ELIMINATE_COPY_RELOCS
  1533. && !bfd_link_pic (info)
  1534. && (sec->flags & SEC_ALLOC) != 0
  1535. && h != NULL
  1536. && (h->root.type == bfd_link_hash_defweak
  1537. || !h->def_regular)))
  1538. {
  1539. struct elf_dyn_relocs *p;
  1540. struct elf_dyn_relocs **head;
  1541. /* We must copy these reloc types into the output file.
  1542. Create a reloc section in dynobj and make room for
  1543. this reloc. */
  1544. if (sreloc == NULL)
  1545. {
  1546. if (htab->elf.dynobj == NULL)
  1547. htab->elf.dynobj = abfd;
  1548. sreloc = _bfd_elf_make_dynamic_reloc_section
  1549. (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
  1550. if (sreloc == NULL)
  1551. return FALSE;
  1552. }
  1553. /* If this is a global symbol, we count the number of
  1554. relocations we need for this symbol. */
  1555. if (h != NULL)
  1556. {
  1557. head = &eh->dyn_relocs;
  1558. }
  1559. else
  1560. {
  1561. /* Track dynamic relocs needed for local syms too.
  1562. We really need local syms available to do this
  1563. easily. Oh well. */
  1564. void **vpp;
  1565. asection *s;
  1566. isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  1567. abfd, r_symndx);
  1568. if (isym == NULL)
  1569. return FALSE;
  1570. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1571. if (s == NULL)
  1572. s = sec;
  1573. vpp = &elf_section_data (s)->local_dynrel;
  1574. head = (struct elf_dyn_relocs **)vpp;
  1575. }
  1576. p = *head;
  1577. if (p == NULL || p->sec != sec)
  1578. {
  1579. bfd_size_type amt = sizeof *p;
  1580. p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
  1581. amt);
  1582. if (p == NULL)
  1583. return FALSE;
  1584. p->next = *head;
  1585. *head = p;
  1586. p->sec = sec;
  1587. p->count = 0;
  1588. p->pc_count = 0;
  1589. }
  1590. p->count += 1;
  1591. /* Count size relocation as PC-relative relocation. */
  1592. if (r_type == R_386_PC32 || size_reloc)
  1593. p->pc_count += 1;
  1594. }
  1595. break;
  1596. /* This relocation describes the C++ object vtable hierarchy.
  1597. Reconstruct it for later use during GC. */
  1598. case R_386_GNU_VTINHERIT:
  1599. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  1600. return FALSE;
  1601. break;
  1602. /* This relocation describes which C++ vtable entries are actually
  1603. used. Record for later use during GC. */
  1604. case R_386_GNU_VTENTRY:
  1605. BFD_ASSERT (h != NULL);
  1606. if (h != NULL
  1607. && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
  1608. return FALSE;
  1609. break;
  1610. default:
  1611. break;
  1612. }
  1613. if (use_plt_got
  1614. && h != NULL
  1615. && h->plt.refcount > 0
  1616. && (((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
  1617. || h->got.refcount > 0)
  1618. && htab->plt_got == NULL)
  1619. {
  1620. /* Create the GOT procedure linkage table. */
  1621. unsigned int plt_got_align;
  1622. const struct elf_backend_data *bed;
  1623. bed = get_elf_backend_data (info->output_bfd);
  1624. BFD_ASSERT (sizeof (elf_i386_got_plt_entry) == 8
  1625. && (sizeof (elf_i386_got_plt_entry)
  1626. == sizeof (elf_i386_pic_got_plt_entry)));
  1627. plt_got_align = 3;
  1628. if (htab->elf.dynobj == NULL)
  1629. htab->elf.dynobj = abfd;
  1630. htab->plt_got
  1631. = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
  1632. ".plt.got",
  1633. (bed->dynamic_sec_flags
  1634. | SEC_ALLOC
  1635. | SEC_CODE
  1636. | SEC_LOAD
  1637. | SEC_READONLY));
  1638. if (htab->plt_got == NULL
  1639. || !bfd_set_section_alignment (htab->elf.dynobj,
  1640. htab->plt_got,
  1641. plt_got_align))
  1642. return FALSE;
  1643. }
  1644. if (r_type == R_386_GOT32
  1645. && (h == NULL || h->type != STT_GNU_IFUNC))
  1646. sec->need_convert_mov_to_lea = 1;
  1647. }
  1648. return TRUE;
  1649. }
  1650. /* Return the section that should be marked against GC for a given
  1651. relocation. */
  1652. static asection *
  1653. elf_i386_gc_mark_hook (asection *sec,
  1654. struct bfd_link_info *info,
  1655. Elf_Internal_Rela *rel,
  1656. struct elf_link_hash_entry *h,
  1657. Elf_Internal_Sym *sym)
  1658. {
  1659. if (h != NULL)
  1660. switch (ELF32_R_TYPE (rel->r_info))
  1661. {
  1662. case R_386_GNU_VTINHERIT:
  1663. case R_386_GNU_VTENTRY:
  1664. return NULL;
  1665. }
  1666. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1667. }
  1668. /* Update the got entry reference counts for the section being removed. */
  1669. static bfd_boolean
  1670. elf_i386_gc_sweep_hook (bfd *abfd,
  1671. struct bfd_link_info *info,
  1672. asection *sec,
  1673. const Elf_Internal_Rela *relocs)
  1674. {
  1675. struct elf_i386_link_hash_table *htab;
  1676. Elf_Internal_Shdr *symtab_hdr;
  1677. struct elf_link_hash_entry **sym_hashes;
  1678. bfd_signed_vma *local_got_refcounts;
  1679. const Elf_Internal_Rela *rel, *relend;
  1680. if (bfd_link_relocatable (info))
  1681. return TRUE;
  1682. htab = elf_i386_hash_table (info);
  1683. if (htab == NULL)
  1684. return FALSE;
  1685. elf_section_data (sec)->local_dynrel = NULL;
  1686. symtab_hdr = &elf_symtab_hdr (abfd);
  1687. sym_hashes = elf_sym_hashes (abfd);
  1688. local_got_refcounts = elf_local_got_refcounts (abfd);
  1689. relend = relocs + sec->reloc_count;
  1690. for (rel = relocs; rel < relend; rel++)
  1691. {
  1692. unsigned long r_symndx;
  1693. unsigned int r_type;
  1694. struct elf_link_hash_entry *h = NULL;
  1695. r_symndx = ELF32_R_SYM (rel->r_info);
  1696. if (r_symndx >= symtab_hdr->sh_info)
  1697. {
  1698. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1699. while (h->root.type == bfd_link_hash_indirect
  1700. || h->root.type == bfd_link_hash_warning)
  1701. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1702. }
  1703. else
  1704. {
  1705. /* A local symbol. */
  1706. Elf_Internal_Sym *isym;
  1707. isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  1708. abfd, r_symndx);
  1709. /* Check relocation against local STT_GNU_IFUNC symbol. */
  1710. if (isym != NULL
  1711. && ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  1712. {
  1713. h = elf_i386_get_local_sym_hash (htab, abfd, rel, FALSE);
  1714. if (h == NULL)
  1715. abort ();
  1716. }
  1717. }
  1718. if (h)
  1719. {
  1720. struct elf_i386_link_hash_entry *eh;
  1721. struct elf_dyn_relocs **pp;
  1722. struct elf_dyn_relocs *p;
  1723. eh = (struct elf_i386_link_hash_entry *) h;
  1724. for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
  1725. if (p->sec == sec)
  1726. {
  1727. /* Everything must go for SEC. */
  1728. *pp = p->next;
  1729. break;
  1730. }
  1731. }
  1732. r_type = ELF32_R_TYPE (rel->r_info);
  1733. if (! elf_i386_tls_transition (info, abfd, sec, NULL,
  1734. symtab_hdr, sym_hashes,
  1735. &r_type, GOT_UNKNOWN,
  1736. rel, relend, h, r_symndx))
  1737. return FALSE;
  1738. switch (r_type)
  1739. {
  1740. case R_386_TLS_LDM:
  1741. if (htab->tls_ldm_got.refcount > 0)
  1742. htab->tls_ldm_got.refcount -= 1;
  1743. break;
  1744. case R_386_TLS_GD:
  1745. case R_386_TLS_GOTDESC:
  1746. case R_386_TLS_DESC_CALL:
  1747. case R_386_TLS_IE_32:
  1748. case R_386_TLS_IE:
  1749. case R_386_TLS_GOTIE:
  1750. case R_386_GOT32:
  1751. if (h != NULL)
  1752. {
  1753. if (h->got.refcount > 0)
  1754. h->got.refcount -= 1;
  1755. if (h->type == STT_GNU_IFUNC)
  1756. {
  1757. if (h->plt.refcount > 0)
  1758. h->plt.refcount -= 1;
  1759. }
  1760. }
  1761. else if (local_got_refcounts != NULL)
  1762. {
  1763. if (local_got_refcounts[r_symndx] > 0)
  1764. local_got_refcounts[r_symndx] -= 1;
  1765. }
  1766. break;
  1767. case R_386_32:
  1768. case R_386_PC32:
  1769. case R_386_SIZE32:
  1770. if (bfd_link_pic (info)
  1771. && (h == NULL || h->type != STT_GNU_IFUNC))
  1772. break;
  1773. /* Fall through */
  1774. case R_386_PLT32:
  1775. if (h != NULL)
  1776. {
  1777. if (h->plt.refcount > 0)
  1778. h->plt.refcount -= 1;
  1779. if (r_type == R_386_32
  1780. && (sec->flags & SEC_READONLY) == 0)
  1781. {
  1782. struct elf_i386_link_hash_entry *eh
  1783. = (struct elf_i386_link_hash_entry *) h;
  1784. if (eh->func_pointer_refcount > 0)
  1785. eh->func_pointer_refcount -= 1;
  1786. }
  1787. }
  1788. break;
  1789. case R_386_GOTOFF:
  1790. if (h != NULL && h->type == STT_GNU_IFUNC)
  1791. {
  1792. if (h->got.refcount > 0)
  1793. h->got.refcount -= 1;
  1794. if (h->plt.refcount > 0)
  1795. h->plt.refcount -= 1;
  1796. }
  1797. break;
  1798. default:
  1799. break;
  1800. }
  1801. }
  1802. return TRUE;
  1803. }
  1804. /* Adjust a symbol defined by a dynamic object and referenced by a
  1805. regular object. The current definition is in some section of the
  1806. dynamic object, but we're not including those sections. We have to
  1807. change the definition to something the rest of the link can
  1808. understand. */
  1809. static bfd_boolean
  1810. elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
  1811. struct elf_link_hash_entry *h)
  1812. {
  1813. struct elf_i386_link_hash_table *htab;
  1814. asection *s;
  1815. struct elf_i386_link_hash_entry *eh;
  1816. struct elf_dyn_relocs *p;
  1817. /* STT_GNU_IFUNC symbol must go through PLT. */
  1818. if (h->type == STT_GNU_IFUNC)
  1819. {
  1820. /* All local STT_GNU_IFUNC references must be treate as local
  1821. calls via local PLT. */
  1822. if (h->ref_regular
  1823. && SYMBOL_CALLS_LOCAL (info, h))
  1824. {
  1825. bfd_size_type pc_count = 0, count = 0;
  1826. struct elf_dyn_relocs **pp;
  1827. eh = (struct elf_i386_link_hash_entry *) h;
  1828. for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
  1829. {
  1830. pc_count += p->pc_count;
  1831. p->count -= p->pc_count;
  1832. p->pc_count = 0;
  1833. count += p->count;
  1834. if (p->count == 0)
  1835. *pp = p->next;
  1836. else
  1837. pp = &p->next;
  1838. }
  1839. if (pc_count || count)
  1840. {
  1841. h->needs_plt = 1;
  1842. h->non_got_ref = 1;
  1843. if (h->plt.refcount <= 0)
  1844. h->plt.refcount = 1;
  1845. else
  1846. h->plt.refcount += 1;
  1847. }
  1848. }
  1849. if (h->plt.refcount <= 0)
  1850. {
  1851. h->plt.offset = (bfd_vma) -1;
  1852. h->needs_plt = 0;
  1853. }
  1854. return TRUE;
  1855. }
  1856. /* If this is a function, put it in the procedure linkage table. We
  1857. will fill in the contents of the procedure linkage table later,
  1858. when we know the address of the .got section. */
  1859. if (h->type == STT_FUNC
  1860. || h->needs_plt)
  1861. {
  1862. if (h->plt.refcount <= 0
  1863. || SYMBOL_CALLS_LOCAL (info, h)
  1864. || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1865. && h->root.type == bfd_link_hash_undefweak))
  1866. {
  1867. /* This case can occur if we saw a PLT32 reloc in an input
  1868. file, but the symbol was never referred to by a dynamic
  1869. object, or if all references were garbage collected. In
  1870. such a case, we don't actually need to build a procedure
  1871. linkage table, and we can just do a PC32 reloc instead. */
  1872. h->plt.offset = (bfd_vma) -1;
  1873. h->needs_plt = 0;
  1874. }
  1875. return TRUE;
  1876. }
  1877. else
  1878. /* It's possible that we incorrectly decided a .plt reloc was
  1879. needed for an R_386_PC32 reloc to a non-function sym in
  1880. check_relocs. We can't decide accurately between function and
  1881. non-function syms in check-relocs; Objects loaded later in
  1882. the link may change h->type. So fix it now. */
  1883. h->plt.offset = (bfd_vma) -1;
  1884. /* If this is a weak symbol, and there is a real definition, the
  1885. processor independent code will have arranged for us to see the
  1886. real definition first, and we can just use the same value. */
  1887. if (h->u.weakdef != NULL)
  1888. {
  1889. BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
  1890. || h->u.weakdef->root.type == bfd_link_hash_defweak);
  1891. h->root.u.def.section = h->u.weakdef->root.u.def.section;
  1892. h->root.u.def.value = h->u.weakdef->root.u.def.value;
  1893. if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
  1894. h->non_got_ref = h->u.weakdef->non_got_ref;
  1895. return TRUE;
  1896. }
  1897. /* This is a reference to a symbol defined by a dynamic object which
  1898. is not a function. */
  1899. /* If we are creating a shared library, we must presume that the
  1900. only references to the symbol are via the global offset table.
  1901. For such cases we need not do anything here; the relocations will
  1902. be handled correctly by relocate_section. */
  1903. if (!bfd_link_executable (info))
  1904. return TRUE;
  1905. /* If there are no references to this symbol that do not use the
  1906. GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
  1907. reloc. */
  1908. eh = (struct elf_i386_link_hash_entry *) h;
  1909. if (!h->non_got_ref && !eh->gotoff_ref)
  1910. return TRUE;
  1911. /* If -z nocopyreloc was given, we won't generate them either. */
  1912. if (info->nocopyreloc)
  1913. {
  1914. h->non_got_ref = 0;
  1915. return TRUE;
  1916. }
  1917. htab = elf_i386_hash_table (info);
  1918. if (htab == NULL)
  1919. return FALSE;
  1920. /* If there aren't any dynamic relocs in read-only sections nor
  1921. R_386_GOTOFF relocation, then we can keep the dynamic relocs and
  1922. avoid the copy reloc. This doesn't work on VxWorks, where we can
  1923. not have dynamic relocations (other than copy and jump slot
  1924. relocations) in an executable. */
  1925. if (ELIMINATE_COPY_RELOCS
  1926. && !eh->gotoff_ref
  1927. && !get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
  1928. {
  1929. for (p = eh->dyn_relocs; p != NULL; p = p->next)
  1930. {
  1931. s = p->sec->output_section;
  1932. if (s != NULL && (s->flags & SEC_READONLY) != 0)
  1933. break;
  1934. }
  1935. if (p == NULL)
  1936. {
  1937. h->non_got_ref = 0;
  1938. return TRUE;
  1939. }
  1940. }
  1941. /* We must allocate the symbol in our .dynbss section, which will
  1942. become part of the .bss section of the executable. There will be
  1943. an entry for this symbol in the .dynsym section. The dynamic
  1944. object will contain position independent code, so all references
  1945. from the dynamic object to this symbol will go through the global
  1946. offset table. The dynamic linker will use the .dynsym entry to
  1947. determine the address it must put in the global offset table, so
  1948. both the dynamic object and the regular object will refer to the
  1949. same memory location for the variable. */
  1950. /* We must generate a R_386_COPY reloc to tell the dynamic linker to
  1951. copy the initial value out of the dynamic object and into the
  1952. runtime process image. */
  1953. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  1954. {
  1955. htab->srelbss->size += sizeof (Elf32_External_Rel);
  1956. h->needs_copy = 1;
  1957. }
  1958. s = htab->sdynbss;
  1959. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  1960. }
  1961. /* Allocate space in .plt, .got and associated reloc sections for
  1962. dynamic relocs. */
  1963. static bfd_boolean
  1964. elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  1965. {
  1966. struct bfd_link_info *info;
  1967. struct elf_i386_link_hash_table *htab;
  1968. struct elf_i386_link_hash_entry *eh;
  1969. struct elf_dyn_relocs *p;
  1970. unsigned plt_entry_size;
  1971. if (h->root.type == bfd_link_hash_indirect)
  1972. return TRUE;
  1973. eh = (struct elf_i386_link_hash_entry *) h;
  1974. info = (struct bfd_link_info *) inf;
  1975. htab = elf_i386_hash_table (info);
  1976. if (htab == NULL)
  1977. return FALSE;
  1978. plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
  1979. /* Clear the reference count of function pointer relocations if
  1980. symbol isn't a normal function. */
  1981. if (h->type != STT_FUNC)
  1982. eh->func_pointer_refcount = 0;
  1983. /* We can't use the GOT PLT if pointer equality is needed since
  1984. finish_dynamic_symbol won't clear symbol value and the dynamic
  1985. linker won't update the GOT slot. We will get into an infinite
  1986. loop at run-time. */
  1987. if (htab->plt_got != NULL
  1988. && h->type != STT_GNU_IFUNC
  1989. && !h->pointer_equality_needed
  1990. && h->plt.refcount > 0
  1991. && h->got.refcount > 0)
  1992. {
  1993. /* Don't use the regular PLT if there are both GOT and GOTPLT
  1994. reloctions. */
  1995. h->plt.offset = (bfd_vma) -1;
  1996. /* Use the GOT PLT. */
  1997. eh->plt_got.refcount = 1;
  1998. }
  1999. /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
  2000. here if it is defined and referenced in a non-shared object. */
  2001. if (h->type == STT_GNU_IFUNC
  2002. && h->def_regular)
  2003. return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
  2004. plt_entry_size,
  2005. plt_entry_size, 4);
  2006. /* Don't create the PLT entry if there are only function pointer
  2007. relocations which can be resolved at run-time. */
  2008. else if (htab->elf.dynamic_sections_created
  2009. && (h->plt.refcount > eh->func_pointer_refcount
  2010. || eh->plt_got.refcount > 0))
  2011. {
  2012. bfd_boolean use_plt_got;
  2013. /* Clear the reference count of function pointer relocations
  2014. if PLT is used. */
  2015. eh->func_pointer_refcount = 0;
  2016. if ((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
  2017. {
  2018. /* Don't use the regular PLT for DF_BIND_NOW. */
  2019. h->plt.offset = (bfd_vma) -1;
  2020. /* Use the GOT PLT. */
  2021. h->got.refcount = 1;
  2022. eh->plt_got.refcount = 1;
  2023. }
  2024. use_plt_got = eh->plt_got.refcount > 0;
  2025. /* Make sure this symbol is output as a dynamic symbol.
  2026. Undefined weak syms won't yet be marked as dynamic. */
  2027. if (h->dynindx == -1
  2028. && !h->forced_local)
  2029. {
  2030. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2031. return FALSE;
  2032. }
  2033. if (bfd_link_pic (info)
  2034. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
  2035. {
  2036. asection *s = htab->elf.splt;
  2037. asection *got_s = htab->plt_got;
  2038. /* If this is the first .plt entry, make room for the special
  2039. first entry. The .plt section is used by prelink to undo
  2040. prelinking for dynamic relocations. */
  2041. if (s->size == 0)
  2042. s->size = plt_entry_size;
  2043. if (use_plt_got)
  2044. eh->plt_got.offset = got_s->size;
  2045. else
  2046. h->plt.offset = s->size;
  2047. /* If this symbol is not defined in a regular file, and we are
  2048. not generating a shared library, then set the symbol to this
  2049. location in the .plt. This is required to make function
  2050. pointers compare as equal between the normal executable and
  2051. the shared library. */
  2052. if (! bfd_link_pic (info)
  2053. && !h->def_regular)
  2054. {
  2055. if (use_plt_got)
  2056. {
  2057. /* We need to make a call to the entry of the GOT PLT
  2058. instead of regular PLT entry. */
  2059. h->root.u.def.section = got_s;
  2060. h->root.u.def.value = eh->plt_got.offset;
  2061. }
  2062. else
  2063. {
  2064. h->root.u.def.section = s;
  2065. h->root.u.def.value = h->plt.offset;
  2066. }
  2067. }
  2068. /* Make room for this entry. */
  2069. if (use_plt_got)
  2070. got_s->size += sizeof (elf_i386_got_plt_entry);
  2071. else
  2072. {
  2073. s->size += plt_entry_size;
  2074. /* We also need to make an entry in the .got.plt section,
  2075. which will be placed in the .got section by the linker
  2076. script. */
  2077. htab->elf.sgotplt->size += 4;
  2078. /* We also need to make an entry in the .rel.plt section. */
  2079. htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
  2080. htab->elf.srelplt->reloc_count++;
  2081. }
  2082. if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks
  2083. && !bfd_link_pic (info))
  2084. {
  2085. /* VxWorks has a second set of relocations for each PLT entry
  2086. in executables. They go in a separate relocation section,
  2087. which is processed by the kernel loader. */
  2088. /* There are two relocations for the initial PLT entry: an
  2089. R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
  2090. R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
  2091. if (h->plt.offset == plt_entry_size)
  2092. htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
  2093. /* There are two extra relocations for each subsequent PLT entry:
  2094. an R_386_32 relocation for the GOT entry, and an R_386_32
  2095. relocation for the PLT entry. */
  2096. htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
  2097. }
  2098. }
  2099. else
  2100. {
  2101. h->plt.offset = (bfd_vma) -1;
  2102. h->needs_plt = 0;
  2103. }
  2104. }
  2105. else
  2106. {
  2107. h->plt.offset = (bfd_vma) -1;
  2108. h->needs_plt = 0;
  2109. }
  2110. eh->tlsdesc_got = (bfd_vma) -1;
  2111. /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
  2112. make it a R_386_TLS_LE_32 requiring no TLS entry. */
  2113. if (h->got.refcount > 0
  2114. && bfd_link_executable (info)
  2115. && h->dynindx == -1
  2116. && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
  2117. h->got.offset = (bfd_vma) -1;
  2118. else if (h->got.refcount > 0)
  2119. {
  2120. asection *s;
  2121. bfd_boolean dyn;
  2122. int tls_type = elf_i386_hash_entry(h)->tls_type;
  2123. /* Make sure this symbol is output as a dynamic symbol.
  2124. Undefined weak syms won't yet be marked as dynamic. */
  2125. if (h->dynindx == -1
  2126. && !h->forced_local)
  2127. {
  2128. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2129. return FALSE;
  2130. }
  2131. s = htab->elf.sgot;
  2132. if (GOT_TLS_GDESC_P (tls_type))
  2133. {
  2134. eh->tlsdesc_got = htab->elf.sgotplt->size
  2135. - elf_i386_compute_jump_table_size (htab);
  2136. htab->elf.sgotplt->size += 8;
  2137. h->got.offset = (bfd_vma) -2;
  2138. }
  2139. if (! GOT_TLS_GDESC_P (tls_type)
  2140. || GOT_TLS_GD_P (tls_type))
  2141. {
  2142. h->got.offset = s->size;
  2143. s->size += 4;
  2144. /* R_386_TLS_GD needs 2 consecutive GOT slots. */
  2145. if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
  2146. s->size += 4;
  2147. }
  2148. dyn = htab->elf.dynamic_sections_created;
  2149. /* R_386_TLS_IE_32 needs one dynamic relocation,
  2150. R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
  2151. (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
  2152. need two), R_386_TLS_GD needs one if local symbol and two if
  2153. global. */
  2154. if (tls_type == GOT_TLS_IE_BOTH)
  2155. htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
  2156. else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
  2157. || (tls_type & GOT_TLS_IE))
  2158. htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
  2159. else if (GOT_TLS_GD_P (tls_type))
  2160. htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
  2161. else if (! GOT_TLS_GDESC_P (tls_type)
  2162. && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2163. || h->root.type != bfd_link_hash_undefweak)
  2164. && (bfd_link_pic (info)
  2165. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
  2166. htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
  2167. if (GOT_TLS_GDESC_P (tls_type))
  2168. htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
  2169. }
  2170. else
  2171. h->got.offset = (bfd_vma) -1;
  2172. if (eh->dyn_relocs == NULL)
  2173. return TRUE;
  2174. /* In the shared -Bsymbolic case, discard space allocated for
  2175. dynamic pc-relative relocs against symbols which turn out to be
  2176. defined in regular objects. For the normal shared case, discard
  2177. space for pc-relative relocs that have become local due to symbol
  2178. visibility changes. */
  2179. if (bfd_link_pic (info))
  2180. {
  2181. /* The only reloc that uses pc_count is R_386_PC32, which will
  2182. appear on a call or on something like ".long foo - .". We
  2183. want calls to protected symbols to resolve directly to the
  2184. function rather than going via the plt. If people want
  2185. function pointer comparisons to work as expected then they
  2186. should avoid writing assembly like ".long foo - .". */
  2187. if (SYMBOL_CALLS_LOCAL (info, h))
  2188. {
  2189. struct elf_dyn_relocs **pp;
  2190. for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
  2191. {
  2192. p->count -= p->pc_count;
  2193. p->pc_count = 0;
  2194. if (p->count == 0)
  2195. *pp = p->next;
  2196. else
  2197. pp = &p->next;
  2198. }
  2199. }
  2200. if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
  2201. {
  2202. struct elf_dyn_relocs **pp;
  2203. for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
  2204. {
  2205. if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
  2206. *pp = p->next;
  2207. else
  2208. pp = &p->next;
  2209. }
  2210. }
  2211. /* Also discard relocs on undefined weak syms with non-default
  2212. visibility. */
  2213. if (eh->dyn_relocs != NULL
  2214. && h->root.type == bfd_link_hash_undefweak)
  2215. {
  2216. if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  2217. eh->dyn_relocs = NULL;
  2218. /* Make sure undefined weak symbols are output as a dynamic
  2219. symbol in PIEs. */
  2220. else if (h->dynindx == -1
  2221. && !h->forced_local)
  2222. {
  2223. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2224. return FALSE;
  2225. }
  2226. }
  2227. }
  2228. else if (ELIMINATE_COPY_RELOCS)
  2229. {
  2230. /* For the non-shared case, discard space for relocs against
  2231. symbols which turn out to need copy relocs or are not
  2232. dynamic. Keep dynamic relocations for run-time function
  2233. pointer initialization. */
  2234. if ((!h->non_got_ref || eh->func_pointer_refcount > 0)
  2235. && ((h->def_dynamic
  2236. && !h->def_regular)
  2237. || (htab->elf.dynamic_sections_created
  2238. && (h->root.type == bfd_link_hash_undefweak
  2239. || h->root.type == bfd_link_hash_undefined))))
  2240. {
  2241. /* Make sure this symbol is output as a dynamic symbol.
  2242. Undefined weak syms won't yet be marked as dynamic. */
  2243. if (h->dynindx == -1
  2244. && !h->forced_local)
  2245. {
  2246. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2247. return FALSE;
  2248. }
  2249. /* If that succeeded, we know we'll be keeping all the
  2250. relocs. */
  2251. if (h->dynindx != -1)
  2252. goto keep;
  2253. }
  2254. eh->dyn_relocs = NULL;
  2255. eh->func_pointer_refcount = 0;
  2256. keep: ;
  2257. }
  2258. /* Finally, allocate space. */
  2259. for (p = eh->dyn_relocs; p != NULL; p = p->next)
  2260. {
  2261. asection *sreloc;
  2262. sreloc = elf_section_data (p->sec)->sreloc;
  2263. BFD_ASSERT (sreloc != NULL);
  2264. sreloc->size += p->count * sizeof (Elf32_External_Rel);
  2265. }
  2266. return TRUE;
  2267. }
  2268. /* Allocate space in .plt, .got and associated reloc sections for
  2269. local dynamic relocs. */
  2270. static bfd_boolean
  2271. elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
  2272. {
  2273. struct elf_link_hash_entry *h
  2274. = (struct elf_link_hash_entry *) *slot;
  2275. if (h->type != STT_GNU_IFUNC
  2276. || !h->def_regular
  2277. || !h->ref_regular
  2278. || !h->forced_local
  2279. || h->root.type != bfd_link_hash_defined)
  2280. abort ();
  2281. return elf_i386_allocate_dynrelocs (h, inf);
  2282. }
  2283. /* Find any dynamic relocs that apply to read-only sections. */
  2284. static bfd_boolean
  2285. elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  2286. {
  2287. struct elf_i386_link_hash_entry *eh;
  2288. struct elf_dyn_relocs *p;
  2289. /* Skip local IFUNC symbols. */
  2290. if (h->forced_local && h->type == STT_GNU_IFUNC)
  2291. return TRUE;
  2292. eh = (struct elf_i386_link_hash_entry *) h;
  2293. for (p = eh->dyn_relocs; p != NULL; p = p->next)
  2294. {
  2295. asection *s = p->sec->output_section;
  2296. if (s != NULL && (s->flags & SEC_READONLY) != 0)
  2297. {
  2298. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  2299. info->flags |= DF_TEXTREL;
  2300. if ((info->warn_shared_textrel && bfd_link_pic (info))
  2301. || info->error_textrel)
  2302. info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
  2303. p->sec->owner, h->root.root.string,
  2304. p->sec);
  2305. /* Not an error, just cut short the traversal. */
  2306. return FALSE;
  2307. }
  2308. }
  2309. return TRUE;
  2310. }
  2311. /* Convert
  2312. mov foo@GOT(%reg), %reg
  2313. to
  2314. lea foo@GOTOFF(%reg), %reg
  2315. with the local symbol, foo. */
  2316. static bfd_boolean
  2317. elf_i386_convert_mov_to_lea (bfd *abfd, asection *sec,
  2318. struct bfd_link_info *link_info)
  2319. {
  2320. Elf_Internal_Shdr *symtab_hdr;
  2321. Elf_Internal_Rela *internal_relocs;
  2322. Elf_Internal_Rela *irel, *irelend;
  2323. bfd_byte *contents;
  2324. struct elf_i386_link_hash_table *htab;
  2325. bfd_boolean changed_contents;
  2326. bfd_boolean changed_relocs;
  2327. bfd_signed_vma *local_got_refcounts;
  2328. /* Don't even try to convert non-ELF outputs. */
  2329. if (!is_elf_hash_table (link_info->hash))
  2330. return FALSE;
  2331. /* Nothing to do if there is no need or no output. */
  2332. if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
  2333. || sec->need_convert_mov_to_lea == 0
  2334. || bfd_is_abs_section (sec->output_section))
  2335. return TRUE;
  2336. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2337. /* Load the relocations for this section. */
  2338. internal_relocs = (_bfd_elf_link_read_relocs
  2339. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  2340. link_info->keep_memory));
  2341. if (internal_relocs == NULL)
  2342. return FALSE;
  2343. htab = elf_i386_hash_table (link_info);
  2344. changed_contents = FALSE;
  2345. changed_relocs = FALSE;
  2346. local_got_refcounts = elf_local_got_refcounts (abfd);
  2347. /* Get the section contents. */
  2348. if (elf_section_data (sec)->this_hdr.contents != NULL)
  2349. contents = elf_section_data (sec)->this_hdr.contents;
  2350. else
  2351. {
  2352. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  2353. goto error_return;
  2354. }
  2355. irelend = internal_relocs + sec->reloc_count;
  2356. for (irel = internal_relocs; irel < irelend; irel++)
  2357. {
  2358. unsigned int r_type = ELF32_R_TYPE (irel->r_info);
  2359. unsigned int r_symndx = ELF32_R_SYM (irel->r_info);
  2360. unsigned int indx;
  2361. struct elf_link_hash_entry *h;
  2362. if (r_type != R_386_GOT32)
  2363. continue;
  2364. /* Get the symbol referred to by the reloc. */
  2365. if (r_symndx < symtab_hdr->sh_info)
  2366. {
  2367. Elf_Internal_Sym *isym;
  2368. isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  2369. abfd, r_symndx);
  2370. /* STT_GNU_IFUNC must keep R_386_GOT32 relocation. */
  2371. if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
  2372. && irel->r_offset >= 2
  2373. && bfd_get_8 (abfd, contents + irel->r_offset - 2) == 0x8b)
  2374. {
  2375. bfd_put_8 (abfd, 0x8d, contents + irel->r_offset - 2);
  2376. irel->r_info = ELF32_R_INFO (r_symndx, R_386_GOTOFF);
  2377. if (local_got_refcounts != NULL
  2378. && local_got_refcounts[r_symndx] > 0)
  2379. local_got_refcounts[r_symndx] -= 1;
  2380. changed_contents = TRUE;
  2381. changed_relocs = TRUE;
  2382. }
  2383. continue;
  2384. }
  2385. indx = r_symndx - symtab_hdr->sh_info;
  2386. h = elf_sym_hashes (abfd)[indx];
  2387. BFD_ASSERT (h != NULL);
  2388. while (h->root.type == bfd_link_hash_indirect
  2389. || h->root.type == bfd_link_hash_warning)
  2390. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2391. /* STT_GNU_IFUNC must keep R_386_GOT32 relocation. We also avoid
  2392. optimizing _DYNAMIC since ld.so may use its link-time address. */
  2393. if ((h->root.type == bfd_link_hash_defined
  2394. || h->root.type == bfd_link_hash_defweak)
  2395. && h->type != STT_GNU_IFUNC
  2396. && h != htab->elf.hdynamic
  2397. && SYMBOL_REFERENCES_LOCAL (link_info, h)
  2398. && irel->r_offset >= 2
  2399. && bfd_get_8 (abfd, contents + irel->r_offset - 2) == 0x8b)
  2400. {
  2401. bfd_put_8 (abfd, 0x8d, contents + irel->r_offset - 2);
  2402. irel->r_info = ELF32_R_INFO (r_symndx, R_386_GOTOFF);
  2403. if (h->got.refcount > 0)
  2404. h->got.refcount -= 1;
  2405. changed_contents = TRUE;
  2406. changed_relocs = TRUE;
  2407. }
  2408. }
  2409. if (contents != NULL
  2410. && elf_section_data (sec)->this_hdr.contents != contents)
  2411. {
  2412. if (!changed_contents && !link_info->keep_memory)
  2413. free (contents);
  2414. else
  2415. {
  2416. /* Cache the section contents for elf_link_input_bfd. */
  2417. elf_section_data (sec)->this_hdr.contents = contents;
  2418. }
  2419. }
  2420. if (elf_section_data (sec)->relocs != internal_relocs)
  2421. {
  2422. if (!changed_relocs)
  2423. free (internal_relocs);
  2424. else
  2425. elf_section_data (sec)->relocs = internal_relocs;
  2426. }
  2427. return TRUE;
  2428. error_return:
  2429. if (contents != NULL
  2430. && elf_section_data (sec)->this_hdr.contents != contents)
  2431. free (contents);
  2432. if (internal_relocs != NULL
  2433. && elf_section_data (sec)->relocs != internal_relocs)
  2434. free (internal_relocs);
  2435. return FALSE;
  2436. }
  2437. /* Set the sizes of the dynamic sections. */
  2438. static bfd_boolean
  2439. elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
  2440. {
  2441. struct elf_i386_link_hash_table *htab;
  2442. bfd *dynobj;
  2443. asection *s;
  2444. bfd_boolean relocs;
  2445. bfd *ibfd;
  2446. htab = elf_i386_hash_table (info);
  2447. if (htab == NULL)
  2448. return FALSE;
  2449. dynobj = htab->elf.dynobj;
  2450. if (dynobj == NULL)
  2451. abort ();
  2452. if (htab->elf.dynamic_sections_created)
  2453. {
  2454. /* Set the contents of the .interp section to the interpreter. */
  2455. if (bfd_link_executable (info) && !info->nointerp)
  2456. {
  2457. s = bfd_get_linker_section (dynobj, ".interp");
  2458. if (s == NULL)
  2459. abort ();
  2460. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  2461. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  2462. }
  2463. }
  2464. /* Set up .got offsets for local syms, and space for local dynamic
  2465. relocs. */
  2466. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  2467. {
  2468. bfd_signed_vma *local_got;
  2469. bfd_signed_vma *end_local_got;
  2470. char *local_tls_type;
  2471. bfd_vma *local_tlsdesc_gotent;
  2472. bfd_size_type locsymcount;
  2473. Elf_Internal_Shdr *symtab_hdr;
  2474. asection *srel;
  2475. if (! is_i386_elf (ibfd))
  2476. continue;
  2477. for (s = ibfd->sections; s != NULL; s = s->next)
  2478. {
  2479. struct elf_dyn_relocs *p;
  2480. if (!elf_i386_convert_mov_to_lea (ibfd, s, info))
  2481. return FALSE;
  2482. for (p = ((struct elf_dyn_relocs *)
  2483. elf_section_data (s)->local_dynrel);
  2484. p != NULL;
  2485. p = p->next)
  2486. {
  2487. if (!bfd_is_abs_section (p->sec)
  2488. && bfd_is_abs_section (p->sec->output_section))
  2489. {
  2490. /* Input section has been discarded, either because
  2491. it is a copy of a linkonce section or due to
  2492. linker script /DISCARD/, so we'll be discarding
  2493. the relocs too. */
  2494. }
  2495. else if (get_elf_i386_backend_data (output_bfd)->is_vxworks
  2496. && strcmp (p->sec->output_section->name,
  2497. ".tls_vars") == 0)
  2498. {
  2499. /* Relocations in vxworks .tls_vars sections are
  2500. handled specially by the loader. */
  2501. }
  2502. else if (p->count != 0)
  2503. {
  2504. srel = elf_section_data (p->sec)->sreloc;
  2505. srel->size += p->count * sizeof (Elf32_External_Rel);
  2506. if ((p->sec->output_section->flags & SEC_READONLY) != 0
  2507. && (info->flags & DF_TEXTREL) == 0)
  2508. {
  2509. info->flags |= DF_TEXTREL;
  2510. if ((info->warn_shared_textrel && bfd_link_pic (info))
  2511. || info->error_textrel)
  2512. info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
  2513. p->sec->owner, p->sec);
  2514. }
  2515. }
  2516. }
  2517. }
  2518. local_got = elf_local_got_refcounts (ibfd);
  2519. if (!local_got)
  2520. continue;
  2521. symtab_hdr = &elf_symtab_hdr (ibfd);
  2522. locsymcount = symtab_hdr->sh_info;
  2523. end_local_got = local_got + locsymcount;
  2524. local_tls_type = elf_i386_local_got_tls_type (ibfd);
  2525. local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
  2526. s = htab->elf.sgot;
  2527. srel = htab->elf.srelgot;
  2528. for (; local_got < end_local_got;
  2529. ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
  2530. {
  2531. *local_tlsdesc_gotent = (bfd_vma) -1;
  2532. if (*local_got > 0)
  2533. {
  2534. if (GOT_TLS_GDESC_P (*local_tls_type))
  2535. {
  2536. *local_tlsdesc_gotent = htab->elf.sgotplt->size
  2537. - elf_i386_compute_jump_table_size (htab);
  2538. htab->elf.sgotplt->size += 8;
  2539. *local_got = (bfd_vma) -2;
  2540. }
  2541. if (! GOT_TLS_GDESC_P (*local_tls_type)
  2542. || GOT_TLS_GD_P (*local_tls_type))
  2543. {
  2544. *local_got = s->size;
  2545. s->size += 4;
  2546. if (GOT_TLS_GD_P (*local_tls_type)
  2547. || *local_tls_type == GOT_TLS_IE_BOTH)
  2548. s->size += 4;
  2549. }
  2550. if (bfd_link_pic (info)
  2551. || GOT_TLS_GD_ANY_P (*local_tls_type)
  2552. || (*local_tls_type & GOT_TLS_IE))
  2553. {
  2554. if (*local_tls_type == GOT_TLS_IE_BOTH)
  2555. srel->size += 2 * sizeof (Elf32_External_Rel);
  2556. else if (GOT_TLS_GD_P (*local_tls_type)
  2557. || ! GOT_TLS_GDESC_P (*local_tls_type))
  2558. srel->size += sizeof (Elf32_External_Rel);
  2559. if (GOT_TLS_GDESC_P (*local_tls_type))
  2560. htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
  2561. }
  2562. }
  2563. else
  2564. *local_got = (bfd_vma) -1;
  2565. }
  2566. }
  2567. if (htab->tls_ldm_got.refcount > 0)
  2568. {
  2569. /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
  2570. relocs. */
  2571. htab->tls_ldm_got.offset = htab->elf.sgot->size;
  2572. htab->elf.sgot->size += 8;
  2573. htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
  2574. }
  2575. else
  2576. htab->tls_ldm_got.offset = -1;
  2577. /* Allocate global sym .plt and .got entries, and space for global
  2578. sym dynamic relocs. */
  2579. elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
  2580. /* Allocate .plt and .got entries, and space for local symbols. */
  2581. htab_traverse (htab->loc_hash_table,
  2582. elf_i386_allocate_local_dynrelocs,
  2583. info);
  2584. /* For every jump slot reserved in the sgotplt, reloc_count is
  2585. incremented. However, when we reserve space for TLS descriptors,
  2586. it's not incremented, so in order to compute the space reserved
  2587. for them, it suffices to multiply the reloc count by the jump
  2588. slot size.
  2589. PR ld/13302: We start next_irelative_index at the end of .rela.plt
  2590. so that R_386_IRELATIVE entries come last. */
  2591. if (htab->elf.srelplt)
  2592. {
  2593. htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
  2594. htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
  2595. htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
  2596. }
  2597. else if (htab->elf.irelplt)
  2598. htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
  2599. if (htab->elf.sgotplt)
  2600. {
  2601. /* Don't allocate .got.plt section if there are no GOT nor PLT
  2602. entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
  2603. if ((htab->elf.hgot == NULL
  2604. || !htab->elf.hgot->ref_regular_nonweak)
  2605. && (htab->elf.sgotplt->size
  2606. == get_elf_backend_data (output_bfd)->got_header_size)
  2607. && (htab->elf.splt == NULL
  2608. || htab->elf.splt->size == 0)
  2609. && (htab->elf.sgot == NULL
  2610. || htab->elf.sgot->size == 0)
  2611. && (htab->elf.iplt == NULL
  2612. || htab->elf.iplt->size == 0)
  2613. && (htab->elf.igotplt == NULL
  2614. || htab->elf.igotplt->size == 0))
  2615. htab->elf.sgotplt->size = 0;
  2616. }
  2617. if (htab->plt_eh_frame != NULL
  2618. && htab->elf.splt != NULL
  2619. && htab->elf.splt->size != 0
  2620. && !bfd_is_abs_section (htab->elf.splt->output_section)
  2621. && _bfd_elf_eh_frame_present (info))
  2622. htab->plt_eh_frame->size = sizeof (elf_i386_eh_frame_plt);
  2623. /* We now have determined the sizes of the various dynamic sections.
  2624. Allocate memory for them. */
  2625. relocs = FALSE;
  2626. for (s = dynobj->sections; s != NULL; s = s->next)
  2627. {
  2628. bfd_boolean strip_section = TRUE;
  2629. if ((s->flags & SEC_LINKER_CREATED) == 0)
  2630. continue;
  2631. if (s == htab->elf.splt
  2632. || s == htab->elf.sgot)
  2633. {
  2634. /* Strip this section if we don't need it; see the
  2635. comment below. */
  2636. /* We'd like to strip these sections if they aren't needed, but if
  2637. we've exported dynamic symbols from them we must leave them.
  2638. It's too late to tell BFD to get rid of the symbols. */
  2639. if (htab->elf.hplt != NULL)
  2640. strip_section = FALSE;
  2641. }
  2642. else if (s == htab->elf.sgotplt
  2643. || s == htab->elf.iplt
  2644. || s == htab->elf.igotplt
  2645. || s == htab->plt_got
  2646. || s == htab->plt_eh_frame
  2647. || s == htab->sdynbss)
  2648. {
  2649. /* Strip these too. */
  2650. }
  2651. else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
  2652. {
  2653. if (s->size != 0
  2654. && s != htab->elf.srelplt
  2655. && s != htab->srelplt2)
  2656. relocs = TRUE;
  2657. /* We use the reloc_count field as a counter if we need
  2658. to copy relocs into the output file. */
  2659. s->reloc_count = 0;
  2660. }
  2661. else
  2662. {
  2663. /* It's not one of our sections, so don't allocate space. */
  2664. continue;
  2665. }
  2666. if (s->size == 0)
  2667. {
  2668. /* If we don't need this section, strip it from the
  2669. output file. This is mostly to handle .rel.bss and
  2670. .rel.plt. We must create both sections in
  2671. create_dynamic_sections, because they must be created
  2672. before the linker maps input sections to output
  2673. sections. The linker does that before
  2674. adjust_dynamic_symbol is called, and it is that
  2675. function which decides whether anything needs to go
  2676. into these sections. */
  2677. if (strip_section)
  2678. s->flags |= SEC_EXCLUDE;
  2679. continue;
  2680. }
  2681. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  2682. continue;
  2683. /* Allocate memory for the section contents. We use bfd_zalloc
  2684. here in case unused entries are not reclaimed before the
  2685. section's contents are written out. This should not happen,
  2686. but this way if it does, we get a R_386_NONE reloc instead
  2687. of garbage. */
  2688. s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
  2689. if (s->contents == NULL)
  2690. return FALSE;
  2691. }
  2692. if (htab->plt_eh_frame != NULL
  2693. && htab->plt_eh_frame->contents != NULL)
  2694. {
  2695. memcpy (htab->plt_eh_frame->contents, elf_i386_eh_frame_plt,
  2696. sizeof (elf_i386_eh_frame_plt));
  2697. bfd_put_32 (dynobj, htab->elf.splt->size,
  2698. htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
  2699. }
  2700. if (htab->elf.dynamic_sections_created)
  2701. {
  2702. /* Add some entries to the .dynamic section. We fill in the
  2703. values later, in elf_i386_finish_dynamic_sections, but we
  2704. must add the entries now so that we get the correct size for
  2705. the .dynamic section. The DT_DEBUG entry is filled in by the
  2706. dynamic linker and used by the debugger. */
  2707. #define add_dynamic_entry(TAG, VAL) \
  2708. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  2709. if (bfd_link_executable (info))
  2710. {
  2711. if (!add_dynamic_entry (DT_DEBUG, 0))
  2712. return FALSE;
  2713. }
  2714. if (htab->elf.splt->size != 0)
  2715. {
  2716. /* DT_PLTGOT is used by prelink even if there is no PLT
  2717. relocation. */
  2718. if (!add_dynamic_entry (DT_PLTGOT, 0))
  2719. return FALSE;
  2720. if (htab->elf.srelplt->size != 0)
  2721. {
  2722. if (!add_dynamic_entry (DT_PLTRELSZ, 0)
  2723. || !add_dynamic_entry (DT_PLTREL, DT_REL)
  2724. || !add_dynamic_entry (DT_JMPREL, 0))
  2725. return FALSE;
  2726. }
  2727. }
  2728. if (relocs)
  2729. {
  2730. if (!add_dynamic_entry (DT_REL, 0)
  2731. || !add_dynamic_entry (DT_RELSZ, 0)
  2732. || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
  2733. return FALSE;
  2734. /* If any dynamic relocs apply to a read-only section,
  2735. then we need a DT_TEXTREL entry. */
  2736. if ((info->flags & DF_TEXTREL) == 0)
  2737. elf_link_hash_traverse (&htab->elf,
  2738. elf_i386_readonly_dynrelocs, info);
  2739. if ((info->flags & DF_TEXTREL) != 0)
  2740. {
  2741. if ((elf_tdata (output_bfd)->has_gnu_symbols
  2742. & elf_gnu_symbol_ifunc) == elf_gnu_symbol_ifunc)
  2743. {
  2744. info->callbacks->einfo
  2745. (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
  2746. bfd_set_error (bfd_error_bad_value);
  2747. return FALSE;
  2748. }
  2749. if (!add_dynamic_entry (DT_TEXTREL, 0))
  2750. return FALSE;
  2751. }
  2752. }
  2753. if (get_elf_i386_backend_data (output_bfd)->is_vxworks
  2754. && !elf_vxworks_add_dynamic_entries (output_bfd, info))
  2755. return FALSE;
  2756. }
  2757. #undef add_dynamic_entry
  2758. return TRUE;
  2759. }
  2760. static bfd_boolean
  2761. elf_i386_always_size_sections (bfd *output_bfd,
  2762. struct bfd_link_info *info)
  2763. {
  2764. asection *tls_sec = elf_hash_table (info)->tls_sec;
  2765. if (tls_sec)
  2766. {
  2767. struct elf_link_hash_entry *tlsbase;
  2768. tlsbase = elf_link_hash_lookup (elf_hash_table (info),
  2769. "_TLS_MODULE_BASE_",
  2770. FALSE, FALSE, FALSE);
  2771. if (tlsbase && tlsbase->type == STT_TLS)
  2772. {
  2773. struct elf_i386_link_hash_table *htab;
  2774. struct bfd_link_hash_entry *bh = NULL;
  2775. const struct elf_backend_data *bed
  2776. = get_elf_backend_data (output_bfd);
  2777. htab = elf_i386_hash_table (info);
  2778. if (htab == NULL)
  2779. return FALSE;
  2780. if (!(_bfd_generic_link_add_one_symbol
  2781. (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
  2782. tls_sec, 0, NULL, FALSE,
  2783. bed->collect, &bh)))
  2784. return FALSE;
  2785. htab->tls_module_base = bh;
  2786. tlsbase = (struct elf_link_hash_entry *)bh;
  2787. tlsbase->def_regular = 1;
  2788. tlsbase->other = STV_HIDDEN;
  2789. tlsbase->root.linker_def = 1;
  2790. (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
  2791. }
  2792. }
  2793. return TRUE;
  2794. }
  2795. /* Set the correct type for an x86 ELF section. We do this by the
  2796. section name, which is a hack, but ought to work. */
  2797. static bfd_boolean
  2798. elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
  2799. Elf_Internal_Shdr *hdr,
  2800. asection *sec)
  2801. {
  2802. const char *name;
  2803. name = bfd_get_section_name (abfd, sec);
  2804. /* This is an ugly, but unfortunately necessary hack that is
  2805. needed when producing EFI binaries on x86. It tells
  2806. elf.c:elf_fake_sections() not to consider ".reloc" as a section
  2807. containing ELF relocation info. We need this hack in order to
  2808. be able to generate ELF binaries that can be translated into
  2809. EFI applications (which are essentially COFF objects). Those
  2810. files contain a COFF ".reloc" section inside an ELFNN object,
  2811. which would normally cause BFD to segfault because it would
  2812. attempt to interpret this section as containing relocation
  2813. entries for section "oc". With this hack enabled, ".reloc"
  2814. will be treated as a normal data section, which will avoid the
  2815. segfault. However, you won't be able to create an ELFNN binary
  2816. with a section named "oc" that needs relocations, but that's
  2817. the kind of ugly side-effects you get when detecting section
  2818. types based on their names... In practice, this limitation is
  2819. unlikely to bite. */
  2820. if (strcmp (name, ".reloc") == 0)
  2821. hdr->sh_type = SHT_PROGBITS;
  2822. return TRUE;
  2823. }
  2824. /* _TLS_MODULE_BASE_ needs to be treated especially when linking
  2825. executables. Rather than setting it to the beginning of the TLS
  2826. section, we have to set it to the end. This function may be called
  2827. multiple times, it is idempotent. */
  2828. static void
  2829. elf_i386_set_tls_module_base (struct bfd_link_info *info)
  2830. {
  2831. struct elf_i386_link_hash_table *htab;
  2832. struct bfd_link_hash_entry *base;
  2833. if (!bfd_link_executable (info))
  2834. return;
  2835. htab = elf_i386_hash_table (info);
  2836. if (htab == NULL)
  2837. return;
  2838. base = htab->tls_module_base;
  2839. if (base == NULL)
  2840. return;
  2841. base->u.def.value = htab->elf.tls_size;
  2842. }
  2843. /* Return the base VMA address which should be subtracted from real addresses
  2844. when resolving @dtpoff relocation.
  2845. This is PT_TLS segment p_vaddr. */
  2846. static bfd_vma
  2847. elf_i386_dtpoff_base (struct bfd_link_info *info)
  2848. {
  2849. /* If tls_sec is NULL, we should have signalled an error already. */
  2850. if (elf_hash_table (info)->tls_sec == NULL)
  2851. return 0;
  2852. return elf_hash_table (info)->tls_sec->vma;
  2853. }
  2854. /* Return the relocation value for @tpoff relocation
  2855. if STT_TLS virtual address is ADDRESS. */
  2856. static bfd_vma
  2857. elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
  2858. {
  2859. struct elf_link_hash_table *htab = elf_hash_table (info);
  2860. const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
  2861. bfd_vma static_tls_size;
  2862. /* If tls_sec is NULL, we should have signalled an error already. */
  2863. if (htab->tls_sec == NULL)
  2864. return 0;
  2865. /* Consider special static TLS alignment requirements. */
  2866. static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
  2867. return static_tls_size + htab->tls_sec->vma - address;
  2868. }
  2869. /* Relocate an i386 ELF section. */
  2870. static bfd_boolean
  2871. elf_i386_relocate_section (bfd *output_bfd,
  2872. struct bfd_link_info *info,
  2873. bfd *input_bfd,
  2874. asection *input_section,
  2875. bfd_byte *contents,
  2876. Elf_Internal_Rela *relocs,
  2877. Elf_Internal_Sym *local_syms,
  2878. asection **local_sections)
  2879. {
  2880. struct elf_i386_link_hash_table *htab;
  2881. Elf_Internal_Shdr *symtab_hdr;
  2882. struct elf_link_hash_entry **sym_hashes;
  2883. bfd_vma *local_got_offsets;
  2884. bfd_vma *local_tlsdesc_gotents;
  2885. Elf_Internal_Rela *rel;
  2886. Elf_Internal_Rela *relend;
  2887. bfd_boolean is_vxworks_tls;
  2888. unsigned plt_entry_size;
  2889. BFD_ASSERT (is_i386_elf (input_bfd));
  2890. htab = elf_i386_hash_table (info);
  2891. if (htab == NULL)
  2892. return FALSE;
  2893. symtab_hdr = &elf_symtab_hdr (input_bfd);
  2894. sym_hashes = elf_sym_hashes (input_bfd);
  2895. local_got_offsets = elf_local_got_offsets (input_bfd);
  2896. local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
  2897. /* We have to handle relocations in vxworks .tls_vars sections
  2898. specially, because the dynamic loader is 'weird'. */
  2899. is_vxworks_tls = (get_elf_i386_backend_data (output_bfd)->is_vxworks
  2900. && bfd_link_pic (info)
  2901. && !strcmp (input_section->output_section->name,
  2902. ".tls_vars"));
  2903. elf_i386_set_tls_module_base (info);
  2904. plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
  2905. rel = relocs;
  2906. relend = relocs + input_section->reloc_count;
  2907. for (; rel < relend; rel++)
  2908. {
  2909. unsigned int r_type;
  2910. reloc_howto_type *howto;
  2911. unsigned long r_symndx;
  2912. struct elf_link_hash_entry *h;
  2913. struct elf_i386_link_hash_entry *eh;
  2914. Elf_Internal_Sym *sym;
  2915. asection *sec;
  2916. bfd_vma off, offplt, plt_offset;
  2917. bfd_vma relocation;
  2918. bfd_boolean unresolved_reloc;
  2919. bfd_reloc_status_type r;
  2920. unsigned int indx;
  2921. int tls_type;
  2922. bfd_vma st_size;
  2923. asection *resolved_plt;
  2924. r_type = ELF32_R_TYPE (rel->r_info);
  2925. if (r_type == R_386_GNU_VTINHERIT
  2926. || r_type == R_386_GNU_VTENTRY)
  2927. continue;
  2928. if ((indx = r_type) >= R_386_standard
  2929. && ((indx = r_type - R_386_ext_offset) - R_386_standard
  2930. >= R_386_ext - R_386_standard)
  2931. && ((indx = r_type - R_386_tls_offset) - R_386_ext
  2932. >= R_386_ext2 - R_386_ext))
  2933. {
  2934. (*_bfd_error_handler)
  2935. (_("%B: unrecognized relocation (0x%x) in section `%A'"),
  2936. input_bfd, input_section, r_type);
  2937. bfd_set_error (bfd_error_bad_value);
  2938. return FALSE;
  2939. }
  2940. howto = elf_howto_table + indx;
  2941. r_symndx = ELF32_R_SYM (rel->r_info);
  2942. h = NULL;
  2943. sym = NULL;
  2944. sec = NULL;
  2945. unresolved_reloc = FALSE;
  2946. if (r_symndx < symtab_hdr->sh_info)
  2947. {
  2948. sym = local_syms + r_symndx;
  2949. sec = local_sections[r_symndx];
  2950. relocation = (sec->output_section->vma
  2951. + sec->output_offset
  2952. + sym->st_value);
  2953. st_size = sym->st_size;
  2954. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
  2955. && ((sec->flags & SEC_MERGE) != 0
  2956. || (bfd_link_relocatable (info)
  2957. && sec->output_offset != 0)))
  2958. {
  2959. bfd_vma addend;
  2960. bfd_byte *where = contents + rel->r_offset;
  2961. switch (howto->size)
  2962. {
  2963. case 0:
  2964. addend = bfd_get_8 (input_bfd, where);
  2965. if (howto->pc_relative)
  2966. {
  2967. addend = (addend ^ 0x80) - 0x80;
  2968. addend += 1;
  2969. }
  2970. break;
  2971. case 1:
  2972. addend = bfd_get_16 (input_bfd, where);
  2973. if (howto->pc_relative)
  2974. {
  2975. addend = (addend ^ 0x8000) - 0x8000;
  2976. addend += 2;
  2977. }
  2978. break;
  2979. case 2:
  2980. addend = bfd_get_32 (input_bfd, where);
  2981. if (howto->pc_relative)
  2982. {
  2983. addend = (addend ^ 0x80000000) - 0x80000000;
  2984. addend += 4;
  2985. }
  2986. break;
  2987. default:
  2988. abort ();
  2989. }
  2990. if (bfd_link_relocatable (info))
  2991. addend += sec->output_offset;
  2992. else
  2993. {
  2994. asection *msec = sec;
  2995. addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
  2996. addend);
  2997. addend -= relocation;
  2998. addend += msec->output_section->vma + msec->output_offset;
  2999. }
  3000. switch (howto->size)
  3001. {
  3002. case 0:
  3003. /* FIXME: overflow checks. */
  3004. if (howto->pc_relative)
  3005. addend -= 1;
  3006. bfd_put_8 (input_bfd, addend, where);
  3007. break;
  3008. case 1:
  3009. if (howto->pc_relative)
  3010. addend -= 2;
  3011. bfd_put_16 (input_bfd, addend, where);
  3012. break;
  3013. case 2:
  3014. if (howto->pc_relative)
  3015. addend -= 4;
  3016. bfd_put_32 (input_bfd, addend, where);
  3017. break;
  3018. }
  3019. }
  3020. else if (!bfd_link_relocatable (info)
  3021. && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  3022. {
  3023. /* Relocate against local STT_GNU_IFUNC symbol. */
  3024. h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
  3025. FALSE);
  3026. if (h == NULL)
  3027. abort ();
  3028. /* Set STT_GNU_IFUNC symbol value. */
  3029. h->root.u.def.value = sym->st_value;
  3030. h->root.u.def.section = sec;
  3031. }
  3032. }
  3033. else
  3034. {
  3035. bfd_boolean warned ATTRIBUTE_UNUSED;
  3036. bfd_boolean ignored ATTRIBUTE_UNUSED;
  3037. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  3038. r_symndx, symtab_hdr, sym_hashes,
  3039. h, sec, relocation,
  3040. unresolved_reloc, warned, ignored);
  3041. st_size = h->size;
  3042. }
  3043. if (sec != NULL && discarded_section (sec))
  3044. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3045. rel, 1, relend, howto, 0, contents);
  3046. if (bfd_link_relocatable (info))
  3047. continue;
  3048. /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
  3049. it here if it is defined in a non-shared object. */
  3050. if (h != NULL
  3051. && h->type == STT_GNU_IFUNC
  3052. && h->def_regular)
  3053. {
  3054. asection *plt, *gotplt, *base_got;
  3055. bfd_vma plt_index;
  3056. const char *name;
  3057. if ((input_section->flags & SEC_ALLOC) == 0)
  3058. {
  3059. /* Dynamic relocs are not propagated for SEC_DEBUGGING
  3060. sections because such sections are not SEC_ALLOC and
  3061. thus ld.so will not process them. */
  3062. if ((input_section->flags & SEC_DEBUGGING) != 0)
  3063. continue;
  3064. abort ();
  3065. }
  3066. else if (h->plt.offset == (bfd_vma) -1)
  3067. abort ();
  3068. /* STT_GNU_IFUNC symbol must go through PLT. */
  3069. if (htab->elf.splt != NULL)
  3070. {
  3071. plt = htab->elf.splt;
  3072. gotplt = htab->elf.sgotplt;
  3073. }
  3074. else
  3075. {
  3076. plt = htab->elf.iplt;
  3077. gotplt = htab->elf.igotplt;
  3078. }
  3079. relocation = (plt->output_section->vma
  3080. + plt->output_offset + h->plt.offset);
  3081. switch (r_type)
  3082. {
  3083. default:
  3084. if (h->root.root.string)
  3085. name = h->root.root.string;
  3086. else
  3087. name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
  3088. NULL);
  3089. (*_bfd_error_handler)
  3090. (_("%B: relocation %s against STT_GNU_IFUNC "
  3091. "symbol `%s' isn't handled by %s"), input_bfd,
  3092. elf_howto_table[r_type].name,
  3093. name, __FUNCTION__);
  3094. bfd_set_error (bfd_error_bad_value);
  3095. return FALSE;
  3096. case R_386_32:
  3097. /* Generate dynamic relcoation only when there is a
  3098. non-GOT reference in a shared object. */
  3099. if (bfd_link_pic (info) && h->non_got_ref)
  3100. {
  3101. Elf_Internal_Rela outrel;
  3102. asection *sreloc;
  3103. bfd_vma offset;
  3104. /* Need a dynamic relocation to get the real function
  3105. adddress. */
  3106. offset = _bfd_elf_section_offset (output_bfd,
  3107. info,
  3108. input_section,
  3109. rel->r_offset);
  3110. if (offset == (bfd_vma) -1
  3111. || offset == (bfd_vma) -2)
  3112. abort ();
  3113. outrel.r_offset = (input_section->output_section->vma
  3114. + input_section->output_offset
  3115. + offset);
  3116. if (h->dynindx == -1
  3117. || h->forced_local
  3118. || bfd_link_executable (info))
  3119. {
  3120. /* This symbol is resolved locally. */
  3121. outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
  3122. bfd_put_32 (output_bfd,
  3123. (h->root.u.def.value
  3124. + h->root.u.def.section->output_section->vma
  3125. + h->root.u.def.section->output_offset),
  3126. contents + offset);
  3127. }
  3128. else
  3129. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  3130. sreloc = htab->elf.irelifunc;
  3131. elf_append_rel (output_bfd, sreloc, &outrel);
  3132. /* If this reloc is against an external symbol, we
  3133. do not want to fiddle with the addend. Otherwise,
  3134. we need to include the symbol value so that it
  3135. becomes an addend for the dynamic reloc. For an
  3136. internal symbol, we have updated addend. */
  3137. continue;
  3138. }
  3139. /* FALLTHROUGH */
  3140. case R_386_PC32:
  3141. case R_386_PLT32:
  3142. goto do_relocation;
  3143. case R_386_GOT32:
  3144. base_got = htab->elf.sgot;
  3145. off = h->got.offset;
  3146. if (base_got == NULL)
  3147. abort ();
  3148. if (off == (bfd_vma) -1)
  3149. {
  3150. /* We can't use h->got.offset here to save state, or
  3151. even just remember the offset, as finish_dynamic_symbol
  3152. would use that as offset into .got. */
  3153. if (htab->elf.splt != NULL)
  3154. {
  3155. plt_index = h->plt.offset / plt_entry_size - 1;
  3156. off = (plt_index + 3) * 4;
  3157. base_got = htab->elf.sgotplt;
  3158. }
  3159. else
  3160. {
  3161. plt_index = h->plt.offset / plt_entry_size;
  3162. off = plt_index * 4;
  3163. base_got = htab->elf.igotplt;
  3164. }
  3165. if (h->dynindx == -1
  3166. || h->forced_local
  3167. || info->symbolic)
  3168. {
  3169. /* This references the local defitionion. We must
  3170. initialize this entry in the global offset table.
  3171. Since the offset must always be a multiple of 8,
  3172. we use the least significant bit to record
  3173. whether we have initialized it already.
  3174. When doing a dynamic link, we create a .rela.got
  3175. relocation entry to initialize the value. This
  3176. is done in the finish_dynamic_symbol routine. */
  3177. if ((off & 1) != 0)
  3178. off &= ~1;
  3179. else
  3180. {
  3181. bfd_put_32 (output_bfd, relocation,
  3182. base_got->contents + off);
  3183. h->got.offset |= 1;
  3184. }
  3185. }
  3186. relocation = off;
  3187. /* Adjust for static executables. */
  3188. if (htab->elf.splt == NULL)
  3189. relocation += gotplt->output_offset;
  3190. }
  3191. else
  3192. {
  3193. relocation = (base_got->output_section->vma
  3194. + base_got->output_offset + off
  3195. - gotplt->output_section->vma
  3196. - gotplt->output_offset);
  3197. /* Adjust for static executables. */
  3198. if (htab->elf.splt == NULL)
  3199. relocation += gotplt->output_offset;
  3200. }
  3201. goto do_relocation;
  3202. case R_386_GOTOFF:
  3203. relocation -= (gotplt->output_section->vma
  3204. + gotplt->output_offset);
  3205. goto do_relocation;
  3206. }
  3207. }
  3208. eh = (struct elf_i386_link_hash_entry *) h;
  3209. switch (r_type)
  3210. {
  3211. case R_386_GOT32:
  3212. /* Relocation is to the entry for this symbol in the global
  3213. offset table. */
  3214. if (htab->elf.sgot == NULL)
  3215. abort ();
  3216. if (h != NULL)
  3217. {
  3218. bfd_boolean dyn;
  3219. off = h->got.offset;
  3220. dyn = htab->elf.dynamic_sections_created;
  3221. if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  3222. bfd_link_pic (info),
  3223. h)
  3224. || (bfd_link_pic (info)
  3225. && SYMBOL_REFERENCES_LOCAL (info, h))
  3226. || (ELF_ST_VISIBILITY (h->other)
  3227. && h->root.type == bfd_link_hash_undefweak))
  3228. {
  3229. /* This is actually a static link, or it is a
  3230. -Bsymbolic link and the symbol is defined
  3231. locally, or the symbol was forced to be local
  3232. because of a version file. We must initialize
  3233. this entry in the global offset table. Since the
  3234. offset must always be a multiple of 4, we use the
  3235. least significant bit to record whether we have
  3236. initialized it already.
  3237. When doing a dynamic link, we create a .rel.got
  3238. relocation entry to initialize the value. This
  3239. is done in the finish_dynamic_symbol routine. */
  3240. if ((off & 1) != 0)
  3241. off &= ~1;
  3242. else
  3243. {
  3244. bfd_put_32 (output_bfd, relocation,
  3245. htab->elf.sgot->contents + off);
  3246. h->got.offset |= 1;
  3247. }
  3248. }
  3249. else
  3250. unresolved_reloc = FALSE;
  3251. }
  3252. else
  3253. {
  3254. if (local_got_offsets == NULL)
  3255. abort ();
  3256. off = local_got_offsets[r_symndx];
  3257. /* The offset must always be a multiple of 4. We use
  3258. the least significant bit to record whether we have
  3259. already generated the necessary reloc. */
  3260. if ((off & 1) != 0)
  3261. off &= ~1;
  3262. else
  3263. {
  3264. bfd_put_32 (output_bfd, relocation,
  3265. htab->elf.sgot->contents + off);
  3266. if (bfd_link_pic (info))
  3267. {
  3268. asection *s;
  3269. Elf_Internal_Rela outrel;
  3270. s = htab->elf.srelgot;
  3271. if (s == NULL)
  3272. abort ();
  3273. outrel.r_offset = (htab->elf.sgot->output_section->vma
  3274. + htab->elf.sgot->output_offset
  3275. + off);
  3276. outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  3277. elf_append_rel (output_bfd, s, &outrel);
  3278. }
  3279. local_got_offsets[r_symndx] |= 1;
  3280. }
  3281. }
  3282. if (off >= (bfd_vma) -2)
  3283. abort ();
  3284. relocation = htab->elf.sgot->output_section->vma
  3285. + htab->elf.sgot->output_offset + off
  3286. - htab->elf.sgotplt->output_section->vma
  3287. - htab->elf.sgotplt->output_offset;
  3288. break;
  3289. case R_386_GOTOFF:
  3290. /* Relocation is relative to the start of the global offset
  3291. table. */
  3292. /* Check to make sure it isn't a protected function or data
  3293. symbol for shared library since it may not be local when
  3294. used as function address or with copy relocation. We also
  3295. need to make sure that a symbol is referenced locally. */
  3296. if (!bfd_link_executable (info) && h)
  3297. {
  3298. if (!h->def_regular)
  3299. {
  3300. const char *v;
  3301. switch (ELF_ST_VISIBILITY (h->other))
  3302. {
  3303. case STV_HIDDEN:
  3304. v = _("hidden symbol");
  3305. break;
  3306. case STV_INTERNAL:
  3307. v = _("internal symbol");
  3308. break;
  3309. case STV_PROTECTED:
  3310. v = _("protected symbol");
  3311. break;
  3312. default:
  3313. v = _("symbol");
  3314. break;
  3315. }
  3316. (*_bfd_error_handler)
  3317. (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
  3318. input_bfd, v, h->root.root.string);
  3319. bfd_set_error (bfd_error_bad_value);
  3320. return FALSE;
  3321. }
  3322. else if (!SYMBOL_REFERENCES_LOCAL (info, h)
  3323. && (h->type == STT_FUNC
  3324. || h->type == STT_OBJECT)
  3325. && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
  3326. {
  3327. (*_bfd_error_handler)
  3328. (_("%B: relocation R_386_GOTOFF against protected %s `%s' can not be used when making a shared object"),
  3329. input_bfd,
  3330. h->type == STT_FUNC ? "function" : "data",
  3331. h->root.root.string);
  3332. bfd_set_error (bfd_error_bad_value);
  3333. return FALSE;
  3334. }
  3335. }
  3336. /* Note that sgot is not involved in this
  3337. calculation. We always want the start of .got.plt. If we
  3338. defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
  3339. permitted by the ABI, we might have to change this
  3340. calculation. */
  3341. relocation -= htab->elf.sgotplt->output_section->vma
  3342. + htab->elf.sgotplt->output_offset;
  3343. break;
  3344. case R_386_GOTPC:
  3345. /* Use global offset table as symbol value. */
  3346. relocation = htab->elf.sgotplt->output_section->vma
  3347. + htab->elf.sgotplt->output_offset;
  3348. unresolved_reloc = FALSE;
  3349. break;
  3350. case R_386_PLT32:
  3351. /* Relocation is to the entry for this symbol in the
  3352. procedure linkage table. */
  3353. /* Resolve a PLT32 reloc against a local symbol directly,
  3354. without using the procedure linkage table. */
  3355. if (h == NULL)
  3356. break;
  3357. if ((h->plt.offset == (bfd_vma) -1
  3358. && eh->plt_got.offset == (bfd_vma) -1)
  3359. || htab->elf.splt == NULL)
  3360. {
  3361. /* We didn't make a PLT entry for this symbol. This
  3362. happens when statically linking PIC code, or when
  3363. using -Bsymbolic. */
  3364. break;
  3365. }
  3366. if (h->plt.offset != (bfd_vma) -1)
  3367. {
  3368. resolved_plt = htab->elf.splt;
  3369. plt_offset = h->plt.offset;
  3370. }
  3371. else
  3372. {
  3373. resolved_plt = htab->plt_got;
  3374. plt_offset = eh->plt_got.offset;
  3375. }
  3376. relocation = (resolved_plt->output_section->vma
  3377. + resolved_plt->output_offset
  3378. + plt_offset);
  3379. unresolved_reloc = FALSE;
  3380. break;
  3381. case R_386_SIZE32:
  3382. /* Set to symbol size. */
  3383. relocation = st_size;
  3384. /* Fall through. */
  3385. case R_386_32:
  3386. case R_386_PC32:
  3387. if ((input_section->flags & SEC_ALLOC) == 0
  3388. || is_vxworks_tls)
  3389. break;
  3390. /* Copy dynamic function pointer relocations. */
  3391. if ((bfd_link_pic (info)
  3392. && (h == NULL
  3393. || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  3394. || h->root.type != bfd_link_hash_undefweak)
  3395. && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
  3396. || !SYMBOL_CALLS_LOCAL (info, h)))
  3397. || (ELIMINATE_COPY_RELOCS
  3398. && !bfd_link_pic (info)
  3399. && h != NULL
  3400. && h->dynindx != -1
  3401. && (!h->non_got_ref || eh->func_pointer_refcount > 0)
  3402. && ((h->def_dynamic
  3403. && !h->def_regular)
  3404. || h->root.type == bfd_link_hash_undefweak
  3405. || h->root.type == bfd_link_hash_undefined)))
  3406. {
  3407. Elf_Internal_Rela outrel;
  3408. bfd_boolean skip, relocate;
  3409. asection *sreloc;
  3410. /* When generating a shared object, these relocations
  3411. are copied into the output file to be resolved at run
  3412. time. */
  3413. skip = FALSE;
  3414. relocate = FALSE;
  3415. outrel.r_offset =
  3416. _bfd_elf_section_offset (output_bfd, info, input_section,
  3417. rel->r_offset);
  3418. if (outrel.r_offset == (bfd_vma) -1)
  3419. skip = TRUE;
  3420. else if (outrel.r_offset == (bfd_vma) -2)
  3421. skip = TRUE, relocate = TRUE;
  3422. outrel.r_offset += (input_section->output_section->vma
  3423. + input_section->output_offset);
  3424. if (skip)
  3425. memset (&outrel, 0, sizeof outrel);
  3426. else if (h != NULL
  3427. && h->dynindx != -1
  3428. && (r_type == R_386_PC32
  3429. || !bfd_link_pic (info)
  3430. || !SYMBOLIC_BIND (info, h)
  3431. || !h->def_regular))
  3432. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  3433. else
  3434. {
  3435. /* This symbol is local, or marked to become local. */
  3436. relocate = TRUE;
  3437. outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  3438. }
  3439. sreloc = elf_section_data (input_section)->sreloc;
  3440. if (sreloc == NULL || sreloc->contents == NULL)
  3441. {
  3442. r = bfd_reloc_notsupported;
  3443. goto check_relocation_error;
  3444. }
  3445. elf_append_rel (output_bfd, sreloc, &outrel);
  3446. /* If this reloc is against an external symbol, we do
  3447. not want to fiddle with the addend. Otherwise, we
  3448. need to include the symbol value so that it becomes
  3449. an addend for the dynamic reloc. */
  3450. if (! relocate)
  3451. continue;
  3452. }
  3453. break;
  3454. case R_386_TLS_IE:
  3455. if (!bfd_link_executable (info))
  3456. {
  3457. Elf_Internal_Rela outrel;
  3458. asection *sreloc;
  3459. outrel.r_offset = rel->r_offset
  3460. + input_section->output_section->vma
  3461. + input_section->output_offset;
  3462. outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  3463. sreloc = elf_section_data (input_section)->sreloc;
  3464. if (sreloc == NULL)
  3465. abort ();
  3466. elf_append_rel (output_bfd, sreloc, &outrel);
  3467. }
  3468. /* Fall through */
  3469. case R_386_TLS_GD:
  3470. case R_386_TLS_GOTDESC:
  3471. case R_386_TLS_DESC_CALL:
  3472. case R_386_TLS_IE_32:
  3473. case R_386_TLS_GOTIE:
  3474. tls_type = GOT_UNKNOWN;
  3475. if (h == NULL && local_got_offsets)
  3476. tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
  3477. else if (h != NULL)
  3478. tls_type = elf_i386_hash_entry(h)->tls_type;
  3479. if (tls_type == GOT_TLS_IE)
  3480. tls_type = GOT_TLS_IE_NEG;
  3481. if (! elf_i386_tls_transition (info, input_bfd,
  3482. input_section, contents,
  3483. symtab_hdr, sym_hashes,
  3484. &r_type, tls_type, rel,
  3485. relend, h, r_symndx))
  3486. return FALSE;
  3487. if (r_type == R_386_TLS_LE_32)
  3488. {
  3489. BFD_ASSERT (! unresolved_reloc);
  3490. if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
  3491. {
  3492. unsigned int type;
  3493. bfd_vma roff;
  3494. /* GD->LE transition. */
  3495. type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
  3496. if (type == 0x04)
  3497. {
  3498. /* leal foo(,%reg,1), %eax; call ___tls_get_addr
  3499. Change it into:
  3500. movl %gs:0, %eax; subl $foo@tpoff, %eax
  3501. (6 byte form of subl). */
  3502. memcpy (contents + rel->r_offset - 3,
  3503. "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
  3504. roff = rel->r_offset + 5;
  3505. }
  3506. else
  3507. {
  3508. /* leal foo(%reg), %eax; call ___tls_get_addr; nop
  3509. Change it into:
  3510. movl %gs:0, %eax; subl $foo@tpoff, %eax
  3511. (6 byte form of subl). */
  3512. memcpy (contents + rel->r_offset - 2,
  3513. "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
  3514. roff = rel->r_offset + 6;
  3515. }
  3516. bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
  3517. contents + roff);
  3518. /* Skip R_386_PC32/R_386_PLT32. */
  3519. rel++;
  3520. continue;
  3521. }
  3522. else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
  3523. {
  3524. /* GDesc -> LE transition.
  3525. It's originally something like:
  3526. leal x@tlsdesc(%ebx), %eax
  3527. leal x@ntpoff, %eax
  3528. Registers other than %eax may be set up here. */
  3529. unsigned int val;
  3530. bfd_vma roff;
  3531. roff = rel->r_offset;
  3532. val = bfd_get_8 (input_bfd, contents + roff - 1);
  3533. /* Now modify the instruction as appropriate. */
  3534. /* aoliva FIXME: remove the above and xor the byte
  3535. below with 0x86. */
  3536. bfd_put_8 (output_bfd, val ^ 0x86,
  3537. contents + roff - 1);
  3538. bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
  3539. contents + roff);
  3540. continue;
  3541. }
  3542. else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
  3543. {
  3544. /* GDesc -> LE transition.
  3545. It's originally:
  3546. call *(%eax)
  3547. Turn it into:
  3548. xchg %ax,%ax */
  3549. bfd_vma roff;
  3550. roff = rel->r_offset;
  3551. bfd_put_8 (output_bfd, 0x66, contents + roff);
  3552. bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
  3553. continue;
  3554. }
  3555. else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
  3556. {
  3557. unsigned int val;
  3558. /* IE->LE transition:
  3559. Originally it can be one of:
  3560. movl foo, %eax
  3561. movl foo, %reg
  3562. addl foo, %reg
  3563. We change it into:
  3564. movl $foo, %eax
  3565. movl $foo, %reg
  3566. addl $foo, %reg. */
  3567. val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
  3568. if (val == 0xa1)
  3569. {
  3570. /* movl foo, %eax. */
  3571. bfd_put_8 (output_bfd, 0xb8,
  3572. contents + rel->r_offset - 1);
  3573. }
  3574. else
  3575. {
  3576. unsigned int type;
  3577. type = bfd_get_8 (input_bfd,
  3578. contents + rel->r_offset - 2);
  3579. switch (type)
  3580. {
  3581. case 0x8b:
  3582. /* movl */
  3583. bfd_put_8 (output_bfd, 0xc7,
  3584. contents + rel->r_offset - 2);
  3585. bfd_put_8 (output_bfd,
  3586. 0xc0 | ((val >> 3) & 7),
  3587. contents + rel->r_offset - 1);
  3588. break;
  3589. case 0x03:
  3590. /* addl */
  3591. bfd_put_8 (output_bfd, 0x81,
  3592. contents + rel->r_offset - 2);
  3593. bfd_put_8 (output_bfd,
  3594. 0xc0 | ((val >> 3) & 7),
  3595. contents + rel->r_offset - 1);
  3596. break;
  3597. default:
  3598. BFD_FAIL ();
  3599. break;
  3600. }
  3601. }
  3602. bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
  3603. contents + rel->r_offset);
  3604. continue;
  3605. }
  3606. else
  3607. {
  3608. unsigned int val, type;
  3609. /* {IE_32,GOTIE}->LE transition:
  3610. Originally it can be one of:
  3611. subl foo(%reg1), %reg2
  3612. movl foo(%reg1), %reg2
  3613. addl foo(%reg1), %reg2
  3614. We change it into:
  3615. subl $foo, %reg2
  3616. movl $foo, %reg2 (6 byte form)
  3617. addl $foo, %reg2. */
  3618. type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
  3619. val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
  3620. if (type == 0x8b)
  3621. {
  3622. /* movl */
  3623. bfd_put_8 (output_bfd, 0xc7,
  3624. contents + rel->r_offset - 2);
  3625. bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
  3626. contents + rel->r_offset - 1);
  3627. }
  3628. else if (type == 0x2b)
  3629. {
  3630. /* subl */
  3631. bfd_put_8 (output_bfd, 0x81,
  3632. contents + rel->r_offset - 2);
  3633. bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
  3634. contents + rel->r_offset - 1);
  3635. }
  3636. else if (type == 0x03)
  3637. {
  3638. /* addl */
  3639. bfd_put_8 (output_bfd, 0x81,
  3640. contents + rel->r_offset - 2);
  3641. bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
  3642. contents + rel->r_offset - 1);
  3643. }
  3644. else
  3645. BFD_FAIL ();
  3646. if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
  3647. bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
  3648. contents + rel->r_offset);
  3649. else
  3650. bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
  3651. contents + rel->r_offset);
  3652. continue;
  3653. }
  3654. }
  3655. if (htab->elf.sgot == NULL)
  3656. abort ();
  3657. if (h != NULL)
  3658. {
  3659. off = h->got.offset;
  3660. offplt = elf_i386_hash_entry (h)->tlsdesc_got;
  3661. }
  3662. else
  3663. {
  3664. if (local_got_offsets == NULL)
  3665. abort ();
  3666. off = local_got_offsets[r_symndx];
  3667. offplt = local_tlsdesc_gotents[r_symndx];
  3668. }
  3669. if ((off & 1) != 0)
  3670. off &= ~1;
  3671. else
  3672. {
  3673. Elf_Internal_Rela outrel;
  3674. int dr_type;
  3675. asection *sreloc;
  3676. if (htab->elf.srelgot == NULL)
  3677. abort ();
  3678. indx = h && h->dynindx != -1 ? h->dynindx : 0;
  3679. if (GOT_TLS_GDESC_P (tls_type))
  3680. {
  3681. bfd_byte *loc;
  3682. outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
  3683. BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
  3684. <= htab->elf.sgotplt->size);
  3685. outrel.r_offset = (htab->elf.sgotplt->output_section->vma
  3686. + htab->elf.sgotplt->output_offset
  3687. + offplt
  3688. + htab->sgotplt_jump_table_size);
  3689. sreloc = htab->elf.srelplt;
  3690. loc = sreloc->contents;
  3691. loc += (htab->next_tls_desc_index++
  3692. * sizeof (Elf32_External_Rel));
  3693. BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
  3694. <= sreloc->contents + sreloc->size);
  3695. bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
  3696. if (indx == 0)
  3697. {
  3698. BFD_ASSERT (! unresolved_reloc);
  3699. bfd_put_32 (output_bfd,
  3700. relocation - elf_i386_dtpoff_base (info),
  3701. htab->elf.sgotplt->contents + offplt
  3702. + htab->sgotplt_jump_table_size + 4);
  3703. }
  3704. else
  3705. {
  3706. bfd_put_32 (output_bfd, 0,
  3707. htab->elf.sgotplt->contents + offplt
  3708. + htab->sgotplt_jump_table_size + 4);
  3709. }
  3710. }
  3711. sreloc = htab->elf.srelgot;
  3712. outrel.r_offset = (htab->elf.sgot->output_section->vma
  3713. + htab->elf.sgot->output_offset + off);
  3714. if (GOT_TLS_GD_P (tls_type))
  3715. dr_type = R_386_TLS_DTPMOD32;
  3716. else if (GOT_TLS_GDESC_P (tls_type))
  3717. goto dr_done;
  3718. else if (tls_type == GOT_TLS_IE_POS)
  3719. dr_type = R_386_TLS_TPOFF;
  3720. else
  3721. dr_type = R_386_TLS_TPOFF32;
  3722. if (dr_type == R_386_TLS_TPOFF && indx == 0)
  3723. bfd_put_32 (output_bfd,
  3724. relocation - elf_i386_dtpoff_base (info),
  3725. htab->elf.sgot->contents + off);
  3726. else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
  3727. bfd_put_32 (output_bfd,
  3728. elf_i386_dtpoff_base (info) - relocation,
  3729. htab->elf.sgot->contents + off);
  3730. else if (dr_type != R_386_TLS_DESC)
  3731. bfd_put_32 (output_bfd, 0,
  3732. htab->elf.sgot->contents + off);
  3733. outrel.r_info = ELF32_R_INFO (indx, dr_type);
  3734. elf_append_rel (output_bfd, sreloc, &outrel);
  3735. if (GOT_TLS_GD_P (tls_type))
  3736. {
  3737. if (indx == 0)
  3738. {
  3739. BFD_ASSERT (! unresolved_reloc);
  3740. bfd_put_32 (output_bfd,
  3741. relocation - elf_i386_dtpoff_base (info),
  3742. htab->elf.sgot->contents + off + 4);
  3743. }
  3744. else
  3745. {
  3746. bfd_put_32 (output_bfd, 0,
  3747. htab->elf.sgot->contents + off + 4);
  3748. outrel.r_info = ELF32_R_INFO (indx,
  3749. R_386_TLS_DTPOFF32);
  3750. outrel.r_offset += 4;
  3751. elf_append_rel (output_bfd, sreloc, &outrel);
  3752. }
  3753. }
  3754. else if (tls_type == GOT_TLS_IE_BOTH)
  3755. {
  3756. bfd_put_32 (output_bfd,
  3757. (indx == 0
  3758. ? relocation - elf_i386_dtpoff_base (info)
  3759. : 0),
  3760. htab->elf.sgot->contents + off + 4);
  3761. outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
  3762. outrel.r_offset += 4;
  3763. elf_append_rel (output_bfd, sreloc, &outrel);
  3764. }
  3765. dr_done:
  3766. if (h != NULL)
  3767. h->got.offset |= 1;
  3768. else
  3769. local_got_offsets[r_symndx] |= 1;
  3770. }
  3771. if (off >= (bfd_vma) -2
  3772. && ! GOT_TLS_GDESC_P (tls_type))
  3773. abort ();
  3774. if (r_type == R_386_TLS_GOTDESC
  3775. || r_type == R_386_TLS_DESC_CALL)
  3776. {
  3777. relocation = htab->sgotplt_jump_table_size + offplt;
  3778. unresolved_reloc = FALSE;
  3779. }
  3780. else if (r_type == ELF32_R_TYPE (rel->r_info))
  3781. {
  3782. bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
  3783. + htab->elf.sgotplt->output_offset;
  3784. relocation = htab->elf.sgot->output_section->vma
  3785. + htab->elf.sgot->output_offset + off - g_o_t;
  3786. if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
  3787. && tls_type == GOT_TLS_IE_BOTH)
  3788. relocation += 4;
  3789. if (r_type == R_386_TLS_IE)
  3790. relocation += g_o_t;
  3791. unresolved_reloc = FALSE;
  3792. }
  3793. else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
  3794. {
  3795. unsigned int val, type;
  3796. bfd_vma roff;
  3797. /* GD->IE transition. */
  3798. type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
  3799. val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
  3800. if (type == 0x04)
  3801. {
  3802. /* leal foo(,%reg,1), %eax; call ___tls_get_addr
  3803. Change it into:
  3804. movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
  3805. val >>= 3;
  3806. roff = rel->r_offset - 3;
  3807. }
  3808. else
  3809. {
  3810. /* leal foo(%reg), %eax; call ___tls_get_addr; nop
  3811. Change it into:
  3812. movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
  3813. roff = rel->r_offset - 2;
  3814. }
  3815. memcpy (contents + roff,
  3816. "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
  3817. contents[roff + 7] = 0x80 | (val & 7);
  3818. /* If foo is used only with foo@gotntpoff(%reg) and
  3819. foo@indntpoff, but not with foo@gottpoff(%reg), change
  3820. subl $foo@gottpoff(%reg), %eax
  3821. into:
  3822. addl $foo@gotntpoff(%reg), %eax. */
  3823. if (tls_type == GOT_TLS_IE_POS)
  3824. contents[roff + 6] = 0x03;
  3825. bfd_put_32 (output_bfd,
  3826. htab->elf.sgot->output_section->vma
  3827. + htab->elf.sgot->output_offset + off
  3828. - htab->elf.sgotplt->output_section->vma
  3829. - htab->elf.sgotplt->output_offset,
  3830. contents + roff + 8);
  3831. /* Skip R_386_PLT32. */
  3832. rel++;
  3833. continue;
  3834. }
  3835. else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
  3836. {
  3837. /* GDesc -> IE transition.
  3838. It's originally something like:
  3839. leal x@tlsdesc(%ebx), %eax
  3840. Change it to:
  3841. movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
  3842. or:
  3843. movl x@gottpoff(%ebx), %eax # before negl %eax
  3844. Registers other than %eax may be set up here. */
  3845. bfd_vma roff;
  3846. /* First, make sure it's a leal adding ebx to a 32-bit
  3847. offset into any register, although it's probably
  3848. almost always going to be eax. */
  3849. roff = rel->r_offset;
  3850. /* Now modify the instruction as appropriate. */
  3851. /* To turn a leal into a movl in the form we use it, it
  3852. suffices to change the first byte from 0x8d to 0x8b.
  3853. aoliva FIXME: should we decide to keep the leal, all
  3854. we have to do is remove the statement below, and
  3855. adjust the relaxation of R_386_TLS_DESC_CALL. */
  3856. bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
  3857. if (tls_type == GOT_TLS_IE_BOTH)
  3858. off += 4;
  3859. bfd_put_32 (output_bfd,
  3860. htab->elf.sgot->output_section->vma
  3861. + htab->elf.sgot->output_offset + off
  3862. - htab->elf.sgotplt->output_section->vma
  3863. - htab->elf.sgotplt->output_offset,
  3864. contents + roff);
  3865. continue;
  3866. }
  3867. else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
  3868. {
  3869. /* GDesc -> IE transition.
  3870. It's originally:
  3871. call *(%eax)
  3872. Change it to:
  3873. xchg %ax,%ax
  3874. or
  3875. negl %eax
  3876. depending on how we transformed the TLS_GOTDESC above.
  3877. */
  3878. bfd_vma roff;
  3879. roff = rel->r_offset;
  3880. /* Now modify the instruction as appropriate. */
  3881. if (tls_type != GOT_TLS_IE_NEG)
  3882. {
  3883. /* xchg %ax,%ax */
  3884. bfd_put_8 (output_bfd, 0x66, contents + roff);
  3885. bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
  3886. }
  3887. else
  3888. {
  3889. /* negl %eax */
  3890. bfd_put_8 (output_bfd, 0xf7, contents + roff);
  3891. bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
  3892. }
  3893. continue;
  3894. }
  3895. else
  3896. BFD_ASSERT (FALSE);
  3897. break;
  3898. case R_386_TLS_LDM:
  3899. if (! elf_i386_tls_transition (info, input_bfd,
  3900. input_section, contents,
  3901. symtab_hdr, sym_hashes,
  3902. &r_type, GOT_UNKNOWN, rel,
  3903. relend, h, r_symndx))
  3904. return FALSE;
  3905. if (r_type != R_386_TLS_LDM)
  3906. {
  3907. /* LD->LE transition:
  3908. leal foo(%reg), %eax; call ___tls_get_addr.
  3909. We change it into:
  3910. movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
  3911. BFD_ASSERT (r_type == R_386_TLS_LE_32);
  3912. memcpy (contents + rel->r_offset - 2,
  3913. "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
  3914. /* Skip R_386_PC32/R_386_PLT32. */
  3915. rel++;
  3916. continue;
  3917. }
  3918. if (htab->elf.sgot == NULL)
  3919. abort ();
  3920. off = htab->tls_ldm_got.offset;
  3921. if (off & 1)
  3922. off &= ~1;
  3923. else
  3924. {
  3925. Elf_Internal_Rela outrel;
  3926. if (htab->elf.srelgot == NULL)
  3927. abort ();
  3928. outrel.r_offset = (htab->elf.sgot->output_section->vma
  3929. + htab->elf.sgot->output_offset + off);
  3930. bfd_put_32 (output_bfd, 0,
  3931. htab->elf.sgot->contents + off);
  3932. bfd_put_32 (output_bfd, 0,
  3933. htab->elf.sgot->contents + off + 4);
  3934. outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
  3935. elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
  3936. htab->tls_ldm_got.offset |= 1;
  3937. }
  3938. relocation = htab->elf.sgot->output_section->vma
  3939. + htab->elf.sgot->output_offset + off
  3940. - htab->elf.sgotplt->output_section->vma
  3941. - htab->elf.sgotplt->output_offset;
  3942. unresolved_reloc = FALSE;
  3943. break;
  3944. case R_386_TLS_LDO_32:
  3945. if (!bfd_link_executable (info)
  3946. || (input_section->flags & SEC_CODE) == 0)
  3947. relocation -= elf_i386_dtpoff_base (info);
  3948. else
  3949. /* When converting LDO to LE, we must negate. */
  3950. relocation = -elf_i386_tpoff (info, relocation);
  3951. break;
  3952. case R_386_TLS_LE_32:
  3953. case R_386_TLS_LE:
  3954. if (!bfd_link_executable (info))
  3955. {
  3956. Elf_Internal_Rela outrel;
  3957. asection *sreloc;
  3958. outrel.r_offset = rel->r_offset
  3959. + input_section->output_section->vma
  3960. + input_section->output_offset;
  3961. if (h != NULL && h->dynindx != -1)
  3962. indx = h->dynindx;
  3963. else
  3964. indx = 0;
  3965. if (r_type == R_386_TLS_LE_32)
  3966. outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
  3967. else
  3968. outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
  3969. sreloc = elf_section_data (input_section)->sreloc;
  3970. if (sreloc == NULL)
  3971. abort ();
  3972. elf_append_rel (output_bfd, sreloc, &outrel);
  3973. if (indx)
  3974. continue;
  3975. else if (r_type == R_386_TLS_LE_32)
  3976. relocation = elf_i386_dtpoff_base (info) - relocation;
  3977. else
  3978. relocation -= elf_i386_dtpoff_base (info);
  3979. }
  3980. else if (r_type == R_386_TLS_LE_32)
  3981. relocation = elf_i386_tpoff (info, relocation);
  3982. else
  3983. relocation = -elf_i386_tpoff (info, relocation);
  3984. break;
  3985. default:
  3986. break;
  3987. }
  3988. /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
  3989. because such sections are not SEC_ALLOC and thus ld.so will
  3990. not process them. */
  3991. if (unresolved_reloc
  3992. && !((input_section->flags & SEC_DEBUGGING) != 0
  3993. && h->def_dynamic)
  3994. && _bfd_elf_section_offset (output_bfd, info, input_section,
  3995. rel->r_offset) != (bfd_vma) -1)
  3996. {
  3997. (*_bfd_error_handler)
  3998. (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
  3999. input_bfd,
  4000. input_section,
  4001. (long) rel->r_offset,
  4002. howto->name,
  4003. h->root.root.string);
  4004. return FALSE;
  4005. }
  4006. do_relocation:
  4007. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  4008. contents, rel->r_offset,
  4009. relocation, 0);
  4010. check_relocation_error:
  4011. if (r != bfd_reloc_ok)
  4012. {
  4013. const char *name;
  4014. if (h != NULL)
  4015. name = h->root.root.string;
  4016. else
  4017. {
  4018. name = bfd_elf_string_from_elf_section (input_bfd,
  4019. symtab_hdr->sh_link,
  4020. sym->st_name);
  4021. if (name == NULL)
  4022. return FALSE;
  4023. if (*name == '\0')
  4024. name = bfd_section_name (input_bfd, sec);
  4025. }
  4026. if (r == bfd_reloc_overflow)
  4027. {
  4028. if (! ((*info->callbacks->reloc_overflow)
  4029. (info, (h ? &h->root : NULL), name, howto->name,
  4030. (bfd_vma) 0, input_bfd, input_section,
  4031. rel->r_offset)))
  4032. return FALSE;
  4033. }
  4034. else
  4035. {
  4036. (*_bfd_error_handler)
  4037. (_("%B(%A+0x%lx): reloc against `%s': error %d"),
  4038. input_bfd, input_section,
  4039. (long) rel->r_offset, name, (int) r);
  4040. return FALSE;
  4041. }
  4042. }
  4043. }
  4044. return TRUE;
  4045. }
  4046. /* Finish up dynamic symbol handling. We set the contents of various
  4047. dynamic sections here. */
  4048. static bfd_boolean
  4049. elf_i386_finish_dynamic_symbol (bfd *output_bfd,
  4050. struct bfd_link_info *info,
  4051. struct elf_link_hash_entry *h,
  4052. Elf_Internal_Sym *sym)
  4053. {
  4054. struct elf_i386_link_hash_table *htab;
  4055. unsigned plt_entry_size;
  4056. const struct elf_i386_backend_data *abed;
  4057. struct elf_i386_link_hash_entry *eh;
  4058. htab = elf_i386_hash_table (info);
  4059. if (htab == NULL)
  4060. return FALSE;
  4061. abed = get_elf_i386_backend_data (output_bfd);
  4062. plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
  4063. eh = (struct elf_i386_link_hash_entry *) h;
  4064. if (h->plt.offset != (bfd_vma) -1)
  4065. {
  4066. bfd_vma plt_index;
  4067. bfd_vma got_offset;
  4068. Elf_Internal_Rela rel;
  4069. bfd_byte *loc;
  4070. asection *plt, *gotplt, *relplt;
  4071. /* When building a static executable, use .iplt, .igot.plt and
  4072. .rel.iplt sections for STT_GNU_IFUNC symbols. */
  4073. if (htab->elf.splt != NULL)
  4074. {
  4075. plt = htab->elf.splt;
  4076. gotplt = htab->elf.sgotplt;
  4077. relplt = htab->elf.srelplt;
  4078. }
  4079. else
  4080. {
  4081. plt = htab->elf.iplt;
  4082. gotplt = htab->elf.igotplt;
  4083. relplt = htab->elf.irelplt;
  4084. }
  4085. /* This symbol has an entry in the procedure linkage table. Set
  4086. it up. */
  4087. if ((h->dynindx == -1
  4088. && !((h->forced_local || bfd_link_executable (info))
  4089. && h->def_regular
  4090. && h->type == STT_GNU_IFUNC))
  4091. || plt == NULL
  4092. || gotplt == NULL
  4093. || relplt == NULL)
  4094. abort ();
  4095. /* Get the index in the procedure linkage table which
  4096. corresponds to this symbol. This is the index of this symbol
  4097. in all the symbols for which we are making plt entries. The
  4098. first entry in the procedure linkage table is reserved.
  4099. Get the offset into the .got table of the entry that
  4100. corresponds to this function. Each .got entry is 4 bytes.
  4101. The first three are reserved.
  4102. For static executables, we don't reserve anything. */
  4103. if (plt == htab->elf.splt)
  4104. {
  4105. got_offset = h->plt.offset / plt_entry_size - 1;
  4106. got_offset = (got_offset + 3) * 4;
  4107. }
  4108. else
  4109. {
  4110. got_offset = h->plt.offset / plt_entry_size;
  4111. got_offset = got_offset * 4;
  4112. }
  4113. /* Fill in the entry in the procedure linkage table. */
  4114. if (! bfd_link_pic (info))
  4115. {
  4116. memcpy (plt->contents + h->plt.offset, abed->plt->plt_entry,
  4117. abed->plt->plt_entry_size);
  4118. bfd_put_32 (output_bfd,
  4119. (gotplt->output_section->vma
  4120. + gotplt->output_offset
  4121. + got_offset),
  4122. plt->contents + h->plt.offset
  4123. + abed->plt->plt_got_offset);
  4124. if (abed->is_vxworks)
  4125. {
  4126. int s, k, reloc_index;
  4127. /* Create the R_386_32 relocation referencing the GOT
  4128. for this PLT entry. */
  4129. /* S: Current slot number (zero-based). */
  4130. s = ((h->plt.offset - abed->plt->plt_entry_size)
  4131. / abed->plt->plt_entry_size);
  4132. /* K: Number of relocations for PLTResolve. */
  4133. if (bfd_link_pic (info))
  4134. k = PLTRESOLVE_RELOCS_SHLIB;
  4135. else
  4136. k = PLTRESOLVE_RELOCS;
  4137. /* Skip the PLTresolve relocations, and the relocations for
  4138. the other PLT slots. */
  4139. reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
  4140. loc = (htab->srelplt2->contents + reloc_index
  4141. * sizeof (Elf32_External_Rel));
  4142. rel.r_offset = (htab->elf.splt->output_section->vma
  4143. + htab->elf.splt->output_offset
  4144. + h->plt.offset + 2),
  4145. rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
  4146. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  4147. /* Create the R_386_32 relocation referencing the beginning of
  4148. the PLT for this GOT entry. */
  4149. rel.r_offset = (htab->elf.sgotplt->output_section->vma
  4150. + htab->elf.sgotplt->output_offset
  4151. + got_offset);
  4152. rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
  4153. bfd_elf32_swap_reloc_out (output_bfd, &rel,
  4154. loc + sizeof (Elf32_External_Rel));
  4155. }
  4156. }
  4157. else
  4158. {
  4159. memcpy (plt->contents + h->plt.offset, abed->plt->pic_plt_entry,
  4160. abed->plt->plt_entry_size);
  4161. bfd_put_32 (output_bfd, got_offset,
  4162. plt->contents + h->plt.offset
  4163. + abed->plt->plt_got_offset);
  4164. }
  4165. /* Fill in the entry in the global offset table. */
  4166. bfd_put_32 (output_bfd,
  4167. (plt->output_section->vma
  4168. + plt->output_offset
  4169. + h->plt.offset
  4170. + abed->plt->plt_lazy_offset),
  4171. gotplt->contents + got_offset);
  4172. /* Fill in the entry in the .rel.plt section. */
  4173. rel.r_offset = (gotplt->output_section->vma
  4174. + gotplt->output_offset
  4175. + got_offset);
  4176. if (h->dynindx == -1
  4177. || ((bfd_link_executable (info)
  4178. || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  4179. && h->def_regular
  4180. && h->type == STT_GNU_IFUNC))
  4181. {
  4182. /* If an STT_GNU_IFUNC symbol is locally defined, generate
  4183. R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
  4184. in the .got.plt section. */
  4185. bfd_put_32 (output_bfd,
  4186. (h->root.u.def.value
  4187. + h->root.u.def.section->output_section->vma
  4188. + h->root.u.def.section->output_offset),
  4189. gotplt->contents + got_offset);
  4190. rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
  4191. /* R_386_IRELATIVE comes last. */
  4192. plt_index = htab->next_irelative_index--;
  4193. }
  4194. else
  4195. {
  4196. rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
  4197. plt_index = htab->next_jump_slot_index++;
  4198. }
  4199. loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
  4200. bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  4201. /* Don't fill PLT entry for static executables. */
  4202. if (plt == htab->elf.splt)
  4203. {
  4204. bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
  4205. plt->contents + h->plt.offset
  4206. + abed->plt->plt_reloc_offset);
  4207. bfd_put_32 (output_bfd, - (h->plt.offset
  4208. + abed->plt->plt_plt_offset + 4),
  4209. plt->contents + h->plt.offset
  4210. + abed->plt->plt_plt_offset);
  4211. }
  4212. }
  4213. else if (eh->plt_got.offset != (bfd_vma) -1)
  4214. {
  4215. bfd_vma got_offset, plt_offset;
  4216. asection *plt, *got, *gotplt;
  4217. const bfd_byte *got_plt_entry;
  4218. /* Offset of displacement of the indirect jump. */
  4219. bfd_vma plt_got_offset = 2;
  4220. /* Set the entry in the GOT procedure linkage table. */
  4221. plt = htab->plt_got;
  4222. got = htab->elf.sgot;
  4223. gotplt = htab->elf.sgotplt;
  4224. got_offset = h->got.offset;
  4225. if (got_offset == (bfd_vma) -1
  4226. || plt == NULL
  4227. || got == NULL
  4228. || gotplt == NULL)
  4229. abort ();
  4230. /* Fill in the entry in the GOT procedure linkage table. */
  4231. if (! bfd_link_pic (info))
  4232. {
  4233. got_plt_entry = elf_i386_got_plt_entry;
  4234. got_offset += got->output_section->vma + got->output_offset;
  4235. }
  4236. else
  4237. {
  4238. got_plt_entry = elf_i386_pic_got_plt_entry;
  4239. got_offset += (got->output_section->vma
  4240. + got->output_offset
  4241. - gotplt->output_section->vma
  4242. - gotplt->output_offset);
  4243. }
  4244. plt_offset = eh->plt_got.offset;
  4245. memcpy (plt->contents + plt_offset, got_plt_entry,
  4246. sizeof (elf_i386_got_plt_entry));
  4247. bfd_put_32 (output_bfd, got_offset,
  4248. plt->contents + plt_offset + plt_got_offset);
  4249. }
  4250. if (!h->def_regular
  4251. && (h->plt.offset != (bfd_vma) -1
  4252. || eh->plt_got.offset != (bfd_vma) -1))
  4253. {
  4254. /* Mark the symbol as undefined, rather than as defined in
  4255. the .plt section. Leave the value if there were any
  4256. relocations where pointer equality matters (this is a clue
  4257. for the dynamic linker, to make function pointer
  4258. comparisons work between an application and shared
  4259. library), otherwise set it to zero. If a function is only
  4260. called from a binary, there is no need to slow down
  4261. shared libraries because of that. */
  4262. sym->st_shndx = SHN_UNDEF;
  4263. if (!h->pointer_equality_needed)
  4264. sym->st_value = 0;
  4265. }
  4266. if (h->got.offset != (bfd_vma) -1
  4267. && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
  4268. && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
  4269. {
  4270. Elf_Internal_Rela rel;
  4271. /* This symbol has an entry in the global offset table. Set it
  4272. up. */
  4273. if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
  4274. abort ();
  4275. rel.r_offset = (htab->elf.sgot->output_section->vma
  4276. + htab->elf.sgot->output_offset
  4277. + (h->got.offset & ~(bfd_vma) 1));
  4278. /* If this is a static link, or it is a -Bsymbolic link and the
  4279. symbol is defined locally or was forced to be local because
  4280. of a version file, we just want to emit a RELATIVE reloc.
  4281. The entry in the global offset table will already have been
  4282. initialized in the relocate_section function. */
  4283. if (h->def_regular
  4284. && h->type == STT_GNU_IFUNC)
  4285. {
  4286. if (bfd_link_pic (info))
  4287. {
  4288. /* Generate R_386_GLOB_DAT. */
  4289. goto do_glob_dat;
  4290. }
  4291. else
  4292. {
  4293. asection *plt;
  4294. if (!h->pointer_equality_needed)
  4295. abort ();
  4296. /* For non-shared object, we can't use .got.plt, which
  4297. contains the real function addres if we need pointer
  4298. equality. We load the GOT entry with the PLT entry. */
  4299. plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
  4300. bfd_put_32 (output_bfd,
  4301. (plt->output_section->vma
  4302. + plt->output_offset + h->plt.offset),
  4303. htab->elf.sgot->contents + h->got.offset);
  4304. return TRUE;
  4305. }
  4306. }
  4307. else if (bfd_link_pic (info)
  4308. && SYMBOL_REFERENCES_LOCAL (info, h))
  4309. {
  4310. BFD_ASSERT((h->got.offset & 1) != 0);
  4311. rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  4312. }
  4313. else
  4314. {
  4315. BFD_ASSERT((h->got.offset & 1) == 0);
  4316. do_glob_dat:
  4317. bfd_put_32 (output_bfd, (bfd_vma) 0,
  4318. htab->elf.sgot->contents + h->got.offset);
  4319. rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
  4320. }
  4321. elf_append_rel (output_bfd, htab->elf.srelgot, &rel);
  4322. }
  4323. if (h->needs_copy)
  4324. {
  4325. Elf_Internal_Rela rel;
  4326. /* This symbol needs a copy reloc. Set it up. */
  4327. if (h->dynindx == -1
  4328. || (h->root.type != bfd_link_hash_defined
  4329. && h->root.type != bfd_link_hash_defweak)
  4330. || htab->srelbss == NULL)
  4331. abort ();
  4332. rel.r_offset = (h->root.u.def.value
  4333. + h->root.u.def.section->output_section->vma
  4334. + h->root.u.def.section->output_offset);
  4335. rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
  4336. elf_append_rel (output_bfd, htab->srelbss, &rel);
  4337. }
  4338. return TRUE;
  4339. }
  4340. /* Finish up local dynamic symbol handling. We set the contents of
  4341. various dynamic sections here. */
  4342. static bfd_boolean
  4343. elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
  4344. {
  4345. struct elf_link_hash_entry *h
  4346. = (struct elf_link_hash_entry *) *slot;
  4347. struct bfd_link_info *info
  4348. = (struct bfd_link_info *) inf;
  4349. return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
  4350. h, NULL);
  4351. }
  4352. /* Used to decide how to sort relocs in an optimal manner for the
  4353. dynamic linker, before writing them out. */
  4354. static enum elf_reloc_type_class
  4355. elf_i386_reloc_type_class (const struct bfd_link_info *info,
  4356. const asection *rel_sec ATTRIBUTE_UNUSED,
  4357. const Elf_Internal_Rela *rela)
  4358. {
  4359. bfd *abfd = info->output_bfd;
  4360. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  4361. struct elf_link_hash_table *htab = elf_hash_table (info);
  4362. unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
  4363. Elf_Internal_Sym sym;
  4364. if (htab->dynsym == NULL
  4365. || !bed->s->swap_symbol_in (abfd,
  4366. (htab->dynsym->contents
  4367. + r_symndx * sizeof (Elf32_External_Sym)),
  4368. 0, &sym))
  4369. abort ();
  4370. /* Check relocation against STT_GNU_IFUNC symbol. */
  4371. if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
  4372. return reloc_class_ifunc;
  4373. switch (ELF32_R_TYPE (rela->r_info))
  4374. {
  4375. case R_386_RELATIVE:
  4376. return reloc_class_relative;
  4377. case R_386_JUMP_SLOT:
  4378. return reloc_class_plt;
  4379. case R_386_COPY:
  4380. return reloc_class_copy;
  4381. default:
  4382. return reloc_class_normal;
  4383. }
  4384. }
  4385. /* Finish up the dynamic sections. */
  4386. static bfd_boolean
  4387. elf_i386_finish_dynamic_sections (bfd *output_bfd,
  4388. struct bfd_link_info *info)
  4389. {
  4390. struct elf_i386_link_hash_table *htab;
  4391. bfd *dynobj;
  4392. asection *sdyn;
  4393. const struct elf_i386_backend_data *abed;
  4394. htab = elf_i386_hash_table (info);
  4395. if (htab == NULL)
  4396. return FALSE;
  4397. dynobj = htab->elf.dynobj;
  4398. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  4399. abed = get_elf_i386_backend_data (output_bfd);
  4400. if (htab->elf.dynamic_sections_created)
  4401. {
  4402. Elf32_External_Dyn *dyncon, *dynconend;
  4403. if (sdyn == NULL || htab->elf.sgot == NULL)
  4404. abort ();
  4405. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  4406. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  4407. for (; dyncon < dynconend; dyncon++)
  4408. {
  4409. Elf_Internal_Dyn dyn;
  4410. asection *s;
  4411. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  4412. switch (dyn.d_tag)
  4413. {
  4414. default:
  4415. if (abed->is_vxworks
  4416. && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
  4417. break;
  4418. continue;
  4419. case DT_PLTGOT:
  4420. s = htab->elf.sgotplt;
  4421. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  4422. break;
  4423. case DT_JMPREL:
  4424. s = htab->elf.srelplt;
  4425. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  4426. break;
  4427. case DT_PLTRELSZ:
  4428. s = htab->elf.srelplt;
  4429. dyn.d_un.d_val = s->size;
  4430. break;
  4431. case DT_RELSZ:
  4432. /* My reading of the SVR4 ABI indicates that the
  4433. procedure linkage table relocs (DT_JMPREL) should be
  4434. included in the overall relocs (DT_REL). This is
  4435. what Solaris does. However, UnixWare can not handle
  4436. that case. Therefore, we override the DT_RELSZ entry
  4437. here to make it not include the JMPREL relocs. */
  4438. s = htab->elf.srelplt;
  4439. if (s == NULL)
  4440. continue;
  4441. dyn.d_un.d_val -= s->size;
  4442. break;
  4443. case DT_REL:
  4444. /* We may not be using the standard ELF linker script.
  4445. If .rel.plt is the first .rel section, we adjust
  4446. DT_REL to not include it. */
  4447. s = htab->elf.srelplt;
  4448. if (s == NULL)
  4449. continue;
  4450. if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
  4451. continue;
  4452. dyn.d_un.d_ptr += s->size;
  4453. break;
  4454. }
  4455. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4456. }
  4457. /* Fill in the first entry in the procedure linkage table. */
  4458. if (htab->elf.splt && htab->elf.splt->size > 0)
  4459. {
  4460. if (bfd_link_pic (info))
  4461. {
  4462. memcpy (htab->elf.splt->contents, abed->plt->pic_plt0_entry,
  4463. abed->plt->plt0_entry_size);
  4464. memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
  4465. abed->plt0_pad_byte,
  4466. abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
  4467. }
  4468. else
  4469. {
  4470. memcpy (htab->elf.splt->contents, abed->plt->plt0_entry,
  4471. abed->plt->plt0_entry_size);
  4472. memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
  4473. abed->plt0_pad_byte,
  4474. abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
  4475. bfd_put_32 (output_bfd,
  4476. (htab->elf.sgotplt->output_section->vma
  4477. + htab->elf.sgotplt->output_offset
  4478. + 4),
  4479. htab->elf.splt->contents
  4480. + abed->plt->plt0_got1_offset);
  4481. bfd_put_32 (output_bfd,
  4482. (htab->elf.sgotplt->output_section->vma
  4483. + htab->elf.sgotplt->output_offset
  4484. + 8),
  4485. htab->elf.splt->contents
  4486. + abed->plt->plt0_got2_offset);
  4487. if (abed->is_vxworks)
  4488. {
  4489. Elf_Internal_Rela rel;
  4490. /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
  4491. On IA32 we use REL relocations so the addend goes in
  4492. the PLT directly. */
  4493. rel.r_offset = (htab->elf.splt->output_section->vma
  4494. + htab->elf.splt->output_offset
  4495. + abed->plt->plt0_got1_offset);
  4496. rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
  4497. bfd_elf32_swap_reloc_out (output_bfd, &rel,
  4498. htab->srelplt2->contents);
  4499. /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
  4500. rel.r_offset = (htab->elf.splt->output_section->vma
  4501. + htab->elf.splt->output_offset
  4502. + abed->plt->plt0_got2_offset);
  4503. rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
  4504. bfd_elf32_swap_reloc_out (output_bfd, &rel,
  4505. htab->srelplt2->contents +
  4506. sizeof (Elf32_External_Rel));
  4507. }
  4508. }
  4509. /* UnixWare sets the entsize of .plt to 4, although that doesn't
  4510. really seem like the right value. */
  4511. elf_section_data (htab->elf.splt->output_section)
  4512. ->this_hdr.sh_entsize = 4;
  4513. /* Correct the .rel.plt.unloaded relocations. */
  4514. if (abed->is_vxworks && !bfd_link_pic (info))
  4515. {
  4516. int num_plts = (htab->elf.splt->size
  4517. / abed->plt->plt_entry_size) - 1;
  4518. unsigned char *p;
  4519. p = htab->srelplt2->contents;
  4520. if (bfd_link_pic (info))
  4521. p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
  4522. else
  4523. p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
  4524. for (; num_plts; num_plts--)
  4525. {
  4526. Elf_Internal_Rela rel;
  4527. bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
  4528. rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
  4529. bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
  4530. p += sizeof (Elf32_External_Rel);
  4531. bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
  4532. rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
  4533. bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
  4534. p += sizeof (Elf32_External_Rel);
  4535. }
  4536. }
  4537. }
  4538. }
  4539. if (htab->elf.sgotplt)
  4540. {
  4541. if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
  4542. {
  4543. (*_bfd_error_handler)
  4544. (_("discarded output section: `%A'"), htab->elf.sgotplt);
  4545. return FALSE;
  4546. }
  4547. /* Fill in the first three entries in the global offset table. */
  4548. if (htab->elf.sgotplt->size > 0)
  4549. {
  4550. bfd_put_32 (output_bfd,
  4551. (sdyn == NULL ? 0
  4552. : sdyn->output_section->vma + sdyn->output_offset),
  4553. htab->elf.sgotplt->contents);
  4554. bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
  4555. bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
  4556. }
  4557. elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
  4558. }
  4559. /* Adjust .eh_frame for .plt section. */
  4560. if (htab->plt_eh_frame != NULL
  4561. && htab->plt_eh_frame->contents != NULL)
  4562. {
  4563. if (htab->elf.splt != NULL
  4564. && htab->elf.splt->size != 0
  4565. && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
  4566. && htab->elf.splt->output_section != NULL
  4567. && htab->plt_eh_frame->output_section != NULL)
  4568. {
  4569. bfd_vma plt_start = htab->elf.splt->output_section->vma;
  4570. bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
  4571. + htab->plt_eh_frame->output_offset
  4572. + PLT_FDE_START_OFFSET;
  4573. bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
  4574. htab->plt_eh_frame->contents
  4575. + PLT_FDE_START_OFFSET);
  4576. }
  4577. if (htab->plt_eh_frame->sec_info_type
  4578. == SEC_INFO_TYPE_EH_FRAME)
  4579. {
  4580. if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
  4581. htab->plt_eh_frame,
  4582. htab->plt_eh_frame->contents))
  4583. return FALSE;
  4584. }
  4585. }
  4586. if (htab->elf.sgot && htab->elf.sgot->size > 0)
  4587. elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
  4588. /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
  4589. htab_traverse (htab->loc_hash_table,
  4590. elf_i386_finish_local_dynamic_symbol,
  4591. info);
  4592. return TRUE;
  4593. }
  4594. /* Return an array of PLT entry symbol values. */
  4595. static bfd_vma *
  4596. elf_i386_get_plt_sym_val (bfd *abfd, asymbol **dynsyms, asection *plt,
  4597. asection *relplt)
  4598. {
  4599. bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
  4600. arelent *p;
  4601. long count, i;
  4602. bfd_vma *plt_sym_val;
  4603. bfd_vma plt_offset;
  4604. bfd_byte *plt_contents;
  4605. const struct elf_i386_backend_data *bed
  4606. = get_elf_i386_backend_data (abfd);
  4607. Elf_Internal_Shdr *hdr;
  4608. /* Get the .plt section contents. */
  4609. plt_contents = (bfd_byte *) bfd_malloc (plt->size);
  4610. if (plt_contents == NULL)
  4611. return NULL;
  4612. if (!bfd_get_section_contents (abfd, (asection *) plt,
  4613. plt_contents, 0, plt->size))
  4614. {
  4615. bad_return:
  4616. free (plt_contents);
  4617. return NULL;
  4618. }
  4619. slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
  4620. if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
  4621. goto bad_return;
  4622. hdr = &elf_section_data (relplt)->this_hdr;
  4623. count = relplt->size / hdr->sh_entsize;
  4624. plt_sym_val = (bfd_vma *) bfd_malloc (sizeof (bfd_vma) * count);
  4625. if (plt_sym_val == NULL)
  4626. goto bad_return;
  4627. for (i = 0; i < count; i++)
  4628. plt_sym_val[i] = -1;
  4629. plt_offset = bed->plt->plt_entry_size;
  4630. p = relplt->relocation;
  4631. for (i = 0; i < count; i++, p++)
  4632. {
  4633. long reloc_index;
  4634. /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
  4635. if (p->howto == NULL)
  4636. continue;
  4637. if (p->howto->type != R_386_JUMP_SLOT
  4638. && p->howto->type != R_386_IRELATIVE)
  4639. continue;
  4640. reloc_index = H_GET_32 (abfd, (plt_contents + plt_offset
  4641. + bed->plt->plt_reloc_offset));
  4642. reloc_index /= sizeof (Elf32_External_Rel);
  4643. if (reloc_index >= count)
  4644. abort ();
  4645. plt_sym_val[reloc_index] = plt->vma + plt_offset;
  4646. plt_offset += bed->plt->plt_entry_size;
  4647. /* PR binutils/18437: Skip extra relocations in the .rel.plt
  4648. section. */
  4649. if (plt_offset >= plt->size)
  4650. break;
  4651. }
  4652. free (plt_contents);
  4653. return plt_sym_val;
  4654. }
  4655. /* Similar to _bfd_elf_get_synthetic_symtab. */
  4656. static long
  4657. elf_i386_get_synthetic_symtab (bfd *abfd,
  4658. long symcount,
  4659. asymbol **syms,
  4660. long dynsymcount,
  4661. asymbol **dynsyms,
  4662. asymbol **ret)
  4663. {
  4664. asection *plt = bfd_get_section_by_name (abfd, ".plt");
  4665. return _bfd_elf_ifunc_get_synthetic_symtab (abfd, symcount, syms,
  4666. dynsymcount, dynsyms, ret,
  4667. plt,
  4668. elf_i386_get_plt_sym_val);
  4669. }
  4670. /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
  4671. static bfd_boolean
  4672. elf_i386_hash_symbol (struct elf_link_hash_entry *h)
  4673. {
  4674. if (h->plt.offset != (bfd_vma) -1
  4675. && !h->def_regular
  4676. && !h->pointer_equality_needed)
  4677. return FALSE;
  4678. return _bfd_elf_hash_symbol (h);
  4679. }
  4680. /* Hook called by the linker routine which adds symbols from an object
  4681. file. */
  4682. static bfd_boolean
  4683. elf_i386_add_symbol_hook (bfd * abfd,
  4684. struct bfd_link_info * info,
  4685. Elf_Internal_Sym * sym,
  4686. const char ** namep ATTRIBUTE_UNUSED,
  4687. flagword * flagsp ATTRIBUTE_UNUSED,
  4688. asection ** secp ATTRIBUTE_UNUSED,
  4689. bfd_vma * valp ATTRIBUTE_UNUSED)
  4690. {
  4691. if (ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE
  4692. && (abfd->flags & DYNAMIC) == 0
  4693. && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
  4694. elf_tdata (info->output_bfd)->has_gnu_symbols
  4695. |= elf_gnu_symbol_unique;
  4696. return TRUE;
  4697. }
  4698. #define TARGET_LITTLE_SYM i386_elf32_vec
  4699. #define TARGET_LITTLE_NAME "elf32-i386"
  4700. #define ELF_ARCH bfd_arch_i386
  4701. #define ELF_TARGET_ID I386_ELF_DATA
  4702. #define ELF_MACHINE_CODE EM_386
  4703. #define ELF_MAXPAGESIZE 0x1000
  4704. #define elf_backend_can_gc_sections 1
  4705. #define elf_backend_can_refcount 1
  4706. #define elf_backend_want_got_plt 1
  4707. #define elf_backend_plt_readonly 1
  4708. #define elf_backend_want_plt_sym 0
  4709. #define elf_backend_got_header_size 12
  4710. #define elf_backend_plt_alignment 4
  4711. #define elf_backend_extern_protected_data 1
  4712. /* Support RELA for objdump of prelink objects. */
  4713. #define elf_info_to_howto elf_i386_info_to_howto_rel
  4714. #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
  4715. #define bfd_elf32_mkobject elf_i386_mkobject
  4716. #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
  4717. #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
  4718. #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
  4719. #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
  4720. #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
  4721. #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
  4722. #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
  4723. #define elf_backend_check_relocs elf_i386_check_relocs
  4724. #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
  4725. #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
  4726. #define elf_backend_fake_sections elf_i386_fake_sections
  4727. #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
  4728. #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
  4729. #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
  4730. #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
  4731. #define elf_backend_grok_prstatus elf_i386_grok_prstatus
  4732. #define elf_backend_grok_psinfo elf_i386_grok_psinfo
  4733. #define elf_backend_reloc_type_class elf_i386_reloc_type_class
  4734. #define elf_backend_relocate_section elf_i386_relocate_section
  4735. #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
  4736. #define elf_backend_always_size_sections elf_i386_always_size_sections
  4737. #define elf_backend_omit_section_dynsym \
  4738. ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
  4739. #define elf_backend_hash_symbol elf_i386_hash_symbol
  4740. #define elf_backend_add_symbol_hook elf_i386_add_symbol_hook
  4741. #include "elf32-target.h"
  4742. /* FreeBSD support. */
  4743. #undef TARGET_LITTLE_SYM
  4744. #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
  4745. #undef TARGET_LITTLE_NAME
  4746. #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
  4747. #undef ELF_OSABI
  4748. #define ELF_OSABI ELFOSABI_FREEBSD
  4749. /* The kernel recognizes executables as valid only if they carry a
  4750. "FreeBSD" label in the ELF header. So we put this label on all
  4751. executables and (for simplicity) also all other object files. */
  4752. static void
  4753. elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
  4754. {
  4755. _bfd_elf_post_process_headers (abfd, info);
  4756. #ifdef OLD_FREEBSD_ABI_LABEL
  4757. {
  4758. /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
  4759. Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
  4760. memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
  4761. }
  4762. #endif
  4763. }
  4764. #undef elf_backend_post_process_headers
  4765. #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
  4766. #undef elf32_bed
  4767. #define elf32_bed elf32_i386_fbsd_bed
  4768. #undef elf_backend_add_symbol_hook
  4769. #include "elf32-target.h"
  4770. /* Solaris 2. */
  4771. #undef TARGET_LITTLE_SYM
  4772. #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
  4773. #undef TARGET_LITTLE_NAME
  4774. #define TARGET_LITTLE_NAME "elf32-i386-sol2"
  4775. #undef elf_backend_post_process_headers
  4776. /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
  4777. objects won't be recognized. */
  4778. #undef ELF_OSABI
  4779. #undef elf32_bed
  4780. #define elf32_bed elf32_i386_sol2_bed
  4781. /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
  4782. boundary. */
  4783. #undef elf_backend_static_tls_alignment
  4784. #define elf_backend_static_tls_alignment 8
  4785. /* The Solaris 2 ABI requires a plt symbol on all platforms.
  4786. Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
  4787. File, p.63. */
  4788. #undef elf_backend_want_plt_sym
  4789. #define elf_backend_want_plt_sym 1
  4790. #include "elf32-target.h"
  4791. /* Intel MCU support. */
  4792. static bfd_boolean
  4793. elf32_iamcu_elf_object_p (bfd *abfd)
  4794. {
  4795. /* Set the right machine number for an IAMCU elf32 file. */
  4796. bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
  4797. return TRUE;
  4798. }
  4799. #undef TARGET_LITTLE_SYM
  4800. #define TARGET_LITTLE_SYM iamcu_elf32_vec
  4801. #undef TARGET_LITTLE_NAME
  4802. #define TARGET_LITTLE_NAME "elf32-iamcu"
  4803. #undef ELF_ARCH
  4804. #define ELF_ARCH bfd_arch_iamcu
  4805. #undef ELF_MACHINE_CODE
  4806. #define ELF_MACHINE_CODE EM_IAMCU
  4807. #undef ELF_OSABI
  4808. #undef elf32_bed
  4809. #define elf32_bed elf32_iamcu_bed
  4810. #undef elf_backend_object_p
  4811. #define elf_backend_object_p elf32_iamcu_elf_object_p
  4812. #undef elf_backend_static_tls_alignment
  4813. #undef elf_backend_want_plt_sym
  4814. #define elf_backend_want_plt_sym 0
  4815. #include "elf32-target.h"
  4816. /* Restore defaults. */
  4817. #undef ELF_ARCH
  4818. #define ELF_ARCH bfd_arch_i386
  4819. #undef ELF_MACHINE_CODE
  4820. #define ELF_MACHINE_CODE EM_386
  4821. /* Native Client support. */
  4822. #undef TARGET_LITTLE_SYM
  4823. #define TARGET_LITTLE_SYM i386_elf32_nacl_vec
  4824. #undef TARGET_LITTLE_NAME
  4825. #define TARGET_LITTLE_NAME "elf32-i386-nacl"
  4826. #undef elf32_bed
  4827. #define elf32_bed elf32_i386_nacl_bed
  4828. #undef ELF_MAXPAGESIZE
  4829. #define ELF_MAXPAGESIZE 0x10000
  4830. /* Restore defaults. */
  4831. #undef ELF_OSABI
  4832. #undef elf_backend_want_plt_sym
  4833. #define elf_backend_want_plt_sym 0
  4834. #undef elf_backend_post_process_headers
  4835. #undef elf_backend_static_tls_alignment
  4836. /* NaCl uses substantially different PLT entries for the same effects. */
  4837. #undef elf_backend_plt_alignment
  4838. #define elf_backend_plt_alignment 5
  4839. #define NACL_PLT_ENTRY_SIZE 64
  4840. #define NACLMASK 0xe0 /* 32-byte alignment mask. */
  4841. static const bfd_byte elf_i386_nacl_plt0_entry[] =
  4842. {
  4843. 0xff, 0x35, /* pushl contents of address */
  4844. 0, 0, 0, 0, /* replaced with address of .got + 4. */
  4845. 0x8b, 0x0d, /* movl contents of address, %ecx */
  4846. 0, 0, 0, 0, /* replaced with address of .got + 8. */
  4847. 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
  4848. 0xff, 0xe1 /* jmp *%ecx */
  4849. };
  4850. static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
  4851. {
  4852. 0x8b, 0x0d, /* movl contents of address, %ecx */
  4853. 0, 0, 0, 0, /* replaced with GOT slot address. */
  4854. 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
  4855. 0xff, 0xe1, /* jmp *%ecx */
  4856. /* Pad to the next 32-byte boundary with nop instructions. */
  4857. 0x90,
  4858. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  4859. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  4860. /* Lazy GOT entries point here (32-byte aligned). */
  4861. 0x68, /* pushl immediate */
  4862. 0, 0, 0, 0, /* replaced with reloc offset. */
  4863. 0xe9, /* jmp relative */
  4864. 0, 0, 0, 0, /* replaced with offset to .plt. */
  4865. /* Pad to the next 32-byte boundary with nop instructions. */
  4866. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  4867. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  4868. 0x90, 0x90
  4869. };
  4870. static const bfd_byte
  4871. elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
  4872. {
  4873. 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
  4874. 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
  4875. 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
  4876. 0xff, 0xe1, /* jmp *%ecx */
  4877. /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
  4878. so pad to that size with nop instructions. */
  4879. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
  4880. };
  4881. static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
  4882. {
  4883. 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
  4884. 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
  4885. 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
  4886. 0xff, 0xe1, /* jmp *%ecx */
  4887. /* Pad to the next 32-byte boundary with nop instructions. */
  4888. 0x90,
  4889. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  4890. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  4891. /* Lazy GOT entries point here (32-byte aligned). */
  4892. 0x68, /* pushl immediate */
  4893. 0, 0, 0, 0, /* replaced with offset into relocation table. */
  4894. 0xe9, /* jmp relative */
  4895. 0, 0, 0, 0, /* replaced with offset to start of .plt. */
  4896. /* Pad to the next 32-byte boundary with nop instructions. */
  4897. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  4898. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  4899. 0x90, 0x90
  4900. };
  4901. static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
  4902. {
  4903. #if (PLT_CIE_LENGTH != 20 \
  4904. || PLT_FDE_LENGTH != 36 \
  4905. || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
  4906. || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
  4907. # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
  4908. #endif
  4909. PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
  4910. 0, 0, 0, 0, /* CIE ID */
  4911. 1, /* CIE version */
  4912. 'z', 'R', 0, /* Augmentation string */
  4913. 1, /* Code alignment factor */
  4914. 0x7c, /* Data alignment factor: -4 */
  4915. 8, /* Return address column */
  4916. 1, /* Augmentation size */
  4917. DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
  4918. DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
  4919. DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
  4920. DW_CFA_nop, DW_CFA_nop,
  4921. PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
  4922. PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
  4923. 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
  4924. 0, 0, 0, 0, /* .plt size goes here */
  4925. 0, /* Augmentation size */
  4926. DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
  4927. DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
  4928. DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
  4929. DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
  4930. DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
  4931. 13, /* Block length */
  4932. DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
  4933. DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
  4934. DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
  4935. DW_OP_lit2, DW_OP_shl, DW_OP_plus,
  4936. DW_CFA_nop, DW_CFA_nop
  4937. };
  4938. static const struct elf_i386_plt_layout elf_i386_nacl_plt =
  4939. {
  4940. elf_i386_nacl_plt0_entry, /* plt0_entry */
  4941. sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
  4942. 2, /* plt0_got1_offset */
  4943. 8, /* plt0_got2_offset */
  4944. elf_i386_nacl_plt_entry, /* plt_entry */
  4945. NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
  4946. 2, /* plt_got_offset */
  4947. 33, /* plt_reloc_offset */
  4948. 38, /* plt_plt_offset */
  4949. 32, /* plt_lazy_offset */
  4950. elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
  4951. elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
  4952. elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
  4953. sizeof (elf_i386_nacl_eh_frame_plt),/* eh_frame_plt_size */
  4954. };
  4955. static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
  4956. {
  4957. &elf_i386_nacl_plt, /* plt */
  4958. 0x90, /* plt0_pad_byte: nop insn */
  4959. 0, /* is_vxworks */
  4960. };
  4961. static bfd_boolean
  4962. elf32_i386_nacl_elf_object_p (bfd *abfd)
  4963. {
  4964. /* Set the right machine number for a NaCl i386 ELF32 file. */
  4965. bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
  4966. return TRUE;
  4967. }
  4968. #undef elf_backend_arch_data
  4969. #define elf_backend_arch_data &elf_i386_nacl_arch_bed
  4970. #undef elf_backend_object_p
  4971. #define elf_backend_object_p elf32_i386_nacl_elf_object_p
  4972. #undef elf_backend_modify_segment_map
  4973. #define elf_backend_modify_segment_map nacl_modify_segment_map
  4974. #undef elf_backend_modify_program_headers
  4975. #define elf_backend_modify_program_headers nacl_modify_program_headers
  4976. #undef elf_backend_final_write_processing
  4977. #define elf_backend_final_write_processing nacl_final_write_processing
  4978. #include "elf32-target.h"
  4979. /* Restore defaults. */
  4980. #undef elf_backend_object_p
  4981. #undef elf_backend_modify_segment_map
  4982. #undef elf_backend_modify_program_headers
  4983. #undef elf_backend_final_write_processing
  4984. /* VxWorks support. */
  4985. #undef TARGET_LITTLE_SYM
  4986. #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
  4987. #undef TARGET_LITTLE_NAME
  4988. #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
  4989. #undef ELF_OSABI
  4990. #undef elf_backend_plt_alignment
  4991. #define elf_backend_plt_alignment 4
  4992. static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
  4993. {
  4994. &elf_i386_plt, /* plt */
  4995. 0x90, /* plt0_pad_byte */
  4996. 1, /* is_vxworks */
  4997. };
  4998. #undef elf_backend_arch_data
  4999. #define elf_backend_arch_data &elf_i386_vxworks_arch_bed
  5000. #undef elf_backend_relocs_compatible
  5001. #undef elf_backend_add_symbol_hook
  5002. #define elf_backend_add_symbol_hook \
  5003. elf_vxworks_add_symbol_hook
  5004. #undef elf_backend_link_output_symbol_hook
  5005. #define elf_backend_link_output_symbol_hook \
  5006. elf_vxworks_link_output_symbol_hook
  5007. #undef elf_backend_emit_relocs
  5008. #define elf_backend_emit_relocs elf_vxworks_emit_relocs
  5009. #undef elf_backend_final_write_processing
  5010. #define elf_backend_final_write_processing \
  5011. elf_vxworks_final_write_processing
  5012. #undef elf_backend_static_tls_alignment
  5013. /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
  5014. define it. */
  5015. #undef elf_backend_want_plt_sym
  5016. #define elf_backend_want_plt_sym 1
  5017. #undef elf32_bed
  5018. #define elf32_bed elf32_i386_vxworks_bed
  5019. #include "elf32-target.h"