output.cc 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574
  1. // output.cc -- manage the output file for gold
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cstdlib>
  19. #include <cstring>
  20. #include <cerrno>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #include <algorithm>
  25. #ifdef HAVE_SYS_MMAN_H
  26. #include <sys/mman.h>
  27. #endif
  28. #include "libiberty.h"
  29. #include "dwarf.h"
  30. #include "parameters.h"
  31. #include "object.h"
  32. #include "symtab.h"
  33. #include "reloc.h"
  34. #include "merge.h"
  35. #include "descriptors.h"
  36. #include "layout.h"
  37. #include "output.h"
  38. // For systems without mmap support.
  39. #ifndef HAVE_MMAP
  40. # define mmap gold_mmap
  41. # define munmap gold_munmap
  42. # define mremap gold_mremap
  43. # ifndef MAP_FAILED
  44. # define MAP_FAILED (reinterpret_cast<void*>(-1))
  45. # endif
  46. # ifndef PROT_READ
  47. # define PROT_READ 0
  48. # endif
  49. # ifndef PROT_WRITE
  50. # define PROT_WRITE 0
  51. # endif
  52. # ifndef MAP_PRIVATE
  53. # define MAP_PRIVATE 0
  54. # endif
  55. # ifndef MAP_ANONYMOUS
  56. # define MAP_ANONYMOUS 0
  57. # endif
  58. # ifndef MAP_SHARED
  59. # define MAP_SHARED 0
  60. # endif
  61. # ifndef ENOSYS
  62. # define ENOSYS EINVAL
  63. # endif
  64. static void *
  65. gold_mmap(void *, size_t, int, int, int, off_t)
  66. {
  67. errno = ENOSYS;
  68. return MAP_FAILED;
  69. }
  70. static int
  71. gold_munmap(void *, size_t)
  72. {
  73. errno = ENOSYS;
  74. return -1;
  75. }
  76. static void *
  77. gold_mremap(void *, size_t, size_t, int)
  78. {
  79. errno = ENOSYS;
  80. return MAP_FAILED;
  81. }
  82. #endif
  83. #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
  84. # define mremap gold_mremap
  85. extern "C" void *gold_mremap(void *, size_t, size_t, int);
  86. #endif
  87. // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
  88. #ifndef MAP_ANONYMOUS
  89. # define MAP_ANONYMOUS MAP_ANON
  90. #endif
  91. #ifndef MREMAP_MAYMOVE
  92. # define MREMAP_MAYMOVE 1
  93. #endif
  94. // Mingw does not have S_ISLNK.
  95. #ifndef S_ISLNK
  96. # define S_ISLNK(mode) 0
  97. #endif
  98. namespace gold
  99. {
  100. // A wrapper around posix_fallocate. If we don't have posix_fallocate,
  101. // or the --no-posix-fallocate option is set, we try the fallocate
  102. // system call directly. If that fails, we use ftruncate to set
  103. // the file size and hope that there is enough disk space.
  104. static int
  105. gold_fallocate(int o, off_t offset, off_t len)
  106. {
  107. if (len <= 0)
  108. return 0;
  109. #ifdef HAVE_POSIX_FALLOCATE
  110. if (parameters->options().posix_fallocate())
  111. {
  112. int err = ::posix_fallocate(o, offset, len);
  113. if (err != EINVAL && err != ENOSYS && err != EOPNOTSUPP)
  114. return err;
  115. }
  116. #endif // defined(HAVE_POSIX_FALLOCATE)
  117. #ifdef HAVE_FALLOCATE
  118. {
  119. errno = 0;
  120. int err = ::fallocate(o, 0, offset, len);
  121. if (err < 0 && errno != EINVAL && errno != ENOSYS && errno != EOPNOTSUPP)
  122. return errno;
  123. }
  124. #endif // defined(HAVE_FALLOCATE)
  125. errno = 0;
  126. if (::ftruncate(o, offset + len) < 0)
  127. return errno;
  128. return 0;
  129. }
  130. // Output_data variables.
  131. bool Output_data::allocated_sizes_are_fixed;
  132. // Output_data methods.
  133. Output_data::~Output_data()
  134. {
  135. }
  136. // Return the default alignment for the target size.
  137. uint64_t
  138. Output_data::default_alignment()
  139. {
  140. return Output_data::default_alignment_for_size(
  141. parameters->target().get_size());
  142. }
  143. // Return the default alignment for a size--32 or 64.
  144. uint64_t
  145. Output_data::default_alignment_for_size(int size)
  146. {
  147. if (size == 32)
  148. return 4;
  149. else if (size == 64)
  150. return 8;
  151. else
  152. gold_unreachable();
  153. }
  154. // Output_section_header methods. This currently assumes that the
  155. // segment and section lists are complete at construction time.
  156. Output_section_headers::Output_section_headers(
  157. const Layout* layout,
  158. const Layout::Segment_list* segment_list,
  159. const Layout::Section_list* section_list,
  160. const Layout::Section_list* unattached_section_list,
  161. const Stringpool* secnamepool,
  162. const Output_section* shstrtab_section)
  163. : layout_(layout),
  164. segment_list_(segment_list),
  165. section_list_(section_list),
  166. unattached_section_list_(unattached_section_list),
  167. secnamepool_(secnamepool),
  168. shstrtab_section_(shstrtab_section)
  169. {
  170. }
  171. // Compute the current data size.
  172. off_t
  173. Output_section_headers::do_size() const
  174. {
  175. // Count all the sections. Start with 1 for the null section.
  176. off_t count = 1;
  177. if (!parameters->options().relocatable())
  178. {
  179. for (Layout::Segment_list::const_iterator p =
  180. this->segment_list_->begin();
  181. p != this->segment_list_->end();
  182. ++p)
  183. if ((*p)->type() == elfcpp::PT_LOAD)
  184. count += (*p)->output_section_count();
  185. }
  186. else
  187. {
  188. for (Layout::Section_list::const_iterator p =
  189. this->section_list_->begin();
  190. p != this->section_list_->end();
  191. ++p)
  192. if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
  193. ++count;
  194. }
  195. count += this->unattached_section_list_->size();
  196. const int size = parameters->target().get_size();
  197. int shdr_size;
  198. if (size == 32)
  199. shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
  200. else if (size == 64)
  201. shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
  202. else
  203. gold_unreachable();
  204. return count * shdr_size;
  205. }
  206. // Write out the section headers.
  207. void
  208. Output_section_headers::do_write(Output_file* of)
  209. {
  210. switch (parameters->size_and_endianness())
  211. {
  212. #ifdef HAVE_TARGET_32_LITTLE
  213. case Parameters::TARGET_32_LITTLE:
  214. this->do_sized_write<32, false>(of);
  215. break;
  216. #endif
  217. #ifdef HAVE_TARGET_32_BIG
  218. case Parameters::TARGET_32_BIG:
  219. this->do_sized_write<32, true>(of);
  220. break;
  221. #endif
  222. #ifdef HAVE_TARGET_64_LITTLE
  223. case Parameters::TARGET_64_LITTLE:
  224. this->do_sized_write<64, false>(of);
  225. break;
  226. #endif
  227. #ifdef HAVE_TARGET_64_BIG
  228. case Parameters::TARGET_64_BIG:
  229. this->do_sized_write<64, true>(of);
  230. break;
  231. #endif
  232. default:
  233. gold_unreachable();
  234. }
  235. }
  236. template<int size, bool big_endian>
  237. void
  238. Output_section_headers::do_sized_write(Output_file* of)
  239. {
  240. off_t all_shdrs_size = this->data_size();
  241. unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
  242. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  243. unsigned char* v = view;
  244. {
  245. typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
  246. oshdr.put_sh_name(0);
  247. oshdr.put_sh_type(elfcpp::SHT_NULL);
  248. oshdr.put_sh_flags(0);
  249. oshdr.put_sh_addr(0);
  250. oshdr.put_sh_offset(0);
  251. size_t section_count = (this->data_size()
  252. / elfcpp::Elf_sizes<size>::shdr_size);
  253. if (section_count < elfcpp::SHN_LORESERVE)
  254. oshdr.put_sh_size(0);
  255. else
  256. oshdr.put_sh_size(section_count);
  257. unsigned int shstrndx = this->shstrtab_section_->out_shndx();
  258. if (shstrndx < elfcpp::SHN_LORESERVE)
  259. oshdr.put_sh_link(0);
  260. else
  261. oshdr.put_sh_link(shstrndx);
  262. size_t segment_count = this->segment_list_->size();
  263. oshdr.put_sh_info(segment_count >= elfcpp::PN_XNUM ? segment_count : 0);
  264. oshdr.put_sh_addralign(0);
  265. oshdr.put_sh_entsize(0);
  266. }
  267. v += shdr_size;
  268. unsigned int shndx = 1;
  269. if (!parameters->options().relocatable())
  270. {
  271. for (Layout::Segment_list::const_iterator p =
  272. this->segment_list_->begin();
  273. p != this->segment_list_->end();
  274. ++p)
  275. v = (*p)->write_section_headers<size, big_endian>(this->layout_,
  276. this->secnamepool_,
  277. v,
  278. &shndx);
  279. }
  280. else
  281. {
  282. for (Layout::Section_list::const_iterator p =
  283. this->section_list_->begin();
  284. p != this->section_list_->end();
  285. ++p)
  286. {
  287. // We do unallocated sections below, except that group
  288. // sections have to come first.
  289. if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
  290. && (*p)->type() != elfcpp::SHT_GROUP)
  291. continue;
  292. gold_assert(shndx == (*p)->out_shndx());
  293. elfcpp::Shdr_write<size, big_endian> oshdr(v);
  294. (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
  295. v += shdr_size;
  296. ++shndx;
  297. }
  298. }
  299. for (Layout::Section_list::const_iterator p =
  300. this->unattached_section_list_->begin();
  301. p != this->unattached_section_list_->end();
  302. ++p)
  303. {
  304. // For a relocatable link, we did unallocated group sections
  305. // above, since they have to come first.
  306. if ((*p)->type() == elfcpp::SHT_GROUP
  307. && parameters->options().relocatable())
  308. continue;
  309. gold_assert(shndx == (*p)->out_shndx());
  310. elfcpp::Shdr_write<size, big_endian> oshdr(v);
  311. (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
  312. v += shdr_size;
  313. ++shndx;
  314. }
  315. of->write_output_view(this->offset(), all_shdrs_size, view);
  316. }
  317. // Output_segment_header methods.
  318. Output_segment_headers::Output_segment_headers(
  319. const Layout::Segment_list& segment_list)
  320. : segment_list_(segment_list)
  321. {
  322. this->set_current_data_size_for_child(this->do_size());
  323. }
  324. void
  325. Output_segment_headers::do_write(Output_file* of)
  326. {
  327. switch (parameters->size_and_endianness())
  328. {
  329. #ifdef HAVE_TARGET_32_LITTLE
  330. case Parameters::TARGET_32_LITTLE:
  331. this->do_sized_write<32, false>(of);
  332. break;
  333. #endif
  334. #ifdef HAVE_TARGET_32_BIG
  335. case Parameters::TARGET_32_BIG:
  336. this->do_sized_write<32, true>(of);
  337. break;
  338. #endif
  339. #ifdef HAVE_TARGET_64_LITTLE
  340. case Parameters::TARGET_64_LITTLE:
  341. this->do_sized_write<64, false>(of);
  342. break;
  343. #endif
  344. #ifdef HAVE_TARGET_64_BIG
  345. case Parameters::TARGET_64_BIG:
  346. this->do_sized_write<64, true>(of);
  347. break;
  348. #endif
  349. default:
  350. gold_unreachable();
  351. }
  352. }
  353. template<int size, bool big_endian>
  354. void
  355. Output_segment_headers::do_sized_write(Output_file* of)
  356. {
  357. const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
  358. off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
  359. gold_assert(all_phdrs_size == this->data_size());
  360. unsigned char* view = of->get_output_view(this->offset(),
  361. all_phdrs_size);
  362. unsigned char* v = view;
  363. for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
  364. p != this->segment_list_.end();
  365. ++p)
  366. {
  367. elfcpp::Phdr_write<size, big_endian> ophdr(v);
  368. (*p)->write_header(&ophdr);
  369. v += phdr_size;
  370. }
  371. gold_assert(v - view == all_phdrs_size);
  372. of->write_output_view(this->offset(), all_phdrs_size, view);
  373. }
  374. off_t
  375. Output_segment_headers::do_size() const
  376. {
  377. const int size = parameters->target().get_size();
  378. int phdr_size;
  379. if (size == 32)
  380. phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
  381. else if (size == 64)
  382. phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
  383. else
  384. gold_unreachable();
  385. return this->segment_list_.size() * phdr_size;
  386. }
  387. // Output_file_header methods.
  388. Output_file_header::Output_file_header(Target* target,
  389. const Symbol_table* symtab,
  390. const Output_segment_headers* osh)
  391. : target_(target),
  392. symtab_(symtab),
  393. segment_header_(osh),
  394. section_header_(NULL),
  395. shstrtab_(NULL)
  396. {
  397. this->set_data_size(this->do_size());
  398. }
  399. // Set the section table information for a file header.
  400. void
  401. Output_file_header::set_section_info(const Output_section_headers* shdrs,
  402. const Output_section* shstrtab)
  403. {
  404. this->section_header_ = shdrs;
  405. this->shstrtab_ = shstrtab;
  406. }
  407. // Write out the file header.
  408. void
  409. Output_file_header::do_write(Output_file* of)
  410. {
  411. gold_assert(this->offset() == 0);
  412. switch (parameters->size_and_endianness())
  413. {
  414. #ifdef HAVE_TARGET_32_LITTLE
  415. case Parameters::TARGET_32_LITTLE:
  416. this->do_sized_write<32, false>(of);
  417. break;
  418. #endif
  419. #ifdef HAVE_TARGET_32_BIG
  420. case Parameters::TARGET_32_BIG:
  421. this->do_sized_write<32, true>(of);
  422. break;
  423. #endif
  424. #ifdef HAVE_TARGET_64_LITTLE
  425. case Parameters::TARGET_64_LITTLE:
  426. this->do_sized_write<64, false>(of);
  427. break;
  428. #endif
  429. #ifdef HAVE_TARGET_64_BIG
  430. case Parameters::TARGET_64_BIG:
  431. this->do_sized_write<64, true>(of);
  432. break;
  433. #endif
  434. default:
  435. gold_unreachable();
  436. }
  437. }
  438. // Write out the file header with appropriate size and endianness.
  439. template<int size, bool big_endian>
  440. void
  441. Output_file_header::do_sized_write(Output_file* of)
  442. {
  443. gold_assert(this->offset() == 0);
  444. int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
  445. unsigned char* view = of->get_output_view(0, ehdr_size);
  446. elfcpp::Ehdr_write<size, big_endian> oehdr(view);
  447. unsigned char e_ident[elfcpp::EI_NIDENT];
  448. memset(e_ident, 0, elfcpp::EI_NIDENT);
  449. e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
  450. e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
  451. e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
  452. e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
  453. if (size == 32)
  454. e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
  455. else if (size == 64)
  456. e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
  457. else
  458. gold_unreachable();
  459. e_ident[elfcpp::EI_DATA] = (big_endian
  460. ? elfcpp::ELFDATA2MSB
  461. : elfcpp::ELFDATA2LSB);
  462. e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
  463. oehdr.put_e_ident(e_ident);
  464. elfcpp::ET e_type;
  465. if (parameters->options().relocatable())
  466. e_type = elfcpp::ET_REL;
  467. else if (parameters->options().output_is_position_independent())
  468. e_type = elfcpp::ET_DYN;
  469. else
  470. e_type = elfcpp::ET_EXEC;
  471. oehdr.put_e_type(e_type);
  472. oehdr.put_e_machine(this->target_->machine_code());
  473. oehdr.put_e_version(elfcpp::EV_CURRENT);
  474. oehdr.put_e_entry(this->entry<size>());
  475. if (this->segment_header_ == NULL)
  476. oehdr.put_e_phoff(0);
  477. else
  478. oehdr.put_e_phoff(this->segment_header_->offset());
  479. oehdr.put_e_shoff(this->section_header_->offset());
  480. oehdr.put_e_flags(this->target_->processor_specific_flags());
  481. oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
  482. if (this->segment_header_ == NULL)
  483. {
  484. oehdr.put_e_phentsize(0);
  485. oehdr.put_e_phnum(0);
  486. }
  487. else
  488. {
  489. oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
  490. size_t phnum = (this->segment_header_->data_size()
  491. / elfcpp::Elf_sizes<size>::phdr_size);
  492. if (phnum > elfcpp::PN_XNUM)
  493. phnum = elfcpp::PN_XNUM;
  494. oehdr.put_e_phnum(phnum);
  495. }
  496. oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
  497. size_t section_count = (this->section_header_->data_size()
  498. / elfcpp::Elf_sizes<size>::shdr_size);
  499. if (section_count < elfcpp::SHN_LORESERVE)
  500. oehdr.put_e_shnum(this->section_header_->data_size()
  501. / elfcpp::Elf_sizes<size>::shdr_size);
  502. else
  503. oehdr.put_e_shnum(0);
  504. unsigned int shstrndx = this->shstrtab_->out_shndx();
  505. if (shstrndx < elfcpp::SHN_LORESERVE)
  506. oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
  507. else
  508. oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
  509. // Let the target adjust the ELF header, e.g., to set EI_OSABI in
  510. // the e_ident field.
  511. this->target_->adjust_elf_header(view, ehdr_size);
  512. of->write_output_view(0, ehdr_size, view);
  513. }
  514. // Return the value to use for the entry address.
  515. template<int size>
  516. typename elfcpp::Elf_types<size>::Elf_Addr
  517. Output_file_header::entry()
  518. {
  519. const bool should_issue_warning = (parameters->options().entry() != NULL
  520. && !parameters->options().relocatable()
  521. && !parameters->options().shared());
  522. const char* entry = parameters->entry();
  523. Symbol* sym = this->symtab_->lookup(entry);
  524. typename Sized_symbol<size>::Value_type v;
  525. if (sym != NULL)
  526. {
  527. Sized_symbol<size>* ssym;
  528. ssym = this->symtab_->get_sized_symbol<size>(sym);
  529. if (!ssym->is_defined() && should_issue_warning)
  530. gold_warning("entry symbol '%s' exists but is not defined", entry);
  531. v = ssym->value();
  532. }
  533. else
  534. {
  535. // We couldn't find the entry symbol. See if we can parse it as
  536. // a number. This supports, e.g., -e 0x1000.
  537. char* endptr;
  538. v = strtoull(entry, &endptr, 0);
  539. if (*endptr != '\0')
  540. {
  541. if (should_issue_warning)
  542. gold_warning("cannot find entry symbol '%s'", entry);
  543. v = 0;
  544. }
  545. }
  546. return v;
  547. }
  548. // Compute the current data size.
  549. off_t
  550. Output_file_header::do_size() const
  551. {
  552. const int size = parameters->target().get_size();
  553. if (size == 32)
  554. return elfcpp::Elf_sizes<32>::ehdr_size;
  555. else if (size == 64)
  556. return elfcpp::Elf_sizes<64>::ehdr_size;
  557. else
  558. gold_unreachable();
  559. }
  560. // Output_data_const methods.
  561. void
  562. Output_data_const::do_write(Output_file* of)
  563. {
  564. of->write(this->offset(), this->data_.data(), this->data_.size());
  565. }
  566. // Output_data_const_buffer methods.
  567. void
  568. Output_data_const_buffer::do_write(Output_file* of)
  569. {
  570. of->write(this->offset(), this->p_, this->data_size());
  571. }
  572. // Output_section_data methods.
  573. // Record the output section, and set the entry size and such.
  574. void
  575. Output_section_data::set_output_section(Output_section* os)
  576. {
  577. gold_assert(this->output_section_ == NULL);
  578. this->output_section_ = os;
  579. this->do_adjust_output_section(os);
  580. }
  581. // Return the section index of the output section.
  582. unsigned int
  583. Output_section_data::do_out_shndx() const
  584. {
  585. gold_assert(this->output_section_ != NULL);
  586. return this->output_section_->out_shndx();
  587. }
  588. // Set the alignment, which means we may need to update the alignment
  589. // of the output section.
  590. void
  591. Output_section_data::set_addralign(uint64_t addralign)
  592. {
  593. this->addralign_ = addralign;
  594. if (this->output_section_ != NULL
  595. && this->output_section_->addralign() < addralign)
  596. this->output_section_->set_addralign(addralign);
  597. }
  598. // Output_data_strtab methods.
  599. // Set the final data size.
  600. void
  601. Output_data_strtab::set_final_data_size()
  602. {
  603. this->strtab_->set_string_offsets();
  604. this->set_data_size(this->strtab_->get_strtab_size());
  605. }
  606. // Write out a string table.
  607. void
  608. Output_data_strtab::do_write(Output_file* of)
  609. {
  610. this->strtab_->write(of, this->offset());
  611. }
  612. // Output_reloc methods.
  613. // A reloc against a global symbol.
  614. template<bool dynamic, int size, bool big_endian>
  615. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  616. Symbol* gsym,
  617. unsigned int type,
  618. Output_data* od,
  619. Address address,
  620. bool is_relative,
  621. bool is_symbolless,
  622. bool use_plt_offset)
  623. : address_(address), local_sym_index_(GSYM_CODE), type_(type),
  624. is_relative_(is_relative), is_symbolless_(is_symbolless),
  625. is_section_symbol_(false), use_plt_offset_(use_plt_offset), shndx_(INVALID_CODE)
  626. {
  627. // this->type_ is a bitfield; make sure TYPE fits.
  628. gold_assert(this->type_ == type);
  629. this->u1_.gsym = gsym;
  630. this->u2_.od = od;
  631. if (dynamic)
  632. this->set_needs_dynsym_index();
  633. }
  634. template<bool dynamic, int size, bool big_endian>
  635. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  636. Symbol* gsym,
  637. unsigned int type,
  638. Sized_relobj<size, big_endian>* relobj,
  639. unsigned int shndx,
  640. Address address,
  641. bool is_relative,
  642. bool is_symbolless,
  643. bool use_plt_offset)
  644. : address_(address), local_sym_index_(GSYM_CODE), type_(type),
  645. is_relative_(is_relative), is_symbolless_(is_symbolless),
  646. is_section_symbol_(false), use_plt_offset_(use_plt_offset), shndx_(shndx)
  647. {
  648. gold_assert(shndx != INVALID_CODE);
  649. // this->type_ is a bitfield; make sure TYPE fits.
  650. gold_assert(this->type_ == type);
  651. this->u1_.gsym = gsym;
  652. this->u2_.relobj = relobj;
  653. if (dynamic)
  654. this->set_needs_dynsym_index();
  655. }
  656. // A reloc against a local symbol.
  657. template<bool dynamic, int size, bool big_endian>
  658. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  659. Sized_relobj<size, big_endian>* relobj,
  660. unsigned int local_sym_index,
  661. unsigned int type,
  662. Output_data* od,
  663. Address address,
  664. bool is_relative,
  665. bool is_symbolless,
  666. bool is_section_symbol,
  667. bool use_plt_offset)
  668. : address_(address), local_sym_index_(local_sym_index), type_(type),
  669. is_relative_(is_relative), is_symbolless_(is_symbolless),
  670. is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
  671. shndx_(INVALID_CODE)
  672. {
  673. gold_assert(local_sym_index != GSYM_CODE
  674. && local_sym_index != INVALID_CODE);
  675. // this->type_ is a bitfield; make sure TYPE fits.
  676. gold_assert(this->type_ == type);
  677. this->u1_.relobj = relobj;
  678. this->u2_.od = od;
  679. if (dynamic)
  680. this->set_needs_dynsym_index();
  681. }
  682. template<bool dynamic, int size, bool big_endian>
  683. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  684. Sized_relobj<size, big_endian>* relobj,
  685. unsigned int local_sym_index,
  686. unsigned int type,
  687. unsigned int shndx,
  688. Address address,
  689. bool is_relative,
  690. bool is_symbolless,
  691. bool is_section_symbol,
  692. bool use_plt_offset)
  693. : address_(address), local_sym_index_(local_sym_index), type_(type),
  694. is_relative_(is_relative), is_symbolless_(is_symbolless),
  695. is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
  696. shndx_(shndx)
  697. {
  698. gold_assert(local_sym_index != GSYM_CODE
  699. && local_sym_index != INVALID_CODE);
  700. gold_assert(shndx != INVALID_CODE);
  701. // this->type_ is a bitfield; make sure TYPE fits.
  702. gold_assert(this->type_ == type);
  703. this->u1_.relobj = relobj;
  704. this->u2_.relobj = relobj;
  705. if (dynamic)
  706. this->set_needs_dynsym_index();
  707. }
  708. // A reloc against the STT_SECTION symbol of an output section.
  709. template<bool dynamic, int size, bool big_endian>
  710. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  711. Output_section* os,
  712. unsigned int type,
  713. Output_data* od,
  714. Address address,
  715. bool is_relative)
  716. : address_(address), local_sym_index_(SECTION_CODE), type_(type),
  717. is_relative_(is_relative), is_symbolless_(is_relative),
  718. is_section_symbol_(true), use_plt_offset_(false), shndx_(INVALID_CODE)
  719. {
  720. // this->type_ is a bitfield; make sure TYPE fits.
  721. gold_assert(this->type_ == type);
  722. this->u1_.os = os;
  723. this->u2_.od = od;
  724. if (dynamic)
  725. this->set_needs_dynsym_index();
  726. else
  727. os->set_needs_symtab_index();
  728. }
  729. template<bool dynamic, int size, bool big_endian>
  730. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  731. Output_section* os,
  732. unsigned int type,
  733. Sized_relobj<size, big_endian>* relobj,
  734. unsigned int shndx,
  735. Address address,
  736. bool is_relative)
  737. : address_(address), local_sym_index_(SECTION_CODE), type_(type),
  738. is_relative_(is_relative), is_symbolless_(is_relative),
  739. is_section_symbol_(true), use_plt_offset_(false), shndx_(shndx)
  740. {
  741. gold_assert(shndx != INVALID_CODE);
  742. // this->type_ is a bitfield; make sure TYPE fits.
  743. gold_assert(this->type_ == type);
  744. this->u1_.os = os;
  745. this->u2_.relobj = relobj;
  746. if (dynamic)
  747. this->set_needs_dynsym_index();
  748. else
  749. os->set_needs_symtab_index();
  750. }
  751. // An absolute or relative relocation.
  752. template<bool dynamic, int size, bool big_endian>
  753. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  754. unsigned int type,
  755. Output_data* od,
  756. Address address,
  757. bool is_relative)
  758. : address_(address), local_sym_index_(0), type_(type),
  759. is_relative_(is_relative), is_symbolless_(false),
  760. is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
  761. {
  762. // this->type_ is a bitfield; make sure TYPE fits.
  763. gold_assert(this->type_ == type);
  764. this->u1_.relobj = NULL;
  765. this->u2_.od = od;
  766. }
  767. template<bool dynamic, int size, bool big_endian>
  768. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  769. unsigned int type,
  770. Sized_relobj<size, big_endian>* relobj,
  771. unsigned int shndx,
  772. Address address,
  773. bool is_relative)
  774. : address_(address), local_sym_index_(0), type_(type),
  775. is_relative_(is_relative), is_symbolless_(false),
  776. is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
  777. {
  778. gold_assert(shndx != INVALID_CODE);
  779. // this->type_ is a bitfield; make sure TYPE fits.
  780. gold_assert(this->type_ == type);
  781. this->u1_.relobj = NULL;
  782. this->u2_.relobj = relobj;
  783. }
  784. // A target specific relocation.
  785. template<bool dynamic, int size, bool big_endian>
  786. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  787. unsigned int type,
  788. void* arg,
  789. Output_data* od,
  790. Address address)
  791. : address_(address), local_sym_index_(TARGET_CODE), type_(type),
  792. is_relative_(false), is_symbolless_(false),
  793. is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
  794. {
  795. // this->type_ is a bitfield; make sure TYPE fits.
  796. gold_assert(this->type_ == type);
  797. this->u1_.arg = arg;
  798. this->u2_.od = od;
  799. }
  800. template<bool dynamic, int size, bool big_endian>
  801. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
  802. unsigned int type,
  803. void* arg,
  804. Sized_relobj<size, big_endian>* relobj,
  805. unsigned int shndx,
  806. Address address)
  807. : address_(address), local_sym_index_(TARGET_CODE), type_(type),
  808. is_relative_(false), is_symbolless_(false),
  809. is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
  810. {
  811. gold_assert(shndx != INVALID_CODE);
  812. // this->type_ is a bitfield; make sure TYPE fits.
  813. gold_assert(this->type_ == type);
  814. this->u1_.arg = arg;
  815. this->u2_.relobj = relobj;
  816. }
  817. // Record that we need a dynamic symbol index for this relocation.
  818. template<bool dynamic, int size, bool big_endian>
  819. void
  820. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
  821. set_needs_dynsym_index()
  822. {
  823. if (this->is_symbolless_)
  824. return;
  825. switch (this->local_sym_index_)
  826. {
  827. case INVALID_CODE:
  828. gold_unreachable();
  829. case GSYM_CODE:
  830. this->u1_.gsym->set_needs_dynsym_entry();
  831. break;
  832. case SECTION_CODE:
  833. this->u1_.os->set_needs_dynsym_index();
  834. break;
  835. case TARGET_CODE:
  836. // The target must take care of this if necessary.
  837. break;
  838. case 0:
  839. break;
  840. default:
  841. {
  842. const unsigned int lsi = this->local_sym_index_;
  843. Sized_relobj_file<size, big_endian>* relobj =
  844. this->u1_.relobj->sized_relobj();
  845. gold_assert(relobj != NULL);
  846. if (!this->is_section_symbol_)
  847. relobj->set_needs_output_dynsym_entry(lsi);
  848. else
  849. relobj->output_section(lsi)->set_needs_dynsym_index();
  850. }
  851. break;
  852. }
  853. }
  854. // Get the symbol index of a relocation.
  855. template<bool dynamic, int size, bool big_endian>
  856. unsigned int
  857. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
  858. const
  859. {
  860. unsigned int index;
  861. if (this->is_symbolless_)
  862. return 0;
  863. switch (this->local_sym_index_)
  864. {
  865. case INVALID_CODE:
  866. gold_unreachable();
  867. case GSYM_CODE:
  868. if (this->u1_.gsym == NULL)
  869. index = 0;
  870. else if (dynamic)
  871. index = this->u1_.gsym->dynsym_index();
  872. else
  873. index = this->u1_.gsym->symtab_index();
  874. break;
  875. case SECTION_CODE:
  876. if (dynamic)
  877. index = this->u1_.os->dynsym_index();
  878. else
  879. index = this->u1_.os->symtab_index();
  880. break;
  881. case TARGET_CODE:
  882. index = parameters->target().reloc_symbol_index(this->u1_.arg,
  883. this->type_);
  884. break;
  885. case 0:
  886. // Relocations without symbols use a symbol index of 0.
  887. index = 0;
  888. break;
  889. default:
  890. {
  891. const unsigned int lsi = this->local_sym_index_;
  892. Sized_relobj_file<size, big_endian>* relobj =
  893. this->u1_.relobj->sized_relobj();
  894. gold_assert(relobj != NULL);
  895. if (!this->is_section_symbol_)
  896. {
  897. if (dynamic)
  898. index = relobj->dynsym_index(lsi);
  899. else
  900. index = relobj->symtab_index(lsi);
  901. }
  902. else
  903. {
  904. Output_section* os = relobj->output_section(lsi);
  905. gold_assert(os != NULL);
  906. if (dynamic)
  907. index = os->dynsym_index();
  908. else
  909. index = os->symtab_index();
  910. }
  911. }
  912. break;
  913. }
  914. gold_assert(index != -1U);
  915. return index;
  916. }
  917. // For a local section symbol, get the address of the offset ADDEND
  918. // within the input section.
  919. template<bool dynamic, int size, bool big_endian>
  920. typename elfcpp::Elf_types<size>::Elf_Addr
  921. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
  922. local_section_offset(Addend addend) const
  923. {
  924. gold_assert(this->local_sym_index_ != GSYM_CODE
  925. && this->local_sym_index_ != SECTION_CODE
  926. && this->local_sym_index_ != TARGET_CODE
  927. && this->local_sym_index_ != INVALID_CODE
  928. && this->local_sym_index_ != 0
  929. && this->is_section_symbol_);
  930. const unsigned int lsi = this->local_sym_index_;
  931. Output_section* os = this->u1_.relobj->output_section(lsi);
  932. gold_assert(os != NULL);
  933. Address offset = this->u1_.relobj->get_output_section_offset(lsi);
  934. if (offset != invalid_address)
  935. return offset + addend;
  936. // This is a merge section.
  937. Sized_relobj_file<size, big_endian>* relobj =
  938. this->u1_.relobj->sized_relobj();
  939. gold_assert(relobj != NULL);
  940. offset = os->output_address(relobj, lsi, addend);
  941. gold_assert(offset != invalid_address);
  942. return offset;
  943. }
  944. // Get the output address of a relocation.
  945. template<bool dynamic, int size, bool big_endian>
  946. typename elfcpp::Elf_types<size>::Elf_Addr
  947. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
  948. {
  949. Address address = this->address_;
  950. if (this->shndx_ != INVALID_CODE)
  951. {
  952. Output_section* os = this->u2_.relobj->output_section(this->shndx_);
  953. gold_assert(os != NULL);
  954. Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
  955. if (off != invalid_address)
  956. address += os->address() + off;
  957. else
  958. {
  959. Sized_relobj_file<size, big_endian>* relobj =
  960. this->u2_.relobj->sized_relobj();
  961. gold_assert(relobj != NULL);
  962. address = os->output_address(relobj, this->shndx_, address);
  963. gold_assert(address != invalid_address);
  964. }
  965. }
  966. else if (this->u2_.od != NULL)
  967. address += this->u2_.od->address();
  968. return address;
  969. }
  970. // Write out the offset and info fields of a Rel or Rela relocation
  971. // entry.
  972. template<bool dynamic, int size, bool big_endian>
  973. template<typename Write_rel>
  974. void
  975. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
  976. Write_rel* wr) const
  977. {
  978. wr->put_r_offset(this->get_address());
  979. unsigned int sym_index = this->get_symbol_index();
  980. wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
  981. }
  982. // Write out a Rel relocation.
  983. template<bool dynamic, int size, bool big_endian>
  984. void
  985. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
  986. unsigned char* pov) const
  987. {
  988. elfcpp::Rel_write<size, big_endian> orel(pov);
  989. this->write_rel(&orel);
  990. }
  991. // Get the value of the symbol referred to by a Rel relocation.
  992. template<bool dynamic, int size, bool big_endian>
  993. typename elfcpp::Elf_types<size>::Elf_Addr
  994. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
  995. Addend addend) const
  996. {
  997. if (this->local_sym_index_ == GSYM_CODE)
  998. {
  999. const Sized_symbol<size>* sym;
  1000. sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
  1001. if (this->use_plt_offset_ && sym->has_plt_offset())
  1002. return parameters->target().plt_address_for_global(sym);
  1003. else
  1004. return sym->value() + addend;
  1005. }
  1006. if (this->local_sym_index_ == SECTION_CODE)
  1007. {
  1008. gold_assert(!this->use_plt_offset_);
  1009. return this->u1_.os->address() + addend;
  1010. }
  1011. gold_assert(this->local_sym_index_ != TARGET_CODE
  1012. && this->local_sym_index_ != INVALID_CODE
  1013. && this->local_sym_index_ != 0
  1014. && !this->is_section_symbol_);
  1015. const unsigned int lsi = this->local_sym_index_;
  1016. Sized_relobj_file<size, big_endian>* relobj =
  1017. this->u1_.relobj->sized_relobj();
  1018. gold_assert(relobj != NULL);
  1019. if (this->use_plt_offset_)
  1020. return parameters->target().plt_address_for_local(relobj, lsi);
  1021. const Symbol_value<size>* symval = relobj->local_symbol(lsi);
  1022. return symval->value(relobj, addend);
  1023. }
  1024. // Reloc comparison. This function sorts the dynamic relocs for the
  1025. // benefit of the dynamic linker. First we sort all relative relocs
  1026. // to the front. Among relative relocs, we sort by output address.
  1027. // Among non-relative relocs, we sort by symbol index, then by output
  1028. // address.
  1029. template<bool dynamic, int size, bool big_endian>
  1030. int
  1031. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
  1032. compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
  1033. const
  1034. {
  1035. if (this->is_relative_)
  1036. {
  1037. if (!r2.is_relative_)
  1038. return -1;
  1039. // Otherwise sort by reloc address below.
  1040. }
  1041. else if (r2.is_relative_)
  1042. return 1;
  1043. else
  1044. {
  1045. unsigned int sym1 = this->get_symbol_index();
  1046. unsigned int sym2 = r2.get_symbol_index();
  1047. if (sym1 < sym2)
  1048. return -1;
  1049. else if (sym1 > sym2)
  1050. return 1;
  1051. // Otherwise sort by reloc address.
  1052. }
  1053. section_offset_type addr1 = this->get_address();
  1054. section_offset_type addr2 = r2.get_address();
  1055. if (addr1 < addr2)
  1056. return -1;
  1057. else if (addr1 > addr2)
  1058. return 1;
  1059. // Final tie breaker, in order to generate the same output on any
  1060. // host: reloc type.
  1061. unsigned int type1 = this->type_;
  1062. unsigned int type2 = r2.type_;
  1063. if (type1 < type2)
  1064. return -1;
  1065. else if (type1 > type2)
  1066. return 1;
  1067. // These relocs appear to be exactly the same.
  1068. return 0;
  1069. }
  1070. // Write out a Rela relocation.
  1071. template<bool dynamic, int size, bool big_endian>
  1072. void
  1073. Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
  1074. unsigned char* pov) const
  1075. {
  1076. elfcpp::Rela_write<size, big_endian> orel(pov);
  1077. this->rel_.write_rel(&orel);
  1078. Addend addend = this->addend_;
  1079. if (this->rel_.is_target_specific())
  1080. addend = parameters->target().reloc_addend(this->rel_.target_arg(),
  1081. this->rel_.type(), addend);
  1082. else if (this->rel_.is_symbolless())
  1083. addend = this->rel_.symbol_value(addend);
  1084. else if (this->rel_.is_local_section_symbol())
  1085. addend = this->rel_.local_section_offset(addend);
  1086. orel.put_r_addend(addend);
  1087. }
  1088. // Output_data_reloc_base methods.
  1089. // Adjust the output section.
  1090. template<int sh_type, bool dynamic, int size, bool big_endian>
  1091. void
  1092. Output_data_reloc_base<sh_type, dynamic, size, big_endian>
  1093. ::do_adjust_output_section(Output_section* os)
  1094. {
  1095. if (sh_type == elfcpp::SHT_REL)
  1096. os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
  1097. else if (sh_type == elfcpp::SHT_RELA)
  1098. os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
  1099. else
  1100. gold_unreachable();
  1101. // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
  1102. // static link. The backends will generate a dynamic reloc section
  1103. // to hold this. In that case we don't want to link to the dynsym
  1104. // section, because there isn't one.
  1105. if (!dynamic)
  1106. os->set_should_link_to_symtab();
  1107. else if (parameters->doing_static_link())
  1108. ;
  1109. else
  1110. os->set_should_link_to_dynsym();
  1111. }
  1112. // Standard relocation writer, which just calls Output_reloc::write().
  1113. template<int sh_type, bool dynamic, int size, bool big_endian>
  1114. struct Output_reloc_writer
  1115. {
  1116. typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
  1117. typedef std::vector<Output_reloc_type> Relocs;
  1118. static void
  1119. write(typename Relocs::const_iterator p, unsigned char* pov)
  1120. { p->write(pov); }
  1121. };
  1122. // Write out relocation data.
  1123. template<int sh_type, bool dynamic, int size, bool big_endian>
  1124. void
  1125. Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
  1126. Output_file* of)
  1127. {
  1128. typedef Output_reloc_writer<sh_type, dynamic, size, big_endian> Writer;
  1129. this->do_write_generic<Writer>(of);
  1130. }
  1131. // Class Output_relocatable_relocs.
  1132. template<int sh_type, int size, bool big_endian>
  1133. void
  1134. Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
  1135. {
  1136. this->set_data_size(this->rr_->output_reloc_count()
  1137. * Reloc_types<sh_type, size, big_endian>::reloc_size);
  1138. }
  1139. // class Output_data_group.
  1140. template<int size, bool big_endian>
  1141. Output_data_group<size, big_endian>::Output_data_group(
  1142. Sized_relobj_file<size, big_endian>* relobj,
  1143. section_size_type entry_count,
  1144. elfcpp::Elf_Word flags,
  1145. std::vector<unsigned int>* input_shndxes)
  1146. : Output_section_data(entry_count * 4, 4, false),
  1147. relobj_(relobj),
  1148. flags_(flags)
  1149. {
  1150. this->input_shndxes_.swap(*input_shndxes);
  1151. }
  1152. // Write out the section group, which means translating the section
  1153. // indexes to apply to the output file.
  1154. template<int size, bool big_endian>
  1155. void
  1156. Output_data_group<size, big_endian>::do_write(Output_file* of)
  1157. {
  1158. const off_t off = this->offset();
  1159. const section_size_type oview_size =
  1160. convert_to_section_size_type(this->data_size());
  1161. unsigned char* const oview = of->get_output_view(off, oview_size);
  1162. elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
  1163. elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
  1164. ++contents;
  1165. for (std::vector<unsigned int>::const_iterator p =
  1166. this->input_shndxes_.begin();
  1167. p != this->input_shndxes_.end();
  1168. ++p, ++contents)
  1169. {
  1170. Output_section* os = this->relobj_->output_section(*p);
  1171. unsigned int output_shndx;
  1172. if (os != NULL)
  1173. output_shndx = os->out_shndx();
  1174. else
  1175. {
  1176. this->relobj_->error(_("section group retained but "
  1177. "group element discarded"));
  1178. output_shndx = 0;
  1179. }
  1180. elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
  1181. }
  1182. size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
  1183. gold_assert(wrote == oview_size);
  1184. of->write_output_view(off, oview_size, oview);
  1185. // We no longer need this information.
  1186. this->input_shndxes_.clear();
  1187. }
  1188. // Output_data_got::Got_entry methods.
  1189. // Write out the entry.
  1190. template<int got_size, bool big_endian>
  1191. void
  1192. Output_data_got<got_size, big_endian>::Got_entry::write(
  1193. Output_data_got_base* got,
  1194. unsigned int got_indx,
  1195. unsigned char* pov) const
  1196. {
  1197. Valtype val = 0;
  1198. switch (this->local_sym_index_)
  1199. {
  1200. case GSYM_CODE:
  1201. {
  1202. // If the symbol is resolved locally, we need to write out the
  1203. // link-time value, which will be relocated dynamically by a
  1204. // RELATIVE relocation.
  1205. Symbol* gsym = this->u_.gsym;
  1206. if (this->use_plt_or_tls_offset_ && gsym->has_plt_offset())
  1207. val = parameters->target().plt_address_for_global(gsym);
  1208. else
  1209. {
  1210. switch (parameters->size_and_endianness())
  1211. {
  1212. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  1213. case Parameters::TARGET_32_LITTLE:
  1214. case Parameters::TARGET_32_BIG:
  1215. {
  1216. // This cast is ugly. We don't want to put a
  1217. // virtual method in Symbol, because we want Symbol
  1218. // to be as small as possible.
  1219. Sized_symbol<32>::Value_type v;
  1220. v = static_cast<Sized_symbol<32>*>(gsym)->value();
  1221. val = convert_types<Valtype, Sized_symbol<32>::Value_type>(v);
  1222. }
  1223. break;
  1224. #endif
  1225. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  1226. case Parameters::TARGET_64_LITTLE:
  1227. case Parameters::TARGET_64_BIG:
  1228. {
  1229. Sized_symbol<64>::Value_type v;
  1230. v = static_cast<Sized_symbol<64>*>(gsym)->value();
  1231. val = convert_types<Valtype, Sized_symbol<64>::Value_type>(v);
  1232. }
  1233. break;
  1234. #endif
  1235. default:
  1236. gold_unreachable();
  1237. }
  1238. // If this is a GOT entry for a known value global symbol,
  1239. // then the value should include the addend. If the value
  1240. // is not known leave the value as zero; The GOT entry
  1241. // will be set by a dynamic relocation.
  1242. if (this->addend_ && gsym->final_value_is_known())
  1243. val += this->addend_;
  1244. if (this->use_plt_or_tls_offset_
  1245. && gsym->type() == elfcpp::STT_TLS)
  1246. val += parameters->target().tls_offset_for_global(gsym,
  1247. got, got_indx,
  1248. this->addend_);
  1249. }
  1250. }
  1251. break;
  1252. case CONSTANT_CODE:
  1253. val = this->u_.constant;
  1254. break;
  1255. case RESERVED_CODE:
  1256. // If we're doing an incremental update, don't touch this GOT entry.
  1257. if (parameters->incremental_update())
  1258. return;
  1259. val = this->u_.constant;
  1260. break;
  1261. default:
  1262. {
  1263. const Relobj* object = this->u_.object;
  1264. const unsigned int lsi = this->local_sym_index_;
  1265. bool is_tls = object->local_is_tls(lsi);
  1266. if (this->use_plt_or_tls_offset_ && !is_tls)
  1267. val = parameters->target().plt_address_for_local(object, lsi);
  1268. else
  1269. {
  1270. uint64_t lval = object->local_symbol_value(lsi, this->addend_);
  1271. val = convert_types<Valtype, uint64_t>(lval);
  1272. if (this->use_plt_or_tls_offset_ && is_tls)
  1273. val += parameters->target().tls_offset_for_local(object, lsi,
  1274. got, got_indx,
  1275. this->addend_);
  1276. }
  1277. }
  1278. break;
  1279. }
  1280. elfcpp::Swap<got_size, big_endian>::writeval(pov, val);
  1281. }
  1282. // Output_data_got methods.
  1283. // Add an entry for a global symbol to the GOT. This returns true if
  1284. // this is a new GOT entry, false if the symbol already had a GOT
  1285. // entry.
  1286. template<int got_size, bool big_endian>
  1287. bool
  1288. Output_data_got<got_size, big_endian>::add_global(Symbol* gsym,
  1289. unsigned int got_type,
  1290. uint64_t addend)
  1291. {
  1292. if (gsym->has_got_offset(got_type, addend))
  1293. return false;
  1294. unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false, addend));
  1295. gsym->set_got_offset(got_type, got_offset, addend);
  1296. return true;
  1297. }
  1298. // Like add_global, but use the PLT offset.
  1299. template<int got_size, bool big_endian>
  1300. bool
  1301. Output_data_got<got_size, big_endian>::add_global_plt(Symbol* gsym,
  1302. unsigned int got_type,
  1303. uint64_t addend)
  1304. {
  1305. if (gsym->has_got_offset(got_type, addend))
  1306. return false;
  1307. unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true, addend));
  1308. gsym->set_got_offset(got_type, got_offset, addend);
  1309. return true;
  1310. }
  1311. // Add an entry for a global symbol to the GOT, and add a dynamic
  1312. // relocation of type R_TYPE for the GOT entry.
  1313. template<int got_size, bool big_endian>
  1314. void
  1315. Output_data_got<got_size, big_endian>::add_global_with_rel(
  1316. Symbol* gsym,
  1317. unsigned int got_type,
  1318. Output_data_reloc_generic* rel_dyn,
  1319. unsigned int r_type,
  1320. uint64_t addend)
  1321. {
  1322. if (gsym->has_got_offset(got_type, addend))
  1323. return;
  1324. unsigned int got_offset = this->add_got_entry(Got_entry());
  1325. gsym->set_got_offset(got_type, got_offset, addend);
  1326. rel_dyn->add_global_generic(gsym, r_type, this, got_offset, addend);
  1327. }
  1328. // Add a pair of entries for a global symbol to the GOT, and add
  1329. // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
  1330. // If R_TYPE_2 == 0, add the second entry with no relocation.
  1331. template<int got_size, bool big_endian>
  1332. void
  1333. Output_data_got<got_size, big_endian>::add_global_pair_with_rel(
  1334. Symbol* gsym,
  1335. unsigned int got_type,
  1336. Output_data_reloc_generic* rel_dyn,
  1337. unsigned int r_type_1,
  1338. unsigned int r_type_2,
  1339. uint64_t addend)
  1340. {
  1341. if (gsym->has_got_offset(got_type, addend))
  1342. return;
  1343. unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
  1344. gsym->set_got_offset(got_type, got_offset, addend);
  1345. rel_dyn->add_global_generic(gsym, r_type_1, this, got_offset, addend);
  1346. if (r_type_2 != 0)
  1347. rel_dyn->add_global_generic(gsym, r_type_2, this,
  1348. got_offset + got_size / 8, addend);
  1349. }
  1350. // Add an entry for a local symbol plus ADDEND to the GOT. This returns
  1351. // true if this is a new GOT entry, false if the symbol already has a GOT
  1352. // entry.
  1353. template<int got_size, bool big_endian>
  1354. bool
  1355. Output_data_got<got_size, big_endian>::add_local(
  1356. Relobj* object,
  1357. unsigned int symndx,
  1358. unsigned int got_type,
  1359. uint64_t addend)
  1360. {
  1361. if (object->local_has_got_offset(symndx, got_type, addend))
  1362. return false;
  1363. unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
  1364. false, addend));
  1365. object->set_local_got_offset(symndx, got_type, got_offset, addend);
  1366. return true;
  1367. }
  1368. // Like add_local, but use the PLT offset.
  1369. template<int got_size, bool big_endian>
  1370. bool
  1371. Output_data_got<got_size, big_endian>::add_local_plt(
  1372. Relobj* object,
  1373. unsigned int symndx,
  1374. unsigned int got_type,
  1375. uint64_t addend)
  1376. {
  1377. if (object->local_has_got_offset(symndx, got_type, addend))
  1378. return false;
  1379. unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
  1380. true, addend));
  1381. object->set_local_got_offset(symndx, got_type, got_offset, addend);
  1382. return true;
  1383. }
  1384. // Add an entry for a local symbol plus ADDEND to the GOT, and add a dynamic
  1385. // relocation of type R_TYPE for the GOT entry.
  1386. template<int got_size, bool big_endian>
  1387. void
  1388. Output_data_got<got_size, big_endian>::add_local_with_rel(
  1389. Relobj* object,
  1390. unsigned int symndx,
  1391. unsigned int got_type,
  1392. Output_data_reloc_generic* rel_dyn,
  1393. unsigned int r_type,
  1394. uint64_t addend)
  1395. {
  1396. if (object->local_has_got_offset(symndx, got_type, addend))
  1397. return;
  1398. unsigned int got_offset = this->add_got_entry(Got_entry());
  1399. object->set_local_got_offset(symndx, got_type, got_offset, addend);
  1400. rel_dyn->add_local_generic(object, symndx, r_type, this, got_offset,
  1401. addend);
  1402. }
  1403. // Add a pair of entries for a local symbol plus ADDEND to the GOT, and add
  1404. // a dynamic relocation of type R_TYPE using the section symbol of
  1405. // the output section to which input section SHNDX maps, on the first.
  1406. // The first got entry will have a value of zero, the second the
  1407. // value of the local symbol.
  1408. template<int got_size, bool big_endian>
  1409. void
  1410. Output_data_got<got_size, big_endian>::add_local_pair_with_rel(
  1411. Relobj* object,
  1412. unsigned int symndx,
  1413. unsigned int shndx,
  1414. unsigned int got_type,
  1415. Output_data_reloc_generic* rel_dyn,
  1416. unsigned int r_type,
  1417. uint64_t addend)
  1418. {
  1419. if (object->local_has_got_offset(symndx, got_type, addend))
  1420. return;
  1421. unsigned int got_offset =
  1422. this->add_got_entry_pair(Got_entry(),
  1423. Got_entry(object, symndx, false, addend));
  1424. object->set_local_got_offset(symndx, got_type, got_offset, addend);
  1425. Output_section* os = object->output_section(shndx);
  1426. rel_dyn->add_output_section_generic(os, r_type, this, got_offset, addend);
  1427. }
  1428. // Add a pair of entries for a local symbol to the GOT, and add
  1429. // a dynamic relocation of type R_TYPE using STN_UNDEF on the first.
  1430. // The first got entry will have a value of zero, the second the
  1431. // value of the local symbol offset by Target::tls_offset_for_local.
  1432. template<int got_size, bool big_endian>
  1433. void
  1434. Output_data_got<got_size, big_endian>::add_local_tls_pair(
  1435. Relobj* object,
  1436. unsigned int symndx,
  1437. unsigned int got_type,
  1438. Output_data_reloc_generic* rel_dyn,
  1439. unsigned int r_type,
  1440. uint64_t addend)
  1441. {
  1442. if (object->local_has_got_offset(symndx, got_type, addend))
  1443. return;
  1444. unsigned int got_offset
  1445. = this->add_got_entry_pair(Got_entry(),
  1446. Got_entry(object, symndx, true, addend));
  1447. object->set_local_got_offset(symndx, got_type, got_offset, addend);
  1448. rel_dyn->add_local_generic(object, 0, r_type, this, got_offset, addend);
  1449. }
  1450. // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
  1451. template<int got_size, bool big_endian>
  1452. void
  1453. Output_data_got<got_size, big_endian>::reserve_local(
  1454. unsigned int i,
  1455. Relobj* object,
  1456. unsigned int sym_index,
  1457. unsigned int got_type,
  1458. uint64_t addend)
  1459. {
  1460. this->do_reserve_slot(i);
  1461. object->set_local_got_offset(sym_index, got_type, this->got_offset(i), addend);
  1462. }
  1463. // Reserve a slot in the GOT for a global symbol.
  1464. template<int got_size, bool big_endian>
  1465. void
  1466. Output_data_got<got_size, big_endian>::reserve_global(
  1467. unsigned int i,
  1468. Symbol* gsym,
  1469. unsigned int got_type,
  1470. uint64_t addend)
  1471. {
  1472. this->do_reserve_slot(i);
  1473. gsym->set_got_offset(got_type, this->got_offset(i), addend);
  1474. }
  1475. // Write out the GOT.
  1476. template<int got_size, bool big_endian>
  1477. void
  1478. Output_data_got<got_size, big_endian>::do_write(Output_file* of)
  1479. {
  1480. const int add = got_size / 8;
  1481. const off_t off = this->offset();
  1482. const off_t oview_size = this->data_size();
  1483. unsigned char* const oview = of->get_output_view(off, oview_size);
  1484. unsigned char* pov = oview;
  1485. for (unsigned int i = 0; i < this->entries_.size(); ++i)
  1486. {
  1487. this->entries_[i].write(this, i, pov);
  1488. pov += add;
  1489. }
  1490. gold_assert(pov - oview == oview_size);
  1491. of->write_output_view(off, oview_size, oview);
  1492. // We no longer need the GOT entries.
  1493. this->entries_.clear();
  1494. }
  1495. // Create a new GOT entry and return its offset.
  1496. template<int got_size, bool big_endian>
  1497. unsigned int
  1498. Output_data_got<got_size, big_endian>::add_got_entry(Got_entry got_entry)
  1499. {
  1500. if (!this->is_data_size_valid())
  1501. {
  1502. this->entries_.push_back(got_entry);
  1503. this->set_got_size();
  1504. return this->last_got_offset();
  1505. }
  1506. else
  1507. {
  1508. // For an incremental update, find an available slot.
  1509. off_t got_offset = this->free_list_.allocate(got_size / 8,
  1510. got_size / 8, 0);
  1511. if (got_offset == -1)
  1512. gold_fallback(_("out of patch space (GOT);"
  1513. " relink with --incremental-full"));
  1514. unsigned int got_index = got_offset / (got_size / 8);
  1515. gold_assert(got_index < this->entries_.size());
  1516. this->entries_[got_index] = got_entry;
  1517. return static_cast<unsigned int>(got_offset);
  1518. }
  1519. }
  1520. // Create a pair of new GOT entries and return the offset of the first.
  1521. template<int got_size, bool big_endian>
  1522. unsigned int
  1523. Output_data_got<got_size, big_endian>::add_got_entry_pair(
  1524. Got_entry got_entry_1,
  1525. Got_entry got_entry_2)
  1526. {
  1527. if (!this->is_data_size_valid())
  1528. {
  1529. unsigned int got_offset;
  1530. this->entries_.push_back(got_entry_1);
  1531. got_offset = this->last_got_offset();
  1532. this->entries_.push_back(got_entry_2);
  1533. this->set_got_size();
  1534. return got_offset;
  1535. }
  1536. else
  1537. {
  1538. // For an incremental update, find an available pair of slots.
  1539. off_t got_offset = this->free_list_.allocate(2 * got_size / 8,
  1540. got_size / 8, 0);
  1541. if (got_offset == -1)
  1542. gold_fallback(_("out of patch space (GOT);"
  1543. " relink with --incremental-full"));
  1544. unsigned int got_index = got_offset / (got_size / 8);
  1545. gold_assert(got_index < this->entries_.size());
  1546. this->entries_[got_index] = got_entry_1;
  1547. this->entries_[got_index + 1] = got_entry_2;
  1548. return static_cast<unsigned int>(got_offset);
  1549. }
  1550. }
  1551. // Replace GOT entry I with a new value.
  1552. template<int got_size, bool big_endian>
  1553. void
  1554. Output_data_got<got_size, big_endian>::replace_got_entry(
  1555. unsigned int i,
  1556. Got_entry got_entry)
  1557. {
  1558. gold_assert(i < this->entries_.size());
  1559. this->entries_[i] = got_entry;
  1560. }
  1561. // Output_data_dynamic::Dynamic_entry methods.
  1562. // Write out the entry.
  1563. template<int size, bool big_endian>
  1564. void
  1565. Output_data_dynamic::Dynamic_entry::write(
  1566. unsigned char* pov,
  1567. const Stringpool* pool) const
  1568. {
  1569. typename elfcpp::Elf_types<size>::Elf_WXword val;
  1570. switch (this->offset_)
  1571. {
  1572. case DYNAMIC_NUMBER:
  1573. val = this->u_.val;
  1574. break;
  1575. case DYNAMIC_SECTION_SIZE:
  1576. val = this->u_.od->data_size();
  1577. if (this->od2 != NULL)
  1578. val += this->od2->data_size();
  1579. break;
  1580. case DYNAMIC_SYMBOL:
  1581. {
  1582. const Sized_symbol<size>* s =
  1583. static_cast<const Sized_symbol<size>*>(this->u_.sym);
  1584. val = s->value();
  1585. }
  1586. break;
  1587. case DYNAMIC_STRING:
  1588. val = pool->get_offset(this->u_.str);
  1589. break;
  1590. case DYNAMIC_CUSTOM:
  1591. val = parameters->target().dynamic_tag_custom_value(this->tag_);
  1592. break;
  1593. default:
  1594. val = this->u_.od->address() + this->offset_;
  1595. break;
  1596. }
  1597. elfcpp::Dyn_write<size, big_endian> dw(pov);
  1598. dw.put_d_tag(this->tag_);
  1599. dw.put_d_val(val);
  1600. }
  1601. // Output_data_dynamic methods.
  1602. // Adjust the output section to set the entry size.
  1603. void
  1604. Output_data_dynamic::do_adjust_output_section(Output_section* os)
  1605. {
  1606. if (parameters->target().get_size() == 32)
  1607. os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
  1608. else if (parameters->target().get_size() == 64)
  1609. os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
  1610. else
  1611. gold_unreachable();
  1612. }
  1613. // Get a dynamic entry offset.
  1614. unsigned int
  1615. Output_data_dynamic::get_entry_offset(elfcpp::DT tag) const
  1616. {
  1617. int dyn_size;
  1618. if (parameters->target().get_size() == 32)
  1619. dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
  1620. else if (parameters->target().get_size() == 64)
  1621. dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
  1622. else
  1623. gold_unreachable();
  1624. for (size_t i = 0; i < entries_.size(); ++i)
  1625. if (entries_[i].tag() == tag)
  1626. return i * dyn_size;
  1627. return -1U;
  1628. }
  1629. // Set the final data size.
  1630. void
  1631. Output_data_dynamic::set_final_data_size()
  1632. {
  1633. // Add the terminating entry if it hasn't been added.
  1634. // Because of relaxation, we can run this multiple times.
  1635. if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
  1636. {
  1637. int extra = parameters->options().spare_dynamic_tags();
  1638. for (int i = 0; i < extra; ++i)
  1639. this->add_constant(elfcpp::DT_NULL, 0);
  1640. this->add_constant(elfcpp::DT_NULL, 0);
  1641. }
  1642. int dyn_size;
  1643. if (parameters->target().get_size() == 32)
  1644. dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
  1645. else if (parameters->target().get_size() == 64)
  1646. dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
  1647. else
  1648. gold_unreachable();
  1649. this->set_data_size(this->entries_.size() * dyn_size);
  1650. }
  1651. // Write out the dynamic entries.
  1652. void
  1653. Output_data_dynamic::do_write(Output_file* of)
  1654. {
  1655. switch (parameters->size_and_endianness())
  1656. {
  1657. #ifdef HAVE_TARGET_32_LITTLE
  1658. case Parameters::TARGET_32_LITTLE:
  1659. this->sized_write<32, false>(of);
  1660. break;
  1661. #endif
  1662. #ifdef HAVE_TARGET_32_BIG
  1663. case Parameters::TARGET_32_BIG:
  1664. this->sized_write<32, true>(of);
  1665. break;
  1666. #endif
  1667. #ifdef HAVE_TARGET_64_LITTLE
  1668. case Parameters::TARGET_64_LITTLE:
  1669. this->sized_write<64, false>(of);
  1670. break;
  1671. #endif
  1672. #ifdef HAVE_TARGET_64_BIG
  1673. case Parameters::TARGET_64_BIG:
  1674. this->sized_write<64, true>(of);
  1675. break;
  1676. #endif
  1677. default:
  1678. gold_unreachable();
  1679. }
  1680. }
  1681. template<int size, bool big_endian>
  1682. void
  1683. Output_data_dynamic::sized_write(Output_file* of)
  1684. {
  1685. const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
  1686. const off_t offset = this->offset();
  1687. const off_t oview_size = this->data_size();
  1688. unsigned char* const oview = of->get_output_view(offset, oview_size);
  1689. unsigned char* pov = oview;
  1690. for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
  1691. p != this->entries_.end();
  1692. ++p)
  1693. {
  1694. p->write<size, big_endian>(pov, this->pool_);
  1695. pov += dyn_size;
  1696. }
  1697. gold_assert(pov - oview == oview_size);
  1698. of->write_output_view(offset, oview_size, oview);
  1699. // We no longer need the dynamic entries.
  1700. this->entries_.clear();
  1701. }
  1702. // Class Output_symtab_xindex.
  1703. void
  1704. Output_symtab_xindex::do_write(Output_file* of)
  1705. {
  1706. const off_t offset = this->offset();
  1707. const off_t oview_size = this->data_size();
  1708. unsigned char* const oview = of->get_output_view(offset, oview_size);
  1709. memset(oview, 0, oview_size);
  1710. if (parameters->target().is_big_endian())
  1711. this->endian_do_write<true>(oview);
  1712. else
  1713. this->endian_do_write<false>(oview);
  1714. of->write_output_view(offset, oview_size, oview);
  1715. // We no longer need the data.
  1716. this->entries_.clear();
  1717. }
  1718. template<bool big_endian>
  1719. void
  1720. Output_symtab_xindex::endian_do_write(unsigned char* const oview)
  1721. {
  1722. for (Xindex_entries::const_iterator p = this->entries_.begin();
  1723. p != this->entries_.end();
  1724. ++p)
  1725. {
  1726. unsigned int symndx = p->first;
  1727. gold_assert(static_cast<off_t>(symndx) * 4 < this->data_size());
  1728. elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
  1729. }
  1730. }
  1731. // Output_fill_debug_info methods.
  1732. // Return the minimum size needed for a dummy compilation unit header.
  1733. size_t
  1734. Output_fill_debug_info::do_minimum_hole_size() const
  1735. {
  1736. // Compile unit header fields: unit_length, version, debug_abbrev_offset,
  1737. // address_size.
  1738. const size_t len = 4 + 2 + 4 + 1;
  1739. // For type units, add type_signature, type_offset.
  1740. if (this->is_debug_types_)
  1741. return len + 8 + 4;
  1742. return len;
  1743. }
  1744. // Write a dummy compilation unit header to fill a hole in the
  1745. // .debug_info or .debug_types section.
  1746. void
  1747. Output_fill_debug_info::do_write(Output_file* of, off_t off, size_t len) const
  1748. {
  1749. gold_debug(DEBUG_INCREMENTAL, "fill_debug_info(%08lx, %08lx)",
  1750. static_cast<long>(off), static_cast<long>(len));
  1751. gold_assert(len >= this->do_minimum_hole_size());
  1752. unsigned char* const oview = of->get_output_view(off, len);
  1753. unsigned char* pov = oview;
  1754. // Write header fields: unit_length, version, debug_abbrev_offset,
  1755. // address_size.
  1756. if (this->is_big_endian())
  1757. {
  1758. elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
  1759. elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
  1760. elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, 0);
  1761. }
  1762. else
  1763. {
  1764. elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
  1765. elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
  1766. elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, 0);
  1767. }
  1768. pov += 4 + 2 + 4;
  1769. *pov++ = 4;
  1770. // For type units, the additional header fields -- type_signature,
  1771. // type_offset -- can be filled with zeroes.
  1772. // Fill the remainder of the free space with zeroes. The first
  1773. // zero should tell the consumer there are no DIEs to read in this
  1774. // compilation unit.
  1775. if (pov < oview + len)
  1776. memset(pov, 0, oview + len - pov);
  1777. of->write_output_view(off, len, oview);
  1778. }
  1779. // Output_fill_debug_line methods.
  1780. // Return the minimum size needed for a dummy line number program header.
  1781. size_t
  1782. Output_fill_debug_line::do_minimum_hole_size() const
  1783. {
  1784. // Line number program header fields: unit_length, version, header_length,
  1785. // minimum_instruction_length, default_is_stmt, line_base, line_range,
  1786. // opcode_base, standard_opcode_lengths[], include_directories, filenames.
  1787. const size_t len = 4 + 2 + 4 + this->header_length;
  1788. return len;
  1789. }
  1790. // Write a dummy line number program header to fill a hole in the
  1791. // .debug_line section.
  1792. void
  1793. Output_fill_debug_line::do_write(Output_file* of, off_t off, size_t len) const
  1794. {
  1795. gold_debug(DEBUG_INCREMENTAL, "fill_debug_line(%08lx, %08lx)",
  1796. static_cast<long>(off), static_cast<long>(len));
  1797. gold_assert(len >= this->do_minimum_hole_size());
  1798. unsigned char* const oview = of->get_output_view(off, len);
  1799. unsigned char* pov = oview;
  1800. // Write header fields: unit_length, version, header_length,
  1801. // minimum_instruction_length, default_is_stmt, line_base, line_range,
  1802. // opcode_base, standard_opcode_lengths[], include_directories, filenames.
  1803. // We set the header_length field to cover the entire hole, so the
  1804. // line number program is empty.
  1805. if (this->is_big_endian())
  1806. {
  1807. elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
  1808. elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
  1809. elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, len - (4 + 2 + 4));
  1810. }
  1811. else
  1812. {
  1813. elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
  1814. elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
  1815. elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, len - (4 + 2 + 4));
  1816. }
  1817. pov += 4 + 2 + 4;
  1818. *pov++ = 1; // minimum_instruction_length
  1819. *pov++ = 0; // default_is_stmt
  1820. *pov++ = 0; // line_base
  1821. *pov++ = 5; // line_range
  1822. *pov++ = 13; // opcode_base
  1823. *pov++ = 0; // standard_opcode_lengths[1]
  1824. *pov++ = 1; // standard_opcode_lengths[2]
  1825. *pov++ = 1; // standard_opcode_lengths[3]
  1826. *pov++ = 1; // standard_opcode_lengths[4]
  1827. *pov++ = 1; // standard_opcode_lengths[5]
  1828. *pov++ = 0; // standard_opcode_lengths[6]
  1829. *pov++ = 0; // standard_opcode_lengths[7]
  1830. *pov++ = 0; // standard_opcode_lengths[8]
  1831. *pov++ = 1; // standard_opcode_lengths[9]
  1832. *pov++ = 0; // standard_opcode_lengths[10]
  1833. *pov++ = 0; // standard_opcode_lengths[11]
  1834. *pov++ = 1; // standard_opcode_lengths[12]
  1835. *pov++ = 0; // include_directories (empty)
  1836. *pov++ = 0; // filenames (empty)
  1837. // Some consumers don't check the header_length field, and simply
  1838. // start reading the line number program immediately following the
  1839. // header. For those consumers, we fill the remainder of the free
  1840. // space with DW_LNS_set_basic_block opcodes. These are effectively
  1841. // no-ops: the resulting line table program will not create any rows.
  1842. if (pov < oview + len)
  1843. memset(pov, elfcpp::DW_LNS_set_basic_block, oview + len - pov);
  1844. of->write_output_view(off, len, oview);
  1845. }
  1846. // Output_section::Input_section methods.
  1847. // Return the current data size. For an input section we store the size here.
  1848. // For an Output_section_data, we have to ask it for the size.
  1849. off_t
  1850. Output_section::Input_section::current_data_size() const
  1851. {
  1852. if (this->is_input_section())
  1853. return this->u1_.data_size;
  1854. else
  1855. {
  1856. this->u2_.posd->pre_finalize_data_size();
  1857. return this->u2_.posd->current_data_size();
  1858. }
  1859. }
  1860. // Return the data size. For an input section we store the size here.
  1861. // For an Output_section_data, we have to ask it for the size.
  1862. off_t
  1863. Output_section::Input_section::data_size() const
  1864. {
  1865. if (this->is_input_section())
  1866. return this->u1_.data_size;
  1867. else
  1868. return this->u2_.posd->data_size();
  1869. }
  1870. // Return the object for an input section.
  1871. Relobj*
  1872. Output_section::Input_section::relobj() const
  1873. {
  1874. if (this->is_input_section())
  1875. return this->u2_.object;
  1876. else if (this->is_merge_section())
  1877. {
  1878. gold_assert(this->u2_.pomb->first_relobj() != NULL);
  1879. return this->u2_.pomb->first_relobj();
  1880. }
  1881. else if (this->is_relaxed_input_section())
  1882. return this->u2_.poris->relobj();
  1883. else
  1884. gold_unreachable();
  1885. }
  1886. // Return the input section index for an input section.
  1887. unsigned int
  1888. Output_section::Input_section::shndx() const
  1889. {
  1890. if (this->is_input_section())
  1891. return this->shndx_;
  1892. else if (this->is_merge_section())
  1893. {
  1894. gold_assert(this->u2_.pomb->first_relobj() != NULL);
  1895. return this->u2_.pomb->first_shndx();
  1896. }
  1897. else if (this->is_relaxed_input_section())
  1898. return this->u2_.poris->shndx();
  1899. else
  1900. gold_unreachable();
  1901. }
  1902. // Set the address and file offset.
  1903. void
  1904. Output_section::Input_section::set_address_and_file_offset(
  1905. uint64_t address,
  1906. off_t file_offset,
  1907. off_t section_file_offset)
  1908. {
  1909. if (this->is_input_section())
  1910. this->u2_.object->set_section_offset(this->shndx_,
  1911. file_offset - section_file_offset);
  1912. else
  1913. this->u2_.posd->set_address_and_file_offset(address, file_offset);
  1914. }
  1915. // Reset the address and file offset.
  1916. void
  1917. Output_section::Input_section::reset_address_and_file_offset()
  1918. {
  1919. if (!this->is_input_section())
  1920. this->u2_.posd->reset_address_and_file_offset();
  1921. }
  1922. // Finalize the data size.
  1923. void
  1924. Output_section::Input_section::finalize_data_size()
  1925. {
  1926. if (!this->is_input_section())
  1927. this->u2_.posd->finalize_data_size();
  1928. }
  1929. // Try to turn an input offset into an output offset. We want to
  1930. // return the output offset relative to the start of this
  1931. // Input_section in the output section.
  1932. inline bool
  1933. Output_section::Input_section::output_offset(
  1934. const Relobj* object,
  1935. unsigned int shndx,
  1936. section_offset_type offset,
  1937. section_offset_type* poutput) const
  1938. {
  1939. if (!this->is_input_section())
  1940. return this->u2_.posd->output_offset(object, shndx, offset, poutput);
  1941. else
  1942. {
  1943. if (this->shndx_ != shndx || this->u2_.object != object)
  1944. return false;
  1945. *poutput = offset;
  1946. return true;
  1947. }
  1948. }
  1949. // Write out the data. We don't have to do anything for an input
  1950. // section--they are handled via Object::relocate--but this is where
  1951. // we write out the data for an Output_section_data.
  1952. void
  1953. Output_section::Input_section::write(Output_file* of)
  1954. {
  1955. if (!this->is_input_section())
  1956. this->u2_.posd->write(of);
  1957. }
  1958. // Write the data to a buffer. As for write(), we don't have to do
  1959. // anything for an input section.
  1960. void
  1961. Output_section::Input_section::write_to_buffer(unsigned char* buffer)
  1962. {
  1963. if (!this->is_input_section())
  1964. this->u2_.posd->write_to_buffer(buffer);
  1965. }
  1966. // Print to a map file.
  1967. void
  1968. Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
  1969. {
  1970. switch (this->shndx_)
  1971. {
  1972. case OUTPUT_SECTION_CODE:
  1973. case MERGE_DATA_SECTION_CODE:
  1974. case MERGE_STRING_SECTION_CODE:
  1975. this->u2_.posd->print_to_mapfile(mapfile);
  1976. break;
  1977. case RELAXED_INPUT_SECTION_CODE:
  1978. {
  1979. Output_relaxed_input_section* relaxed_section =
  1980. this->relaxed_input_section();
  1981. mapfile->print_input_section(relaxed_section->relobj(),
  1982. relaxed_section->shndx());
  1983. }
  1984. break;
  1985. default:
  1986. mapfile->print_input_section(this->u2_.object, this->shndx_);
  1987. break;
  1988. }
  1989. }
  1990. // Output_section methods.
  1991. // Construct an Output_section. NAME will point into a Stringpool.
  1992. Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
  1993. elfcpp::Elf_Xword flags)
  1994. : name_(name),
  1995. addralign_(0),
  1996. entsize_(0),
  1997. load_address_(0),
  1998. link_section_(NULL),
  1999. link_(0),
  2000. info_section_(NULL),
  2001. info_symndx_(NULL),
  2002. info_(0),
  2003. type_(type),
  2004. flags_(flags),
  2005. order_(ORDER_INVALID),
  2006. out_shndx_(-1U),
  2007. symtab_index_(0),
  2008. dynsym_index_(0),
  2009. input_sections_(),
  2010. first_input_offset_(0),
  2011. fills_(),
  2012. postprocessing_buffer_(NULL),
  2013. needs_symtab_index_(false),
  2014. needs_dynsym_index_(false),
  2015. should_link_to_symtab_(false),
  2016. should_link_to_dynsym_(false),
  2017. after_input_sections_(false),
  2018. requires_postprocessing_(false),
  2019. found_in_sections_clause_(false),
  2020. has_load_address_(false),
  2021. info_uses_section_index_(false),
  2022. input_section_order_specified_(false),
  2023. may_sort_attached_input_sections_(false),
  2024. must_sort_attached_input_sections_(false),
  2025. attached_input_sections_are_sorted_(false),
  2026. is_relro_(false),
  2027. is_small_section_(false),
  2028. is_large_section_(false),
  2029. generate_code_fills_at_write_(false),
  2030. is_entsize_zero_(false),
  2031. section_offsets_need_adjustment_(false),
  2032. is_noload_(false),
  2033. always_keeps_input_sections_(false),
  2034. has_fixed_layout_(false),
  2035. is_patch_space_allowed_(false),
  2036. is_unique_segment_(false),
  2037. tls_offset_(0),
  2038. extra_segment_flags_(0),
  2039. segment_alignment_(0),
  2040. checkpoint_(NULL),
  2041. lookup_maps_(new Output_section_lookup_maps),
  2042. free_list_(),
  2043. free_space_fill_(NULL),
  2044. patch_space_(0),
  2045. reloc_section_(NULL)
  2046. {
  2047. // An unallocated section has no address. Forcing this means that
  2048. // we don't need special treatment for symbols defined in debug
  2049. // sections.
  2050. if ((flags & elfcpp::SHF_ALLOC) == 0)
  2051. this->set_address(0);
  2052. }
  2053. Output_section::~Output_section()
  2054. {
  2055. delete this->checkpoint_;
  2056. }
  2057. // Set the entry size.
  2058. void
  2059. Output_section::set_entsize(uint64_t v)
  2060. {
  2061. if (this->is_entsize_zero_)
  2062. ;
  2063. else if (this->entsize_ == 0)
  2064. this->entsize_ = v;
  2065. else if (this->entsize_ != v)
  2066. {
  2067. this->entsize_ = 0;
  2068. this->is_entsize_zero_ = 1;
  2069. }
  2070. }
  2071. // Add the input section SHNDX, with header SHDR, named SECNAME, in
  2072. // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
  2073. // relocation section which applies to this section, or 0 if none, or
  2074. // -1U if more than one. Return the offset of the input section
  2075. // within the output section. Return -1 if the input section will
  2076. // receive special handling. In the normal case we don't always keep
  2077. // track of input sections for an Output_section. Instead, each
  2078. // Object keeps track of the Output_section for each of its input
  2079. // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
  2080. // track of input sections here; this is used when SECTIONS appears in
  2081. // a linker script.
  2082. template<int size, bool big_endian>
  2083. off_t
  2084. Output_section::add_input_section(Layout* layout,
  2085. Sized_relobj_file<size, big_endian>* object,
  2086. unsigned int shndx,
  2087. const char* secname,
  2088. const elfcpp::Shdr<size, big_endian>& shdr,
  2089. unsigned int reloc_shndx,
  2090. bool have_sections_script)
  2091. {
  2092. section_size_type input_section_size = shdr.get_sh_size();
  2093. section_size_type uncompressed_size;
  2094. elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
  2095. if (object->section_is_compressed(shndx, &uncompressed_size,
  2096. &addralign))
  2097. input_section_size = uncompressed_size;
  2098. if ((addralign & (addralign - 1)) != 0)
  2099. {
  2100. object->error(_("invalid alignment %lu for section \"%s\""),
  2101. static_cast<unsigned long>(addralign), secname);
  2102. addralign = 1;
  2103. }
  2104. if (addralign > this->addralign_)
  2105. this->addralign_ = addralign;
  2106. typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
  2107. uint64_t entsize = shdr.get_sh_entsize();
  2108. // .debug_str is a mergeable string section, but is not always so
  2109. // marked by compilers. Mark manually here so we can optimize.
  2110. if (strcmp(secname, ".debug_str") == 0)
  2111. {
  2112. sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
  2113. entsize = 1;
  2114. }
  2115. this->update_flags_for_input_section(sh_flags);
  2116. this->set_entsize(entsize);
  2117. // If this is a SHF_MERGE section, we pass all the input sections to
  2118. // a Output_data_merge. We don't try to handle relocations for such
  2119. // a section. We don't try to handle empty merge sections--they
  2120. // mess up the mappings, and are useless anyhow.
  2121. // FIXME: Need to handle merge sections during incremental update.
  2122. if ((sh_flags & elfcpp::SHF_MERGE) != 0
  2123. && reloc_shndx == 0
  2124. && shdr.get_sh_size() > 0
  2125. && !parameters->incremental())
  2126. {
  2127. // Keep information about merged input sections for rebuilding fast
  2128. // lookup maps if we have sections-script or we do relaxation.
  2129. bool keeps_input_sections = (this->always_keeps_input_sections_
  2130. || have_sections_script
  2131. || parameters->target().may_relax());
  2132. if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
  2133. addralign, keeps_input_sections))
  2134. {
  2135. // Tell the relocation routines that they need to call the
  2136. // output_offset method to determine the final address.
  2137. return -1;
  2138. }
  2139. }
  2140. off_t offset_in_section;
  2141. if (this->has_fixed_layout())
  2142. {
  2143. // For incremental updates, find a chunk of unused space in the section.
  2144. offset_in_section = this->free_list_.allocate(input_section_size,
  2145. addralign, 0);
  2146. if (offset_in_section == -1)
  2147. gold_fallback(_("out of patch space in section %s; "
  2148. "relink with --incremental-full"),
  2149. this->name());
  2150. return offset_in_section;
  2151. }
  2152. offset_in_section = this->current_data_size_for_child();
  2153. off_t aligned_offset_in_section = align_address(offset_in_section,
  2154. addralign);
  2155. this->set_current_data_size_for_child(aligned_offset_in_section
  2156. + input_section_size);
  2157. // Determine if we want to delay code-fill generation until the output
  2158. // section is written. When the target is relaxing, we want to delay fill
  2159. // generating to avoid adjusting them during relaxation. Also, if we are
  2160. // sorting input sections we must delay fill generation.
  2161. if (!this->generate_code_fills_at_write_
  2162. && !have_sections_script
  2163. && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
  2164. && parameters->target().has_code_fill()
  2165. && (parameters->target().may_relax()
  2166. || layout->is_section_ordering_specified()))
  2167. {
  2168. gold_assert(this->fills_.empty());
  2169. this->generate_code_fills_at_write_ = true;
  2170. }
  2171. if (aligned_offset_in_section > offset_in_section
  2172. && !this->generate_code_fills_at_write_
  2173. && !have_sections_script
  2174. && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
  2175. && parameters->target().has_code_fill())
  2176. {
  2177. // We need to add some fill data. Using fill_list_ when
  2178. // possible is an optimization, since we will often have fill
  2179. // sections without input sections.
  2180. off_t fill_len = aligned_offset_in_section - offset_in_section;
  2181. if (this->input_sections_.empty())
  2182. this->fills_.push_back(Fill(offset_in_section, fill_len));
  2183. else
  2184. {
  2185. std::string fill_data(parameters->target().code_fill(fill_len));
  2186. Output_data_const* odc = new Output_data_const(fill_data, 1);
  2187. this->input_sections_.push_back(Input_section(odc));
  2188. }
  2189. }
  2190. // We need to keep track of this section if we are already keeping
  2191. // track of sections, or if we are relaxing. Also, if this is a
  2192. // section which requires sorting, or which may require sorting in
  2193. // the future, we keep track of the sections. If the
  2194. // --section-ordering-file option is used to specify the order of
  2195. // sections, we need to keep track of sections.
  2196. if (this->always_keeps_input_sections_
  2197. || have_sections_script
  2198. || !this->input_sections_.empty()
  2199. || this->may_sort_attached_input_sections()
  2200. || this->must_sort_attached_input_sections()
  2201. || parameters->options().user_set_Map()
  2202. || parameters->target().may_relax()
  2203. || layout->is_section_ordering_specified())
  2204. {
  2205. Input_section isecn(object, shndx, input_section_size, addralign);
  2206. /* If section ordering is requested by specifying a ordering file,
  2207. using --section-ordering-file, match the section name with
  2208. a pattern. */
  2209. if (parameters->options().section_ordering_file())
  2210. {
  2211. unsigned int section_order_index =
  2212. layout->find_section_order_index(std::string(secname));
  2213. if (section_order_index != 0)
  2214. {
  2215. isecn.set_section_order_index(section_order_index);
  2216. this->set_input_section_order_specified();
  2217. }
  2218. }
  2219. this->input_sections_.push_back(isecn);
  2220. }
  2221. return aligned_offset_in_section;
  2222. }
  2223. // Add arbitrary data to an output section.
  2224. void
  2225. Output_section::add_output_section_data(Output_section_data* posd)
  2226. {
  2227. Input_section inp(posd);
  2228. this->add_output_section_data(&inp);
  2229. if (posd->is_data_size_valid())
  2230. {
  2231. off_t offset_in_section;
  2232. if (this->has_fixed_layout())
  2233. {
  2234. // For incremental updates, find a chunk of unused space.
  2235. offset_in_section = this->free_list_.allocate(posd->data_size(),
  2236. posd->addralign(), 0);
  2237. if (offset_in_section == -1)
  2238. gold_fallback(_("out of patch space in section %s; "
  2239. "relink with --incremental-full"),
  2240. this->name());
  2241. // Finalize the address and offset now.
  2242. uint64_t addr = this->address();
  2243. off_t offset = this->offset();
  2244. posd->set_address_and_file_offset(addr + offset_in_section,
  2245. offset + offset_in_section);
  2246. }
  2247. else
  2248. {
  2249. offset_in_section = this->current_data_size_for_child();
  2250. off_t aligned_offset_in_section = align_address(offset_in_section,
  2251. posd->addralign());
  2252. this->set_current_data_size_for_child(aligned_offset_in_section
  2253. + posd->data_size());
  2254. }
  2255. }
  2256. else if (this->has_fixed_layout())
  2257. {
  2258. // For incremental updates, arrange for the data to have a fixed layout.
  2259. // This will mean that additions to the data must be allocated from
  2260. // free space within the containing output section.
  2261. uint64_t addr = this->address();
  2262. posd->set_address(addr);
  2263. posd->set_file_offset(0);
  2264. // FIXME: This should eventually be unreachable.
  2265. // gold_unreachable();
  2266. }
  2267. }
  2268. // Add a relaxed input section.
  2269. void
  2270. Output_section::add_relaxed_input_section(Layout* layout,
  2271. Output_relaxed_input_section* poris,
  2272. const std::string& name)
  2273. {
  2274. Input_section inp(poris);
  2275. // If the --section-ordering-file option is used to specify the order of
  2276. // sections, we need to keep track of sections.
  2277. if (layout->is_section_ordering_specified())
  2278. {
  2279. unsigned int section_order_index =
  2280. layout->find_section_order_index(name);
  2281. if (section_order_index != 0)
  2282. {
  2283. inp.set_section_order_index(section_order_index);
  2284. this->set_input_section_order_specified();
  2285. }
  2286. }
  2287. this->add_output_section_data(&inp);
  2288. if (this->lookup_maps_->is_valid())
  2289. this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
  2290. poris->shndx(), poris);
  2291. // For a relaxed section, we use the current data size. Linker scripts
  2292. // get all the input sections, including relaxed one from an output
  2293. // section and add them back to the same output section to compute the
  2294. // output section size. If we do not account for sizes of relaxed input
  2295. // sections, an output section would be incorrectly sized.
  2296. off_t offset_in_section = this->current_data_size_for_child();
  2297. off_t aligned_offset_in_section = align_address(offset_in_section,
  2298. poris->addralign());
  2299. this->set_current_data_size_for_child(aligned_offset_in_section
  2300. + poris->current_data_size());
  2301. }
  2302. // Add arbitrary data to an output section by Input_section.
  2303. void
  2304. Output_section::add_output_section_data(Input_section* inp)
  2305. {
  2306. if (this->input_sections_.empty())
  2307. this->first_input_offset_ = this->current_data_size_for_child();
  2308. this->input_sections_.push_back(*inp);
  2309. uint64_t addralign = inp->addralign();
  2310. if (addralign > this->addralign_)
  2311. this->addralign_ = addralign;
  2312. inp->set_output_section(this);
  2313. }
  2314. // Add a merge section to an output section.
  2315. void
  2316. Output_section::add_output_merge_section(Output_section_data* posd,
  2317. bool is_string, uint64_t entsize)
  2318. {
  2319. Input_section inp(posd, is_string, entsize);
  2320. this->add_output_section_data(&inp);
  2321. }
  2322. // Add an input section to a SHF_MERGE section.
  2323. bool
  2324. Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
  2325. uint64_t flags, uint64_t entsize,
  2326. uint64_t addralign,
  2327. bool keeps_input_sections)
  2328. {
  2329. // We cannot merge sections with entsize == 0.
  2330. if (entsize == 0)
  2331. return false;
  2332. bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
  2333. // We cannot restore merged input section states.
  2334. gold_assert(this->checkpoint_ == NULL);
  2335. // Look up merge sections by required properties.
  2336. // Currently, we only invalidate the lookup maps in script processing
  2337. // and relaxation. We should not have done either when we reach here.
  2338. // So we assume that the lookup maps are valid to simply code.
  2339. gold_assert(this->lookup_maps_->is_valid());
  2340. Merge_section_properties msp(is_string, entsize, addralign);
  2341. Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
  2342. bool is_new = false;
  2343. if (pomb != NULL)
  2344. {
  2345. gold_assert(pomb->is_string() == is_string
  2346. && pomb->entsize() == entsize
  2347. && pomb->addralign() == addralign);
  2348. }
  2349. else
  2350. {
  2351. // Create a new Output_merge_data or Output_merge_string_data.
  2352. if (!is_string)
  2353. pomb = new Output_merge_data(entsize, addralign);
  2354. else
  2355. {
  2356. switch (entsize)
  2357. {
  2358. case 1:
  2359. pomb = new Output_merge_string<char>(addralign);
  2360. break;
  2361. case 2:
  2362. pomb = new Output_merge_string<uint16_t>(addralign);
  2363. break;
  2364. case 4:
  2365. pomb = new Output_merge_string<uint32_t>(addralign);
  2366. break;
  2367. default:
  2368. return false;
  2369. }
  2370. }
  2371. // If we need to do script processing or relaxation, we need to keep
  2372. // the original input sections to rebuild the fast lookup maps.
  2373. if (keeps_input_sections)
  2374. pomb->set_keeps_input_sections();
  2375. is_new = true;
  2376. }
  2377. if (pomb->add_input_section(object, shndx))
  2378. {
  2379. // Add new merge section to this output section and link merge
  2380. // section properties to new merge section in map.
  2381. if (is_new)
  2382. {
  2383. this->add_output_merge_section(pomb, is_string, entsize);
  2384. this->lookup_maps_->add_merge_section(msp, pomb);
  2385. }
  2386. return true;
  2387. }
  2388. else
  2389. {
  2390. // If add_input_section failed, delete new merge section to avoid
  2391. // exporting empty merge sections in Output_section::get_input_section.
  2392. if (is_new)
  2393. delete pomb;
  2394. return false;
  2395. }
  2396. }
  2397. // Build a relaxation map to speed up relaxation of existing input sections.
  2398. // Look up to the first LIMIT elements in INPUT_SECTIONS.
  2399. void
  2400. Output_section::build_relaxation_map(
  2401. const Input_section_list& input_sections,
  2402. size_t limit,
  2403. Relaxation_map* relaxation_map) const
  2404. {
  2405. for (size_t i = 0; i < limit; ++i)
  2406. {
  2407. const Input_section& is(input_sections[i]);
  2408. if (is.is_input_section() || is.is_relaxed_input_section())
  2409. {
  2410. Section_id sid(is.relobj(), is.shndx());
  2411. (*relaxation_map)[sid] = i;
  2412. }
  2413. }
  2414. }
  2415. // Convert regular input sections in INPUT_SECTIONS into relaxed input
  2416. // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
  2417. // indices of INPUT_SECTIONS.
  2418. void
  2419. Output_section::convert_input_sections_in_list_to_relaxed_sections(
  2420. const std::vector<Output_relaxed_input_section*>& relaxed_sections,
  2421. const Relaxation_map& map,
  2422. Input_section_list* input_sections)
  2423. {
  2424. for (size_t i = 0; i < relaxed_sections.size(); ++i)
  2425. {
  2426. Output_relaxed_input_section* poris = relaxed_sections[i];
  2427. Section_id sid(poris->relobj(), poris->shndx());
  2428. Relaxation_map::const_iterator p = map.find(sid);
  2429. gold_assert(p != map.end());
  2430. gold_assert((*input_sections)[p->second].is_input_section());
  2431. // Remember section order index of original input section
  2432. // if it is set. Copy it to the relaxed input section.
  2433. unsigned int soi =
  2434. (*input_sections)[p->second].section_order_index();
  2435. (*input_sections)[p->second] = Input_section(poris);
  2436. (*input_sections)[p->second].set_section_order_index(soi);
  2437. }
  2438. }
  2439. // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
  2440. // is a vector of pointers to Output_relaxed_input_section or its derived
  2441. // classes. The relaxed sections must correspond to existing input sections.
  2442. void
  2443. Output_section::convert_input_sections_to_relaxed_sections(
  2444. const std::vector<Output_relaxed_input_section*>& relaxed_sections)
  2445. {
  2446. gold_assert(parameters->target().may_relax());
  2447. // We want to make sure that restore_states does not undo the effect of
  2448. // this. If there is no checkpoint active, just search the current
  2449. // input section list and replace the sections there. If there is
  2450. // a checkpoint, also replace the sections there.
  2451. // By default, we look at the whole list.
  2452. size_t limit = this->input_sections_.size();
  2453. if (this->checkpoint_ != NULL)
  2454. {
  2455. // Replace input sections with relaxed input section in the saved
  2456. // copy of the input section list.
  2457. if (this->checkpoint_->input_sections_saved())
  2458. {
  2459. Relaxation_map map;
  2460. this->build_relaxation_map(
  2461. *(this->checkpoint_->input_sections()),
  2462. this->checkpoint_->input_sections()->size(),
  2463. &map);
  2464. this->convert_input_sections_in_list_to_relaxed_sections(
  2465. relaxed_sections,
  2466. map,
  2467. this->checkpoint_->input_sections());
  2468. }
  2469. else
  2470. {
  2471. // We have not copied the input section list yet. Instead, just
  2472. // look at the portion that would be saved.
  2473. limit = this->checkpoint_->input_sections_size();
  2474. }
  2475. }
  2476. // Convert input sections in input_section_list.
  2477. Relaxation_map map;
  2478. this->build_relaxation_map(this->input_sections_, limit, &map);
  2479. this->convert_input_sections_in_list_to_relaxed_sections(
  2480. relaxed_sections,
  2481. map,
  2482. &this->input_sections_);
  2483. // Update fast look-up map.
  2484. if (this->lookup_maps_->is_valid())
  2485. for (size_t i = 0; i < relaxed_sections.size(); ++i)
  2486. {
  2487. Output_relaxed_input_section* poris = relaxed_sections[i];
  2488. this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
  2489. poris->shndx(), poris);
  2490. }
  2491. }
  2492. // Update the output section flags based on input section flags.
  2493. void
  2494. Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
  2495. {
  2496. // If we created the section with SHF_ALLOC clear, we set the
  2497. // address. If we are now setting the SHF_ALLOC flag, we need to
  2498. // undo that.
  2499. if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
  2500. && (flags & elfcpp::SHF_ALLOC) != 0)
  2501. this->mark_address_invalid();
  2502. this->flags_ |= (flags
  2503. & (elfcpp::SHF_WRITE
  2504. | elfcpp::SHF_ALLOC
  2505. | elfcpp::SHF_EXECINSTR));
  2506. if ((flags & elfcpp::SHF_MERGE) == 0)
  2507. this->flags_ &=~ elfcpp::SHF_MERGE;
  2508. else
  2509. {
  2510. if (this->current_data_size_for_child() == 0)
  2511. this->flags_ |= elfcpp::SHF_MERGE;
  2512. }
  2513. if ((flags & elfcpp::SHF_STRINGS) == 0)
  2514. this->flags_ &=~ elfcpp::SHF_STRINGS;
  2515. else
  2516. {
  2517. if (this->current_data_size_for_child() == 0)
  2518. this->flags_ |= elfcpp::SHF_STRINGS;
  2519. }
  2520. }
  2521. // Find the merge section into which an input section with index SHNDX in
  2522. // OBJECT has been added. Return NULL if none found.
  2523. const Output_section_data*
  2524. Output_section::find_merge_section(const Relobj* object,
  2525. unsigned int shndx) const
  2526. {
  2527. return object->find_merge_section(shndx);
  2528. }
  2529. // Build the lookup maps for relaxed sections. This needs
  2530. // to be declared as a const method so that it is callable with a const
  2531. // Output_section pointer. The method only updates states of the maps.
  2532. void
  2533. Output_section::build_lookup_maps() const
  2534. {
  2535. this->lookup_maps_->clear();
  2536. for (Input_section_list::const_iterator p = this->input_sections_.begin();
  2537. p != this->input_sections_.end();
  2538. ++p)
  2539. {
  2540. if (p->is_relaxed_input_section())
  2541. {
  2542. Output_relaxed_input_section* poris = p->relaxed_input_section();
  2543. this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
  2544. poris->shndx(), poris);
  2545. }
  2546. }
  2547. }
  2548. // Find an relaxed input section corresponding to an input section
  2549. // in OBJECT with index SHNDX.
  2550. const Output_relaxed_input_section*
  2551. Output_section::find_relaxed_input_section(const Relobj* object,
  2552. unsigned int shndx) const
  2553. {
  2554. if (!this->lookup_maps_->is_valid())
  2555. this->build_lookup_maps();
  2556. return this->lookup_maps_->find_relaxed_input_section(object, shndx);
  2557. }
  2558. // Given an address OFFSET relative to the start of input section
  2559. // SHNDX in OBJECT, return whether this address is being included in
  2560. // the final link. This should only be called if SHNDX in OBJECT has
  2561. // a special mapping.
  2562. bool
  2563. Output_section::is_input_address_mapped(const Relobj* object,
  2564. unsigned int shndx,
  2565. off_t offset) const
  2566. {
  2567. // Look at the Output_section_data_maps first.
  2568. const Output_section_data* posd = this->find_merge_section(object, shndx);
  2569. if (posd == NULL)
  2570. posd = this->find_relaxed_input_section(object, shndx);
  2571. if (posd != NULL)
  2572. {
  2573. section_offset_type output_offset;
  2574. bool found = posd->output_offset(object, shndx, offset, &output_offset);
  2575. // By default we assume that the address is mapped. See comment at the
  2576. // end.
  2577. if (!found)
  2578. return true;
  2579. return output_offset != -1;
  2580. }
  2581. // Fall back to the slow look-up.
  2582. for (Input_section_list::const_iterator p = this->input_sections_.begin();
  2583. p != this->input_sections_.end();
  2584. ++p)
  2585. {
  2586. section_offset_type output_offset;
  2587. if (p->output_offset(object, shndx, offset, &output_offset))
  2588. return output_offset != -1;
  2589. }
  2590. // By default we assume that the address is mapped. This should
  2591. // only be called after we have passed all sections to Layout. At
  2592. // that point we should know what we are discarding.
  2593. return true;
  2594. }
  2595. // Given an address OFFSET relative to the start of input section
  2596. // SHNDX in object OBJECT, return the output offset relative to the
  2597. // start of the input section in the output section. This should only
  2598. // be called if SHNDX in OBJECT has a special mapping.
  2599. section_offset_type
  2600. Output_section::output_offset(const Relobj* object, unsigned int shndx,
  2601. section_offset_type offset) const
  2602. {
  2603. // This can only be called meaningfully when we know the data size
  2604. // of this.
  2605. gold_assert(this->is_data_size_valid());
  2606. // Look at the Output_section_data_maps first.
  2607. const Output_section_data* posd = this->find_merge_section(object, shndx);
  2608. if (posd == NULL)
  2609. posd = this->find_relaxed_input_section(object, shndx);
  2610. if (posd != NULL)
  2611. {
  2612. section_offset_type output_offset;
  2613. bool found = posd->output_offset(object, shndx, offset, &output_offset);
  2614. gold_assert(found);
  2615. return output_offset;
  2616. }
  2617. // Fall back to the slow look-up.
  2618. for (Input_section_list::const_iterator p = this->input_sections_.begin();
  2619. p != this->input_sections_.end();
  2620. ++p)
  2621. {
  2622. section_offset_type output_offset;
  2623. if (p->output_offset(object, shndx, offset, &output_offset))
  2624. return output_offset;
  2625. }
  2626. gold_unreachable();
  2627. }
  2628. // Return the output virtual address of OFFSET relative to the start
  2629. // of input section SHNDX in object OBJECT.
  2630. uint64_t
  2631. Output_section::output_address(const Relobj* object, unsigned int shndx,
  2632. off_t offset) const
  2633. {
  2634. uint64_t addr = this->address() + this->first_input_offset_;
  2635. // Look at the Output_section_data_maps first.
  2636. const Output_section_data* posd = this->find_merge_section(object, shndx);
  2637. if (posd == NULL)
  2638. posd = this->find_relaxed_input_section(object, shndx);
  2639. if (posd != NULL && posd->is_address_valid())
  2640. {
  2641. section_offset_type output_offset;
  2642. bool found = posd->output_offset(object, shndx, offset, &output_offset);
  2643. gold_assert(found);
  2644. return posd->address() + output_offset;
  2645. }
  2646. // Fall back to the slow look-up.
  2647. for (Input_section_list::const_iterator p = this->input_sections_.begin();
  2648. p != this->input_sections_.end();
  2649. ++p)
  2650. {
  2651. addr = align_address(addr, p->addralign());
  2652. section_offset_type output_offset;
  2653. if (p->output_offset(object, shndx, offset, &output_offset))
  2654. {
  2655. if (output_offset == -1)
  2656. return -1ULL;
  2657. return addr + output_offset;
  2658. }
  2659. addr += p->data_size();
  2660. }
  2661. // If we get here, it means that we don't know the mapping for this
  2662. // input section. This might happen in principle if
  2663. // add_input_section were called before add_output_section_data.
  2664. // But it should never actually happen.
  2665. gold_unreachable();
  2666. }
  2667. // Find the output address of the start of the merged section for
  2668. // input section SHNDX in object OBJECT.
  2669. bool
  2670. Output_section::find_starting_output_address(const Relobj* object,
  2671. unsigned int shndx,
  2672. uint64_t* paddr) const
  2673. {
  2674. const Output_section_data* data = this->find_merge_section(object, shndx);
  2675. if (data == NULL)
  2676. return false;
  2677. // FIXME: This becomes a bottle-neck if we have many relaxed sections.
  2678. // Looking up the merge section map does not always work as we sometimes
  2679. // find a merge section without its address set.
  2680. uint64_t addr = this->address() + this->first_input_offset_;
  2681. for (Input_section_list::const_iterator p = this->input_sections_.begin();
  2682. p != this->input_sections_.end();
  2683. ++p)
  2684. {
  2685. addr = align_address(addr, p->addralign());
  2686. // It would be nice if we could use the existing output_offset
  2687. // method to get the output offset of input offset 0.
  2688. // Unfortunately we don't know for sure that input offset 0 is
  2689. // mapped at all.
  2690. if (!p->is_input_section() && p->output_section_data() == data)
  2691. {
  2692. *paddr = addr;
  2693. return true;
  2694. }
  2695. addr += p->data_size();
  2696. }
  2697. // We couldn't find a merge output section for this input section.
  2698. return false;
  2699. }
  2700. // Update the data size of an Output_section.
  2701. void
  2702. Output_section::update_data_size()
  2703. {
  2704. if (this->input_sections_.empty())
  2705. return;
  2706. if (this->must_sort_attached_input_sections()
  2707. || this->input_section_order_specified())
  2708. this->sort_attached_input_sections();
  2709. off_t off = this->first_input_offset_;
  2710. for (Input_section_list::iterator p = this->input_sections_.begin();
  2711. p != this->input_sections_.end();
  2712. ++p)
  2713. {
  2714. off = align_address(off, p->addralign());
  2715. off += p->current_data_size();
  2716. }
  2717. this->set_current_data_size_for_child(off);
  2718. }
  2719. // Set the data size of an Output_section. This is where we handle
  2720. // setting the addresses of any Output_section_data objects.
  2721. void
  2722. Output_section::set_final_data_size()
  2723. {
  2724. off_t data_size;
  2725. if (this->input_sections_.empty())
  2726. data_size = this->current_data_size_for_child();
  2727. else
  2728. {
  2729. if (this->must_sort_attached_input_sections()
  2730. || this->input_section_order_specified())
  2731. this->sort_attached_input_sections();
  2732. uint64_t address = this->address();
  2733. off_t startoff = this->offset();
  2734. off_t off = this->first_input_offset_;
  2735. for (Input_section_list::iterator p = this->input_sections_.begin();
  2736. p != this->input_sections_.end();
  2737. ++p)
  2738. {
  2739. off = align_address(off, p->addralign());
  2740. p->set_address_and_file_offset(address + off, startoff + off,
  2741. startoff);
  2742. off += p->data_size();
  2743. }
  2744. data_size = off;
  2745. }
  2746. // For full incremental links, we want to allocate some patch space
  2747. // in most sections for subsequent incremental updates.
  2748. if (this->is_patch_space_allowed_ && parameters->incremental_full())
  2749. {
  2750. double pct = parameters->options().incremental_patch();
  2751. size_t extra = static_cast<size_t>(data_size * pct);
  2752. if (this->free_space_fill_ != NULL
  2753. && this->free_space_fill_->minimum_hole_size() > extra)
  2754. extra = this->free_space_fill_->minimum_hole_size();
  2755. off_t new_size = align_address(data_size + extra, this->addralign());
  2756. this->patch_space_ = new_size - data_size;
  2757. gold_debug(DEBUG_INCREMENTAL,
  2758. "set_final_data_size: %08lx + %08lx: section %s",
  2759. static_cast<long>(data_size),
  2760. static_cast<long>(this->patch_space_),
  2761. this->name());
  2762. data_size = new_size;
  2763. }
  2764. this->set_data_size(data_size);
  2765. }
  2766. // Reset the address and file offset.
  2767. void
  2768. Output_section::do_reset_address_and_file_offset()
  2769. {
  2770. // An unallocated section has no address. Forcing this means that
  2771. // we don't need special treatment for symbols defined in debug
  2772. // sections. We do the same in the constructor. This does not
  2773. // apply to NOLOAD sections though.
  2774. if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
  2775. this->set_address(0);
  2776. for (Input_section_list::iterator p = this->input_sections_.begin();
  2777. p != this->input_sections_.end();
  2778. ++p)
  2779. p->reset_address_and_file_offset();
  2780. // Remove any patch space that was added in set_final_data_size.
  2781. if (this->patch_space_ > 0)
  2782. {
  2783. this->set_current_data_size_for_child(this->current_data_size_for_child()
  2784. - this->patch_space_);
  2785. this->patch_space_ = 0;
  2786. }
  2787. }
  2788. // Return true if address and file offset have the values after reset.
  2789. bool
  2790. Output_section::do_address_and_file_offset_have_reset_values() const
  2791. {
  2792. if (this->is_offset_valid())
  2793. return false;
  2794. // An unallocated section has address 0 after its construction or a reset.
  2795. if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
  2796. return this->is_address_valid() && this->address() == 0;
  2797. else
  2798. return !this->is_address_valid();
  2799. }
  2800. // Set the TLS offset. Called only for SHT_TLS sections.
  2801. void
  2802. Output_section::do_set_tls_offset(uint64_t tls_base)
  2803. {
  2804. this->tls_offset_ = this->address() - tls_base;
  2805. }
  2806. // In a few cases we need to sort the input sections attached to an
  2807. // output section. This is used to implement the type of constructor
  2808. // priority ordering implemented by the GNU linker, in which the
  2809. // priority becomes part of the section name and the sections are
  2810. // sorted by name. We only do this for an output section if we see an
  2811. // attached input section matching ".ctors.*", ".dtors.*",
  2812. // ".init_array.*" or ".fini_array.*".
  2813. class Output_section::Input_section_sort_entry
  2814. {
  2815. public:
  2816. Input_section_sort_entry()
  2817. : input_section_(), index_(-1U), section_name_()
  2818. { }
  2819. Input_section_sort_entry(const Input_section& input_section,
  2820. unsigned int index,
  2821. bool must_sort_attached_input_sections,
  2822. const char* output_section_name)
  2823. : input_section_(input_section), index_(index), section_name_()
  2824. {
  2825. if ((input_section.is_input_section()
  2826. || input_section.is_relaxed_input_section())
  2827. && must_sort_attached_input_sections)
  2828. {
  2829. // This is only called single-threaded from Layout::finalize,
  2830. // so it is OK to lock. Unfortunately we have no way to pass
  2831. // in a Task token.
  2832. const Task* dummy_task = reinterpret_cast<const Task*>(-1);
  2833. Object* obj = (input_section.is_input_section()
  2834. ? input_section.relobj()
  2835. : input_section.relaxed_input_section()->relobj());
  2836. Task_lock_obj<Object> tl(dummy_task, obj);
  2837. // This is a slow operation, which should be cached in
  2838. // Layout::layout if this becomes a speed problem.
  2839. this->section_name_ = obj->section_name(input_section.shndx());
  2840. }
  2841. else if (input_section.is_output_section_data()
  2842. && must_sort_attached_input_sections)
  2843. {
  2844. // For linker-generated sections, use the output section name.
  2845. this->section_name_.assign(output_section_name);
  2846. }
  2847. }
  2848. // Return the Input_section.
  2849. const Input_section&
  2850. input_section() const
  2851. {
  2852. gold_assert(this->index_ != -1U);
  2853. return this->input_section_;
  2854. }
  2855. // The index of this entry in the original list. This is used to
  2856. // make the sort stable.
  2857. unsigned int
  2858. index() const
  2859. {
  2860. gold_assert(this->index_ != -1U);
  2861. return this->index_;
  2862. }
  2863. // The section name.
  2864. const std::string&
  2865. section_name() const
  2866. {
  2867. return this->section_name_;
  2868. }
  2869. // Return true if the section name has a priority. This is assumed
  2870. // to be true if it has a dot after the initial dot.
  2871. bool
  2872. has_priority() const
  2873. {
  2874. return this->section_name_.find('.', 1) != std::string::npos;
  2875. }
  2876. // Return the priority. Believe it or not, gcc encodes the priority
  2877. // differently for .ctors/.dtors and .init_array/.fini_array
  2878. // sections.
  2879. unsigned int
  2880. get_priority() const
  2881. {
  2882. bool is_ctors;
  2883. if (is_prefix_of(".ctors.", this->section_name_.c_str())
  2884. || is_prefix_of(".dtors.", this->section_name_.c_str()))
  2885. is_ctors = true;
  2886. else if (is_prefix_of(".init_array.", this->section_name_.c_str())
  2887. || is_prefix_of(".fini_array.", this->section_name_.c_str()))
  2888. is_ctors = false;
  2889. else
  2890. return 0;
  2891. char* end;
  2892. unsigned long prio = strtoul((this->section_name_.c_str()
  2893. + (is_ctors ? 7 : 12)),
  2894. &end, 10);
  2895. if (*end != '\0')
  2896. return 0;
  2897. else if (is_ctors)
  2898. return 65535 - prio;
  2899. else
  2900. return prio;
  2901. }
  2902. // Return true if this an input file whose base name matches
  2903. // FILE_NAME. The base name must have an extension of ".o", and
  2904. // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
  2905. // This is to match crtbegin.o as well as crtbeginS.o without
  2906. // getting confused by other possibilities. Overall matching the
  2907. // file name this way is a dreadful hack, but the GNU linker does it
  2908. // in order to better support gcc, and we need to be compatible.
  2909. bool
  2910. match_file_name(const char* file_name) const
  2911. {
  2912. if (this->input_section_.is_output_section_data())
  2913. return false;
  2914. return Layout::match_file_name(this->input_section_.relobj(), file_name);
  2915. }
  2916. // Returns 1 if THIS should appear before S in section order, -1 if S
  2917. // appears before THIS and 0 if they are not comparable.
  2918. int
  2919. compare_section_ordering(const Input_section_sort_entry& s) const
  2920. {
  2921. unsigned int this_secn_index = this->input_section_.section_order_index();
  2922. unsigned int s_secn_index = s.input_section().section_order_index();
  2923. if (this_secn_index > 0 && s_secn_index > 0)
  2924. {
  2925. if (this_secn_index < s_secn_index)
  2926. return 1;
  2927. else if (this_secn_index > s_secn_index)
  2928. return -1;
  2929. }
  2930. return 0;
  2931. }
  2932. private:
  2933. // The Input_section we are sorting.
  2934. Input_section input_section_;
  2935. // The index of this Input_section in the original list.
  2936. unsigned int index_;
  2937. // The section name if there is one.
  2938. std::string section_name_;
  2939. };
  2940. // Return true if S1 should come before S2 in the output section.
  2941. bool
  2942. Output_section::Input_section_sort_compare::operator()(
  2943. const Output_section::Input_section_sort_entry& s1,
  2944. const Output_section::Input_section_sort_entry& s2) const
  2945. {
  2946. // crtbegin.o must come first.
  2947. bool s1_begin = s1.match_file_name("crtbegin");
  2948. bool s2_begin = s2.match_file_name("crtbegin");
  2949. if (s1_begin || s2_begin)
  2950. {
  2951. if (!s1_begin)
  2952. return false;
  2953. if (!s2_begin)
  2954. return true;
  2955. return s1.index() < s2.index();
  2956. }
  2957. // crtend.o must come last.
  2958. bool s1_end = s1.match_file_name("crtend");
  2959. bool s2_end = s2.match_file_name("crtend");
  2960. if (s1_end || s2_end)
  2961. {
  2962. if (!s1_end)
  2963. return true;
  2964. if (!s2_end)
  2965. return false;
  2966. return s1.index() < s2.index();
  2967. }
  2968. // A section with a priority follows a section without a priority.
  2969. bool s1_has_priority = s1.has_priority();
  2970. bool s2_has_priority = s2.has_priority();
  2971. if (s1_has_priority && !s2_has_priority)
  2972. return false;
  2973. if (!s1_has_priority && s2_has_priority)
  2974. return true;
  2975. // Check if a section order exists for these sections through a section
  2976. // ordering file. If sequence_num is 0, an order does not exist.
  2977. int sequence_num = s1.compare_section_ordering(s2);
  2978. if (sequence_num != 0)
  2979. return sequence_num == 1;
  2980. // Otherwise we sort by name.
  2981. int compare = s1.section_name().compare(s2.section_name());
  2982. if (compare != 0)
  2983. return compare < 0;
  2984. // Otherwise we keep the input order.
  2985. return s1.index() < s2.index();
  2986. }
  2987. // Return true if S1 should come before S2 in an .init_array or .fini_array
  2988. // output section.
  2989. bool
  2990. Output_section::Input_section_sort_init_fini_compare::operator()(
  2991. const Output_section::Input_section_sort_entry& s1,
  2992. const Output_section::Input_section_sort_entry& s2) const
  2993. {
  2994. // A section without a priority follows a section with a priority.
  2995. // This is the reverse of .ctors and .dtors sections.
  2996. bool s1_has_priority = s1.has_priority();
  2997. bool s2_has_priority = s2.has_priority();
  2998. if (s1_has_priority && !s2_has_priority)
  2999. return true;
  3000. if (!s1_has_priority && s2_has_priority)
  3001. return false;
  3002. // .ctors and .dtors sections without priority come after
  3003. // .init_array and .fini_array sections without priority.
  3004. if (!s1_has_priority
  3005. && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors")
  3006. && s1.section_name() != s2.section_name())
  3007. return false;
  3008. if (!s2_has_priority
  3009. && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors")
  3010. && s2.section_name() != s1.section_name())
  3011. return true;
  3012. // Sort by priority if we can.
  3013. if (s1_has_priority)
  3014. {
  3015. unsigned int s1_prio = s1.get_priority();
  3016. unsigned int s2_prio = s2.get_priority();
  3017. if (s1_prio < s2_prio)
  3018. return true;
  3019. else if (s1_prio > s2_prio)
  3020. return false;
  3021. }
  3022. // Check if a section order exists for these sections through a section
  3023. // ordering file. If sequence_num is 0, an order does not exist.
  3024. int sequence_num = s1.compare_section_ordering(s2);
  3025. if (sequence_num != 0)
  3026. return sequence_num == 1;
  3027. // Otherwise we sort by name.
  3028. int compare = s1.section_name().compare(s2.section_name());
  3029. if (compare != 0)
  3030. return compare < 0;
  3031. // Otherwise we keep the input order.
  3032. return s1.index() < s2.index();
  3033. }
  3034. // Return true if S1 should come before S2. Sections that do not match
  3035. // any pattern in the section ordering file are placed ahead of the sections
  3036. // that match some pattern.
  3037. bool
  3038. Output_section::Input_section_sort_section_order_index_compare::operator()(
  3039. const Output_section::Input_section_sort_entry& s1,
  3040. const Output_section::Input_section_sort_entry& s2) const
  3041. {
  3042. unsigned int s1_secn_index = s1.input_section().section_order_index();
  3043. unsigned int s2_secn_index = s2.input_section().section_order_index();
  3044. // Keep input order if section ordering cannot determine order.
  3045. if (s1_secn_index == s2_secn_index)
  3046. return s1.index() < s2.index();
  3047. return s1_secn_index < s2_secn_index;
  3048. }
  3049. // Return true if S1 should come before S2. This is the sort comparison
  3050. // function for .text to sort sections with prefixes
  3051. // .text.{unlikely,exit,startup,hot} before other sections.
  3052. bool
  3053. Output_section::Input_section_sort_section_prefix_special_ordering_compare
  3054. ::operator()(
  3055. const Output_section::Input_section_sort_entry& s1,
  3056. const Output_section::Input_section_sort_entry& s2) const
  3057. {
  3058. // Some input section names have special ordering requirements.
  3059. const char *s1_section_name = s1.section_name().c_str();
  3060. const char *s2_section_name = s2.section_name().c_str();
  3061. int o1 = Layout::special_ordering_of_input_section(s1_section_name);
  3062. int o2 = Layout::special_ordering_of_input_section(s2_section_name);
  3063. if (o1 != o2)
  3064. {
  3065. if (o1 < 0)
  3066. return false;
  3067. else if (o2 < 0)
  3068. return true;
  3069. else
  3070. return o1 < o2;
  3071. }
  3072. else if (is_prefix_of(".text.sorted", s1_section_name))
  3073. return strcmp(s1_section_name, s2_section_name) <= 0;
  3074. // Keep input order otherwise.
  3075. return s1.index() < s2.index();
  3076. }
  3077. // Return true if S1 should come before S2. This is the sort comparison
  3078. // function for sections to sort them by name.
  3079. bool
  3080. Output_section::Input_section_sort_section_name_compare
  3081. ::operator()(
  3082. const Output_section::Input_section_sort_entry& s1,
  3083. const Output_section::Input_section_sort_entry& s2) const
  3084. {
  3085. // We sort by name.
  3086. int compare = s1.section_name().compare(s2.section_name());
  3087. if (compare != 0)
  3088. return compare < 0;
  3089. // Keep input order otherwise.
  3090. return s1.index() < s2.index();
  3091. }
  3092. // This updates the section order index of input sections according to the
  3093. // the order specified in the mapping from Section id to order index.
  3094. void
  3095. Output_section::update_section_layout(
  3096. const Section_layout_order* order_map)
  3097. {
  3098. for (Input_section_list::iterator p = this->input_sections_.begin();
  3099. p != this->input_sections_.end();
  3100. ++p)
  3101. {
  3102. if (p->is_input_section()
  3103. || p->is_relaxed_input_section())
  3104. {
  3105. Relobj* obj = (p->is_input_section()
  3106. ? p->relobj()
  3107. : p->relaxed_input_section()->relobj());
  3108. unsigned int shndx = p->shndx();
  3109. Section_layout_order::const_iterator it
  3110. = order_map->find(Section_id(obj, shndx));
  3111. if (it == order_map->end())
  3112. continue;
  3113. unsigned int section_order_index = it->second;
  3114. if (section_order_index != 0)
  3115. {
  3116. p->set_section_order_index(section_order_index);
  3117. this->set_input_section_order_specified();
  3118. }
  3119. }
  3120. }
  3121. }
  3122. // Sort the input sections attached to an output section.
  3123. void
  3124. Output_section::sort_attached_input_sections()
  3125. {
  3126. if (this->attached_input_sections_are_sorted_)
  3127. return;
  3128. if (this->checkpoint_ != NULL
  3129. && !this->checkpoint_->input_sections_saved())
  3130. this->checkpoint_->save_input_sections();
  3131. // The only thing we know about an input section is the object and
  3132. // the section index. We need the section name. Recomputing this
  3133. // is slow but this is an unusual case. If this becomes a speed
  3134. // problem we can cache the names as required in Layout::layout.
  3135. // We start by building a larger vector holding a copy of each
  3136. // Input_section, plus its current index in the list and its name.
  3137. std::vector<Input_section_sort_entry> sort_list;
  3138. unsigned int i = 0;
  3139. for (Input_section_list::iterator p = this->input_sections_.begin();
  3140. p != this->input_sections_.end();
  3141. ++p, ++i)
  3142. sort_list.push_back(Input_section_sort_entry(*p, i,
  3143. this->must_sort_attached_input_sections(),
  3144. this->name()));
  3145. // Sort the input sections.
  3146. if (this->must_sort_attached_input_sections())
  3147. {
  3148. if (this->type() == elfcpp::SHT_PREINIT_ARRAY
  3149. || this->type() == elfcpp::SHT_INIT_ARRAY
  3150. || this->type() == elfcpp::SHT_FINI_ARRAY)
  3151. std::sort(sort_list.begin(), sort_list.end(),
  3152. Input_section_sort_init_fini_compare());
  3153. else if (strcmp(parameters->options().sort_section(), "name") == 0)
  3154. std::sort(sort_list.begin(), sort_list.end(),
  3155. Input_section_sort_section_name_compare());
  3156. else if (strcmp(this->name(), ".text") == 0)
  3157. std::sort(sort_list.begin(), sort_list.end(),
  3158. Input_section_sort_section_prefix_special_ordering_compare());
  3159. else
  3160. std::sort(sort_list.begin(), sort_list.end(),
  3161. Input_section_sort_compare());
  3162. }
  3163. else
  3164. {
  3165. gold_assert(this->input_section_order_specified());
  3166. std::sort(sort_list.begin(), sort_list.end(),
  3167. Input_section_sort_section_order_index_compare());
  3168. }
  3169. // Copy the sorted input sections back to our list.
  3170. this->input_sections_.clear();
  3171. for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
  3172. p != sort_list.end();
  3173. ++p)
  3174. this->input_sections_.push_back(p->input_section());
  3175. sort_list.clear();
  3176. // Remember that we sorted the input sections, since we might get
  3177. // called again.
  3178. this->attached_input_sections_are_sorted_ = true;
  3179. }
  3180. // Write the section header to *OSHDR.
  3181. template<int size, bool big_endian>
  3182. void
  3183. Output_section::write_header(const Layout* layout,
  3184. const Stringpool* secnamepool,
  3185. elfcpp::Shdr_write<size, big_endian>* oshdr) const
  3186. {
  3187. oshdr->put_sh_name(secnamepool->get_offset(this->name_));
  3188. oshdr->put_sh_type(this->type_);
  3189. elfcpp::Elf_Xword flags = this->flags_;
  3190. if (this->info_section_ != NULL && this->info_uses_section_index_)
  3191. flags |= elfcpp::SHF_INFO_LINK;
  3192. oshdr->put_sh_flags(flags);
  3193. oshdr->put_sh_addr(this->address());
  3194. oshdr->put_sh_offset(this->offset());
  3195. oshdr->put_sh_size(this->data_size());
  3196. if (this->link_section_ != NULL)
  3197. oshdr->put_sh_link(this->link_section_->out_shndx());
  3198. else if (this->should_link_to_symtab_)
  3199. oshdr->put_sh_link(layout->symtab_section_shndx());
  3200. else if (this->should_link_to_dynsym_)
  3201. oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
  3202. else
  3203. oshdr->put_sh_link(this->link_);
  3204. elfcpp::Elf_Word info;
  3205. if (this->info_section_ != NULL)
  3206. {
  3207. if (this->info_uses_section_index_)
  3208. info = this->info_section_->out_shndx();
  3209. else
  3210. info = this->info_section_->symtab_index();
  3211. }
  3212. else if (this->info_symndx_ != NULL)
  3213. info = this->info_symndx_->symtab_index();
  3214. else
  3215. info = this->info_;
  3216. oshdr->put_sh_info(info);
  3217. oshdr->put_sh_addralign(this->addralign_);
  3218. oshdr->put_sh_entsize(this->entsize_);
  3219. }
  3220. // Write out the data. For input sections the data is written out by
  3221. // Object::relocate, but we have to handle Output_section_data objects
  3222. // here.
  3223. void
  3224. Output_section::do_write(Output_file* of)
  3225. {
  3226. gold_assert(!this->requires_postprocessing());
  3227. // If the target performs relaxation, we delay filler generation until now.
  3228. gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
  3229. off_t output_section_file_offset = this->offset();
  3230. for (Fill_list::iterator p = this->fills_.begin();
  3231. p != this->fills_.end();
  3232. ++p)
  3233. {
  3234. std::string fill_data(parameters->target().code_fill(p->length()));
  3235. of->write(output_section_file_offset + p->section_offset(),
  3236. fill_data.data(), fill_data.size());
  3237. }
  3238. off_t off = this->offset() + this->first_input_offset_;
  3239. for (Input_section_list::iterator p = this->input_sections_.begin();
  3240. p != this->input_sections_.end();
  3241. ++p)
  3242. {
  3243. off_t aligned_off = align_address(off, p->addralign());
  3244. if (this->generate_code_fills_at_write_ && (off != aligned_off))
  3245. {
  3246. size_t fill_len = aligned_off - off;
  3247. std::string fill_data(parameters->target().code_fill(fill_len));
  3248. of->write(off, fill_data.data(), fill_data.size());
  3249. }
  3250. p->write(of);
  3251. off = aligned_off + p->data_size();
  3252. }
  3253. // For incremental links, fill in unused chunks in debug sections
  3254. // with dummy compilation unit headers.
  3255. if (this->free_space_fill_ != NULL)
  3256. {
  3257. for (Free_list::Const_iterator p = this->free_list_.begin();
  3258. p != this->free_list_.end();
  3259. ++p)
  3260. {
  3261. off_t off = p->start_;
  3262. size_t len = p->end_ - off;
  3263. this->free_space_fill_->write(of, this->offset() + off, len);
  3264. }
  3265. if (this->patch_space_ > 0)
  3266. {
  3267. off_t off = this->current_data_size_for_child() - this->patch_space_;
  3268. this->free_space_fill_->write(of, this->offset() + off,
  3269. this->patch_space_);
  3270. }
  3271. }
  3272. }
  3273. // If a section requires postprocessing, create the buffer to use.
  3274. void
  3275. Output_section::create_postprocessing_buffer()
  3276. {
  3277. gold_assert(this->requires_postprocessing());
  3278. if (this->postprocessing_buffer_ != NULL)
  3279. return;
  3280. if (!this->input_sections_.empty())
  3281. {
  3282. off_t off = this->first_input_offset_;
  3283. for (Input_section_list::iterator p = this->input_sections_.begin();
  3284. p != this->input_sections_.end();
  3285. ++p)
  3286. {
  3287. off = align_address(off, p->addralign());
  3288. p->finalize_data_size();
  3289. off += p->data_size();
  3290. }
  3291. this->set_current_data_size_for_child(off);
  3292. }
  3293. off_t buffer_size = this->current_data_size_for_child();
  3294. this->postprocessing_buffer_ = new unsigned char[buffer_size];
  3295. }
  3296. // Write all the data of an Output_section into the postprocessing
  3297. // buffer. This is used for sections which require postprocessing,
  3298. // such as compression. Input sections are handled by
  3299. // Object::Relocate.
  3300. void
  3301. Output_section::write_to_postprocessing_buffer()
  3302. {
  3303. gold_assert(this->requires_postprocessing());
  3304. // If the target performs relaxation, we delay filler generation until now.
  3305. gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
  3306. unsigned char* buffer = this->postprocessing_buffer();
  3307. for (Fill_list::iterator p = this->fills_.begin();
  3308. p != this->fills_.end();
  3309. ++p)
  3310. {
  3311. std::string fill_data(parameters->target().code_fill(p->length()));
  3312. memcpy(buffer + p->section_offset(), fill_data.data(),
  3313. fill_data.size());
  3314. }
  3315. off_t off = this->first_input_offset_;
  3316. for (Input_section_list::iterator p = this->input_sections_.begin();
  3317. p != this->input_sections_.end();
  3318. ++p)
  3319. {
  3320. off_t aligned_off = align_address(off, p->addralign());
  3321. if (this->generate_code_fills_at_write_ && (off != aligned_off))
  3322. {
  3323. size_t fill_len = aligned_off - off;
  3324. std::string fill_data(parameters->target().code_fill(fill_len));
  3325. memcpy(buffer + off, fill_data.data(), fill_data.size());
  3326. }
  3327. p->write_to_buffer(buffer + aligned_off);
  3328. off = aligned_off + p->data_size();
  3329. }
  3330. }
  3331. // Get the input sections for linker script processing. We leave
  3332. // behind the Output_section_data entries. Note that this may be
  3333. // slightly incorrect for merge sections. We will leave them behind,
  3334. // but it is possible that the script says that they should follow
  3335. // some other input sections, as in:
  3336. // .rodata { *(.rodata) *(.rodata.cst*) }
  3337. // For that matter, we don't handle this correctly:
  3338. // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
  3339. // With luck this will never matter.
  3340. uint64_t
  3341. Output_section::get_input_sections(
  3342. uint64_t address,
  3343. const std::string& fill,
  3344. std::list<Input_section>* input_sections)
  3345. {
  3346. if (this->checkpoint_ != NULL
  3347. && !this->checkpoint_->input_sections_saved())
  3348. this->checkpoint_->save_input_sections();
  3349. // Invalidate fast look-up maps.
  3350. this->lookup_maps_->invalidate();
  3351. uint64_t orig_address = address;
  3352. address = align_address(address, this->addralign());
  3353. Input_section_list remaining;
  3354. for (Input_section_list::iterator p = this->input_sections_.begin();
  3355. p != this->input_sections_.end();
  3356. ++p)
  3357. {
  3358. if (p->is_input_section()
  3359. || p->is_relaxed_input_section()
  3360. || p->is_merge_section())
  3361. input_sections->push_back(*p);
  3362. else
  3363. {
  3364. uint64_t aligned_address = align_address(address, p->addralign());
  3365. if (aligned_address != address && !fill.empty())
  3366. {
  3367. section_size_type length =
  3368. convert_to_section_size_type(aligned_address - address);
  3369. std::string this_fill;
  3370. this_fill.reserve(length);
  3371. while (this_fill.length() + fill.length() <= length)
  3372. this_fill += fill;
  3373. if (this_fill.length() < length)
  3374. this_fill.append(fill, 0, length - this_fill.length());
  3375. Output_section_data* posd = new Output_data_const(this_fill, 0);
  3376. remaining.push_back(Input_section(posd));
  3377. }
  3378. address = aligned_address;
  3379. remaining.push_back(*p);
  3380. p->finalize_data_size();
  3381. address += p->data_size();
  3382. }
  3383. }
  3384. this->input_sections_.swap(remaining);
  3385. this->first_input_offset_ = 0;
  3386. uint64_t data_size = address - orig_address;
  3387. this->set_current_data_size_for_child(data_size);
  3388. return data_size;
  3389. }
  3390. // Add a script input section. SIS is an Output_section::Input_section,
  3391. // which can be either a plain input section or a special input section like
  3392. // a relaxed input section. For a special input section, its size must be
  3393. // finalized.
  3394. void
  3395. Output_section::add_script_input_section(const Input_section& sis)
  3396. {
  3397. uint64_t data_size = sis.data_size();
  3398. uint64_t addralign = sis.addralign();
  3399. if (addralign > this->addralign_)
  3400. this->addralign_ = addralign;
  3401. off_t offset_in_section = this->current_data_size_for_child();
  3402. off_t aligned_offset_in_section = align_address(offset_in_section,
  3403. addralign);
  3404. this->set_current_data_size_for_child(aligned_offset_in_section
  3405. + data_size);
  3406. this->input_sections_.push_back(sis);
  3407. // Update fast lookup maps if necessary.
  3408. if (this->lookup_maps_->is_valid())
  3409. {
  3410. if (sis.is_relaxed_input_section())
  3411. {
  3412. Output_relaxed_input_section* poris = sis.relaxed_input_section();
  3413. this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
  3414. poris->shndx(), poris);
  3415. }
  3416. }
  3417. }
  3418. // Save states for relaxation.
  3419. void
  3420. Output_section::save_states()
  3421. {
  3422. gold_assert(this->checkpoint_ == NULL);
  3423. Checkpoint_output_section* checkpoint =
  3424. new Checkpoint_output_section(this->addralign_, this->flags_,
  3425. this->input_sections_,
  3426. this->first_input_offset_,
  3427. this->attached_input_sections_are_sorted_);
  3428. this->checkpoint_ = checkpoint;
  3429. gold_assert(this->fills_.empty());
  3430. }
  3431. void
  3432. Output_section::discard_states()
  3433. {
  3434. gold_assert(this->checkpoint_ != NULL);
  3435. delete this->checkpoint_;
  3436. this->checkpoint_ = NULL;
  3437. gold_assert(this->fills_.empty());
  3438. // Simply invalidate the fast lookup maps since we do not keep
  3439. // track of them.
  3440. this->lookup_maps_->invalidate();
  3441. }
  3442. void
  3443. Output_section::restore_states()
  3444. {
  3445. gold_assert(this->checkpoint_ != NULL);
  3446. Checkpoint_output_section* checkpoint = this->checkpoint_;
  3447. this->addralign_ = checkpoint->addralign();
  3448. this->flags_ = checkpoint->flags();
  3449. this->first_input_offset_ = checkpoint->first_input_offset();
  3450. if (!checkpoint->input_sections_saved())
  3451. {
  3452. // If we have not copied the input sections, just resize it.
  3453. size_t old_size = checkpoint->input_sections_size();
  3454. gold_assert(this->input_sections_.size() >= old_size);
  3455. this->input_sections_.resize(old_size);
  3456. }
  3457. else
  3458. {
  3459. // We need to copy the whole list. This is not efficient for
  3460. // extremely large output with hundreads of thousands of input
  3461. // objects. We may need to re-think how we should pass sections
  3462. // to scripts.
  3463. this->input_sections_ = *checkpoint->input_sections();
  3464. }
  3465. this->attached_input_sections_are_sorted_ =
  3466. checkpoint->attached_input_sections_are_sorted();
  3467. // Simply invalidate the fast lookup maps since we do not keep
  3468. // track of them.
  3469. this->lookup_maps_->invalidate();
  3470. }
  3471. // Update the section offsets of input sections in this. This is required if
  3472. // relaxation causes some input sections to change sizes.
  3473. void
  3474. Output_section::adjust_section_offsets()
  3475. {
  3476. if (!this->section_offsets_need_adjustment_)
  3477. return;
  3478. off_t off = 0;
  3479. for (Input_section_list::iterator p = this->input_sections_.begin();
  3480. p != this->input_sections_.end();
  3481. ++p)
  3482. {
  3483. off = align_address(off, p->addralign());
  3484. if (p->is_input_section())
  3485. p->relobj()->set_section_offset(p->shndx(), off);
  3486. off += p->data_size();
  3487. }
  3488. this->section_offsets_need_adjustment_ = false;
  3489. }
  3490. // Print to the map file.
  3491. void
  3492. Output_section::do_print_to_mapfile(Mapfile* mapfile) const
  3493. {
  3494. mapfile->print_output_section(this);
  3495. for (Input_section_list::const_iterator p = this->input_sections_.begin();
  3496. p != this->input_sections_.end();
  3497. ++p)
  3498. p->print_to_mapfile(mapfile);
  3499. }
  3500. // Print stats for merge sections to stderr.
  3501. void
  3502. Output_section::print_merge_stats()
  3503. {
  3504. Input_section_list::iterator p;
  3505. for (p = this->input_sections_.begin();
  3506. p != this->input_sections_.end();
  3507. ++p)
  3508. p->print_merge_stats(this->name_);
  3509. }
  3510. // Set a fixed layout for the section. Used for incremental update links.
  3511. void
  3512. Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
  3513. off_t sh_size, uint64_t sh_addralign)
  3514. {
  3515. this->addralign_ = sh_addralign;
  3516. this->set_current_data_size(sh_size);
  3517. if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
  3518. this->set_address(sh_addr);
  3519. this->set_file_offset(sh_offset);
  3520. this->finalize_data_size();
  3521. this->free_list_.init(sh_size, false);
  3522. this->has_fixed_layout_ = true;
  3523. }
  3524. // Reserve space within the fixed layout for the section. Used for
  3525. // incremental update links.
  3526. void
  3527. Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
  3528. {
  3529. this->free_list_.remove(sh_offset, sh_offset + sh_size);
  3530. }
  3531. // Allocate space from the free list for the section. Used for
  3532. // incremental update links.
  3533. off_t
  3534. Output_section::allocate(off_t len, uint64_t addralign)
  3535. {
  3536. return this->free_list_.allocate(len, addralign, 0);
  3537. }
  3538. // Output segment methods.
  3539. Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
  3540. : vaddr_(0),
  3541. paddr_(0),
  3542. memsz_(0),
  3543. align_(0),
  3544. max_align_(0),
  3545. min_p_align_(0),
  3546. offset_(0),
  3547. filesz_(0),
  3548. type_(type),
  3549. flags_(flags),
  3550. is_max_align_known_(false),
  3551. are_addresses_set_(false),
  3552. is_large_data_segment_(false),
  3553. is_unique_segment_(false)
  3554. {
  3555. // The ELF ABI specifies that a PT_TLS segment always has PF_R as
  3556. // the flags.
  3557. if (type == elfcpp::PT_TLS)
  3558. this->flags_ = elfcpp::PF_R;
  3559. }
  3560. // Add an Output_section to a PT_LOAD Output_segment.
  3561. void
  3562. Output_segment::add_output_section_to_load(Layout* layout,
  3563. Output_section* os,
  3564. elfcpp::Elf_Word seg_flags)
  3565. {
  3566. gold_assert(this->type() == elfcpp::PT_LOAD);
  3567. gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
  3568. gold_assert(!this->is_max_align_known_);
  3569. gold_assert(os->is_large_data_section() == this->is_large_data_segment());
  3570. this->update_flags_for_output_section(seg_flags);
  3571. // We don't want to change the ordering if we have a linker script
  3572. // with a SECTIONS clause.
  3573. Output_section_order order = os->order();
  3574. if (layout->script_options()->saw_sections_clause())
  3575. order = static_cast<Output_section_order>(0);
  3576. else
  3577. gold_assert(order != ORDER_INVALID);
  3578. this->output_lists_[order].push_back(os);
  3579. }
  3580. // Add an Output_section to a non-PT_LOAD Output_segment.
  3581. void
  3582. Output_segment::add_output_section_to_nonload(Output_section* os,
  3583. elfcpp::Elf_Word seg_flags)
  3584. {
  3585. gold_assert(this->type() != elfcpp::PT_LOAD);
  3586. gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
  3587. gold_assert(!this->is_max_align_known_);
  3588. this->update_flags_for_output_section(seg_flags);
  3589. this->output_lists_[0].push_back(os);
  3590. }
  3591. // Remove an Output_section from this segment. It is an error if it
  3592. // is not present.
  3593. void
  3594. Output_segment::remove_output_section(Output_section* os)
  3595. {
  3596. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  3597. {
  3598. Output_data_list* pdl = &this->output_lists_[i];
  3599. for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
  3600. {
  3601. if (*p == os)
  3602. {
  3603. pdl->erase(p);
  3604. return;
  3605. }
  3606. }
  3607. }
  3608. gold_unreachable();
  3609. }
  3610. // Add an Output_data (which need not be an Output_section) to the
  3611. // start of a segment.
  3612. void
  3613. Output_segment::add_initial_output_data(Output_data* od)
  3614. {
  3615. gold_assert(!this->is_max_align_known_);
  3616. Output_data_list::iterator p = this->output_lists_[0].begin();
  3617. this->output_lists_[0].insert(p, od);
  3618. }
  3619. // Return true if this segment has any sections which hold actual
  3620. // data, rather than being a BSS section.
  3621. bool
  3622. Output_segment::has_any_data_sections() const
  3623. {
  3624. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  3625. {
  3626. const Output_data_list* pdl = &this->output_lists_[i];
  3627. for (Output_data_list::const_iterator p = pdl->begin();
  3628. p != pdl->end();
  3629. ++p)
  3630. {
  3631. if (!(*p)->is_section())
  3632. return true;
  3633. if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
  3634. return true;
  3635. }
  3636. }
  3637. return false;
  3638. }
  3639. // Return whether the first data section (not counting TLS sections)
  3640. // is a relro section.
  3641. bool
  3642. Output_segment::is_first_section_relro() const
  3643. {
  3644. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  3645. {
  3646. if (i == static_cast<int>(ORDER_TLS_BSS))
  3647. continue;
  3648. const Output_data_list* pdl = &this->output_lists_[i];
  3649. if (!pdl->empty())
  3650. {
  3651. Output_data* p = pdl->front();
  3652. return p->is_section() && p->output_section()->is_relro();
  3653. }
  3654. }
  3655. return false;
  3656. }
  3657. // Return the maximum alignment of the Output_data in Output_segment.
  3658. uint64_t
  3659. Output_segment::maximum_alignment()
  3660. {
  3661. if (!this->is_max_align_known_)
  3662. {
  3663. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  3664. {
  3665. const Output_data_list* pdl = &this->output_lists_[i];
  3666. uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
  3667. if (addralign > this->max_align_)
  3668. this->max_align_ = addralign;
  3669. }
  3670. this->is_max_align_known_ = true;
  3671. }
  3672. return this->max_align_;
  3673. }
  3674. // Return the maximum alignment of a list of Output_data.
  3675. uint64_t
  3676. Output_segment::maximum_alignment_list(const Output_data_list* pdl)
  3677. {
  3678. uint64_t ret = 0;
  3679. for (Output_data_list::const_iterator p = pdl->begin();
  3680. p != pdl->end();
  3681. ++p)
  3682. {
  3683. uint64_t addralign = (*p)->addralign();
  3684. if (addralign > ret)
  3685. ret = addralign;
  3686. }
  3687. return ret;
  3688. }
  3689. // Return whether this segment has any dynamic relocs.
  3690. bool
  3691. Output_segment::has_dynamic_reloc() const
  3692. {
  3693. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  3694. if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
  3695. return true;
  3696. return false;
  3697. }
  3698. // Return whether this Output_data_list has any dynamic relocs.
  3699. bool
  3700. Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
  3701. {
  3702. for (Output_data_list::const_iterator p = pdl->begin();
  3703. p != pdl->end();
  3704. ++p)
  3705. if ((*p)->has_dynamic_reloc())
  3706. return true;
  3707. return false;
  3708. }
  3709. // Set the section addresses for an Output_segment. If RESET is true,
  3710. // reset the addresses first. ADDR is the address and *POFF is the
  3711. // file offset. Set the section indexes starting with *PSHNDX.
  3712. // INCREASE_RELRO is the size of the portion of the first non-relro
  3713. // section that should be included in the PT_GNU_RELRO segment.
  3714. // If this segment has relro sections, and has been aligned for
  3715. // that purpose, set *HAS_RELRO to TRUE. Return the address of
  3716. // the immediately following segment. Update *HAS_RELRO, *POFF,
  3717. // and *PSHNDX.
  3718. uint64_t
  3719. Output_segment::set_section_addresses(const Target* target,
  3720. Layout* layout, bool reset,
  3721. uint64_t addr,
  3722. unsigned int* increase_relro,
  3723. bool* has_relro,
  3724. off_t* poff,
  3725. unsigned int* pshndx)
  3726. {
  3727. gold_assert(this->type_ == elfcpp::PT_LOAD);
  3728. uint64_t last_relro_pad = 0;
  3729. off_t orig_off = *poff;
  3730. bool in_tls = false;
  3731. // If we have relro sections, we need to pad forward now so that the
  3732. // relro sections plus INCREASE_RELRO end on an abi page boundary.
  3733. if (parameters->options().relro()
  3734. && this->is_first_section_relro()
  3735. && (!this->are_addresses_set_ || reset))
  3736. {
  3737. uint64_t relro_size = 0;
  3738. off_t off = *poff;
  3739. uint64_t max_align = 0;
  3740. for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
  3741. {
  3742. Output_data_list* pdl = &this->output_lists_[i];
  3743. Output_data_list::iterator p;
  3744. for (p = pdl->begin(); p != pdl->end(); ++p)
  3745. {
  3746. if (!(*p)->is_section())
  3747. break;
  3748. uint64_t align = (*p)->addralign();
  3749. if (align > max_align)
  3750. max_align = align;
  3751. if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
  3752. in_tls = true;
  3753. else if (in_tls)
  3754. {
  3755. // Align the first non-TLS section to the alignment
  3756. // of the TLS segment.
  3757. align = max_align;
  3758. in_tls = false;
  3759. }
  3760. // Ignore the size of the .tbss section.
  3761. if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
  3762. && (*p)->is_section_type(elfcpp::SHT_NOBITS))
  3763. continue;
  3764. relro_size = align_address(relro_size, align);
  3765. if ((*p)->is_address_valid())
  3766. relro_size += (*p)->data_size();
  3767. else
  3768. {
  3769. // FIXME: This could be faster.
  3770. (*p)->set_address_and_file_offset(relro_size,
  3771. relro_size);
  3772. relro_size += (*p)->data_size();
  3773. (*p)->reset_address_and_file_offset();
  3774. }
  3775. }
  3776. if (p != pdl->end())
  3777. break;
  3778. }
  3779. relro_size += *increase_relro;
  3780. // Pad the total relro size to a multiple of the maximum
  3781. // section alignment seen.
  3782. uint64_t aligned_size = align_address(relro_size, max_align);
  3783. // Note the amount of padding added after the last relro section.
  3784. last_relro_pad = aligned_size - relro_size;
  3785. *has_relro = true;
  3786. uint64_t page_align = parameters->target().abi_pagesize();
  3787. // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
  3788. uint64_t desired_align = page_align - (aligned_size % page_align);
  3789. if (desired_align < off % page_align)
  3790. off += page_align;
  3791. off += desired_align - off % page_align;
  3792. addr += off - orig_off;
  3793. orig_off = off;
  3794. *poff = off;
  3795. }
  3796. if (!reset && this->are_addresses_set_)
  3797. {
  3798. gold_assert(this->paddr_ == addr);
  3799. addr = this->vaddr_;
  3800. }
  3801. else
  3802. {
  3803. this->vaddr_ = addr;
  3804. this->paddr_ = addr;
  3805. this->are_addresses_set_ = true;
  3806. }
  3807. in_tls = false;
  3808. this->offset_ = orig_off;
  3809. off_t off = 0;
  3810. off_t foff = *poff;
  3811. uint64_t ret = 0;
  3812. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  3813. {
  3814. if (i == static_cast<int>(ORDER_RELRO_LAST))
  3815. {
  3816. *poff += last_relro_pad;
  3817. foff += last_relro_pad;
  3818. addr += last_relro_pad;
  3819. if (this->output_lists_[i].empty())
  3820. {
  3821. // If there is nothing in the ORDER_RELRO_LAST list,
  3822. // the padding will occur at the end of the relro
  3823. // segment, and we need to add it to *INCREASE_RELRO.
  3824. *increase_relro += last_relro_pad;
  3825. }
  3826. }
  3827. addr = this->set_section_list_addresses(layout, reset,
  3828. &this->output_lists_[i],
  3829. addr, poff, &foff, pshndx,
  3830. &in_tls);
  3831. // FOFF tracks the last offset used for the file image,
  3832. // and *POFF tracks the last offset used for the memory image.
  3833. // When not using a linker script, bss sections should all
  3834. // be processed in the ORDER_SMALL_BSS and later buckets.
  3835. gold_assert(*poff == foff
  3836. || i == static_cast<int>(ORDER_TLS_BSS)
  3837. || i >= static_cast<int>(ORDER_SMALL_BSS)
  3838. || layout->script_options()->saw_sections_clause());
  3839. this->filesz_ = foff - orig_off;
  3840. off = foff;
  3841. ret = addr;
  3842. }
  3843. // If the last section was a TLS section, align upward to the
  3844. // alignment of the TLS segment, so that the overall size of the TLS
  3845. // segment is aligned.
  3846. if (in_tls)
  3847. {
  3848. uint64_t segment_align = layout->tls_segment()->maximum_alignment();
  3849. *poff = align_address(*poff, segment_align);
  3850. }
  3851. this->memsz_ = *poff - orig_off;
  3852. // Ignore the file offset adjustments made by the BSS Output_data
  3853. // objects.
  3854. *poff = off;
  3855. // If code segments must contain only code, and this code segment is
  3856. // page-aligned in the file, then fill it out to a whole page with
  3857. // code fill (the tail of the segment will not be within any section).
  3858. // Thus the entire code segment can be mapped from the file as whole
  3859. // pages and that mapping will contain only valid instructions.
  3860. if (target->isolate_execinstr() && (this->flags() & elfcpp::PF_X) != 0)
  3861. {
  3862. uint64_t abi_pagesize = target->abi_pagesize();
  3863. if (orig_off % abi_pagesize == 0 && off % abi_pagesize != 0)
  3864. {
  3865. size_t fill_size = abi_pagesize - (off % abi_pagesize);
  3866. std::string fill_data;
  3867. if (target->has_code_fill())
  3868. fill_data = target->code_fill(fill_size);
  3869. else
  3870. fill_data.resize(fill_size); // Zero fill.
  3871. Output_data_const* fill = new Output_data_const(fill_data, 0);
  3872. fill->set_address(this->vaddr_ + this->memsz_);
  3873. fill->set_file_offset(off);
  3874. layout->add_relax_output(fill);
  3875. off += fill_size;
  3876. gold_assert(off % abi_pagesize == 0);
  3877. ret += fill_size;
  3878. gold_assert(ret % abi_pagesize == 0);
  3879. gold_assert((uint64_t) this->filesz_ == this->memsz_);
  3880. this->memsz_ = this->filesz_ += fill_size;
  3881. *poff = off;
  3882. }
  3883. }
  3884. return ret;
  3885. }
  3886. // Set the addresses and file offsets in a list of Output_data
  3887. // structures.
  3888. uint64_t
  3889. Output_segment::set_section_list_addresses(Layout* layout, bool reset,
  3890. Output_data_list* pdl,
  3891. uint64_t addr, off_t* poff,
  3892. off_t* pfoff,
  3893. unsigned int* pshndx,
  3894. bool* in_tls)
  3895. {
  3896. off_t startoff = *poff;
  3897. // For incremental updates, we may allocate non-fixed sections from
  3898. // free space in the file. This keeps track of the high-water mark.
  3899. off_t maxoff = startoff;
  3900. off_t off = startoff;
  3901. off_t foff = *pfoff;
  3902. for (Output_data_list::iterator p = pdl->begin();
  3903. p != pdl->end();
  3904. ++p)
  3905. {
  3906. bool is_bss = (*p)->is_section_type(elfcpp::SHT_NOBITS);
  3907. bool is_tls = (*p)->is_section_flag_set(elfcpp::SHF_TLS);
  3908. if (reset)
  3909. (*p)->reset_address_and_file_offset();
  3910. // When doing an incremental update or when using a linker script,
  3911. // the section will most likely already have an address.
  3912. if (!(*p)->is_address_valid())
  3913. {
  3914. uint64_t align = (*p)->addralign();
  3915. if (is_tls)
  3916. {
  3917. // Give the first TLS section the alignment of the
  3918. // entire TLS segment. Otherwise the TLS segment as a
  3919. // whole may be misaligned.
  3920. if (!*in_tls)
  3921. {
  3922. Output_segment* tls_segment = layout->tls_segment();
  3923. gold_assert(tls_segment != NULL);
  3924. uint64_t segment_align = tls_segment->maximum_alignment();
  3925. gold_assert(segment_align >= align);
  3926. align = segment_align;
  3927. *in_tls = true;
  3928. }
  3929. }
  3930. else
  3931. {
  3932. // If this is the first section after the TLS segment,
  3933. // align it to at least the alignment of the TLS
  3934. // segment, so that the size of the overall TLS segment
  3935. // is aligned.
  3936. if (*in_tls)
  3937. {
  3938. uint64_t segment_align =
  3939. layout->tls_segment()->maximum_alignment();
  3940. if (segment_align > align)
  3941. align = segment_align;
  3942. *in_tls = false;
  3943. }
  3944. }
  3945. if (!parameters->incremental_update())
  3946. {
  3947. gold_assert(off == foff || is_bss);
  3948. off = align_address(off, align);
  3949. if (is_tls || !is_bss)
  3950. foff = off;
  3951. (*p)->set_address_and_file_offset(addr + (off - startoff), foff);
  3952. }
  3953. else
  3954. {
  3955. // Incremental update: allocate file space from free list.
  3956. (*p)->pre_finalize_data_size();
  3957. off_t current_size = (*p)->current_data_size();
  3958. off = layout->allocate(current_size, align, startoff);
  3959. foff = off;
  3960. if (off == -1)
  3961. {
  3962. gold_assert((*p)->output_section() != NULL);
  3963. gold_fallback(_("out of patch space for section %s; "
  3964. "relink with --incremental-full"),
  3965. (*p)->output_section()->name());
  3966. }
  3967. (*p)->set_address_and_file_offset(addr + (off - startoff), foff);
  3968. if ((*p)->data_size() > current_size)
  3969. {
  3970. gold_assert((*p)->output_section() != NULL);
  3971. gold_fallback(_("%s: section changed size; "
  3972. "relink with --incremental-full"),
  3973. (*p)->output_section()->name());
  3974. }
  3975. }
  3976. }
  3977. else if (parameters->incremental_update())
  3978. {
  3979. // For incremental updates, use the fixed offset for the
  3980. // high-water mark computation.
  3981. off = (*p)->offset();
  3982. foff = off;
  3983. }
  3984. else
  3985. {
  3986. // The script may have inserted a skip forward, but it
  3987. // better not have moved backward.
  3988. if ((*p)->address() >= addr + (off - startoff))
  3989. {
  3990. if (!is_bss && off > foff)
  3991. gold_warning(_("script places BSS section in the middle "
  3992. "of a LOAD segment; space will be allocated "
  3993. "in the file"));
  3994. off += (*p)->address() - (addr + (off - startoff));
  3995. if (is_tls || !is_bss)
  3996. foff = off;
  3997. }
  3998. else
  3999. {
  4000. if (!layout->script_options()->saw_sections_clause())
  4001. gold_unreachable();
  4002. else
  4003. {
  4004. Output_section* os = (*p)->output_section();
  4005. // Cast to unsigned long long to avoid format warnings.
  4006. unsigned long long previous_dot =
  4007. static_cast<unsigned long long>(addr + (off - startoff));
  4008. unsigned long long dot =
  4009. static_cast<unsigned long long>((*p)->address());
  4010. if (os == NULL)
  4011. gold_error(_("dot moves backward in linker script "
  4012. "from 0x%llx to 0x%llx"), previous_dot, dot);
  4013. else
  4014. gold_error(_("address of section '%s' moves backward "
  4015. "from 0x%llx to 0x%llx"),
  4016. os->name(), previous_dot, dot);
  4017. }
  4018. }
  4019. (*p)->set_file_offset(foff);
  4020. (*p)->finalize_data_size();
  4021. }
  4022. if (parameters->incremental_update())
  4023. gold_debug(DEBUG_INCREMENTAL,
  4024. "set_section_list_addresses: %08lx %08lx %s",
  4025. static_cast<long>(off),
  4026. static_cast<long>((*p)->data_size()),
  4027. ((*p)->output_section() != NULL
  4028. ? (*p)->output_section()->name() : "(special)"));
  4029. // We want to ignore the size of a SHF_TLS SHT_NOBITS
  4030. // section. Such a section does not affect the size of a
  4031. // PT_LOAD segment.
  4032. if (!is_tls || !is_bss)
  4033. off += (*p)->data_size();
  4034. // We don't allocate space in the file for SHT_NOBITS sections,
  4035. // unless a script has force-placed one in the middle of a segment.
  4036. if (!is_bss)
  4037. foff = off;
  4038. if (off > maxoff)
  4039. maxoff = off;
  4040. if ((*p)->is_section())
  4041. {
  4042. (*p)->set_out_shndx(*pshndx);
  4043. ++*pshndx;
  4044. }
  4045. }
  4046. *poff = maxoff;
  4047. *pfoff = foff;
  4048. return addr + (maxoff - startoff);
  4049. }
  4050. // For a non-PT_LOAD segment, set the offset from the sections, if
  4051. // any. Add INCREASE to the file size and the memory size.
  4052. void
  4053. Output_segment::set_offset(unsigned int increase)
  4054. {
  4055. gold_assert(this->type_ != elfcpp::PT_LOAD);
  4056. gold_assert(!this->are_addresses_set_);
  4057. // A non-load section only uses output_lists_[0].
  4058. Output_data_list* pdl = &this->output_lists_[0];
  4059. if (pdl->empty())
  4060. {
  4061. gold_assert(increase == 0);
  4062. this->vaddr_ = 0;
  4063. this->paddr_ = 0;
  4064. this->are_addresses_set_ = true;
  4065. this->memsz_ = 0;
  4066. this->min_p_align_ = 0;
  4067. this->offset_ = 0;
  4068. this->filesz_ = 0;
  4069. return;
  4070. }
  4071. // Find the first and last section by address.
  4072. const Output_data* first = NULL;
  4073. const Output_data* last_data = NULL;
  4074. const Output_data* last_bss = NULL;
  4075. for (Output_data_list::const_iterator p = pdl->begin();
  4076. p != pdl->end();
  4077. ++p)
  4078. {
  4079. if (first == NULL
  4080. || (*p)->address() < first->address()
  4081. || ((*p)->address() == first->address()
  4082. && (*p)->data_size() < first->data_size()))
  4083. first = *p;
  4084. const Output_data** plast;
  4085. if ((*p)->is_section()
  4086. && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
  4087. plast = &last_bss;
  4088. else
  4089. plast = &last_data;
  4090. if (*plast == NULL
  4091. || (*p)->address() > (*plast)->address()
  4092. || ((*p)->address() == (*plast)->address()
  4093. && (*p)->data_size() > (*plast)->data_size()))
  4094. *plast = *p;
  4095. }
  4096. this->vaddr_ = first->address();
  4097. this->paddr_ = (first->has_load_address()
  4098. ? first->load_address()
  4099. : this->vaddr_);
  4100. this->are_addresses_set_ = true;
  4101. this->offset_ = first->offset();
  4102. if (last_data == NULL)
  4103. this->filesz_ = 0;
  4104. else
  4105. this->filesz_ = (last_data->address()
  4106. + last_data->data_size()
  4107. - this->vaddr_);
  4108. const Output_data* last = last_bss != NULL ? last_bss : last_data;
  4109. this->memsz_ = (last->address()
  4110. + last->data_size()
  4111. - this->vaddr_);
  4112. this->filesz_ += increase;
  4113. this->memsz_ += increase;
  4114. // If this is a RELRO segment, verify that the segment ends at a
  4115. // page boundary.
  4116. if (this->type_ == elfcpp::PT_GNU_RELRO)
  4117. {
  4118. uint64_t page_align = parameters->target().abi_pagesize();
  4119. uint64_t segment_end = this->vaddr_ + this->memsz_;
  4120. if (parameters->incremental_update())
  4121. {
  4122. // The INCREASE_RELRO calculation is bypassed for an incremental
  4123. // update, so we need to adjust the segment size manually here.
  4124. segment_end = align_address(segment_end, page_align);
  4125. this->memsz_ = segment_end - this->vaddr_;
  4126. }
  4127. else
  4128. gold_assert(segment_end == align_address(segment_end, page_align));
  4129. }
  4130. // If this is a TLS segment, align the memory size. The code in
  4131. // set_section_list ensures that the section after the TLS segment
  4132. // is aligned to give us room.
  4133. if (this->type_ == elfcpp::PT_TLS)
  4134. {
  4135. uint64_t segment_align = this->maximum_alignment();
  4136. gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
  4137. this->memsz_ = align_address(this->memsz_, segment_align);
  4138. }
  4139. }
  4140. // Set the TLS offsets of the sections in the PT_TLS segment.
  4141. void
  4142. Output_segment::set_tls_offsets()
  4143. {
  4144. gold_assert(this->type_ == elfcpp::PT_TLS);
  4145. for (Output_data_list::iterator p = this->output_lists_[0].begin();
  4146. p != this->output_lists_[0].end();
  4147. ++p)
  4148. (*p)->set_tls_offset(this->vaddr_);
  4149. }
  4150. // Return the first section.
  4151. Output_section*
  4152. Output_segment::first_section() const
  4153. {
  4154. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  4155. {
  4156. const Output_data_list* pdl = &this->output_lists_[i];
  4157. for (Output_data_list::const_iterator p = pdl->begin();
  4158. p != pdl->end();
  4159. ++p)
  4160. {
  4161. if ((*p)->is_section())
  4162. return (*p)->output_section();
  4163. }
  4164. }
  4165. return NULL;
  4166. }
  4167. // Return the number of Output_sections in an Output_segment.
  4168. unsigned int
  4169. Output_segment::output_section_count() const
  4170. {
  4171. unsigned int ret = 0;
  4172. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  4173. ret += this->output_section_count_list(&this->output_lists_[i]);
  4174. return ret;
  4175. }
  4176. // Return the number of Output_sections in an Output_data_list.
  4177. unsigned int
  4178. Output_segment::output_section_count_list(const Output_data_list* pdl) const
  4179. {
  4180. unsigned int count = 0;
  4181. for (Output_data_list::const_iterator p = pdl->begin();
  4182. p != pdl->end();
  4183. ++p)
  4184. {
  4185. if ((*p)->is_section())
  4186. ++count;
  4187. }
  4188. return count;
  4189. }
  4190. // Return the section attached to the list segment with the lowest
  4191. // load address. This is used when handling a PHDRS clause in a
  4192. // linker script.
  4193. Output_section*
  4194. Output_segment::section_with_lowest_load_address() const
  4195. {
  4196. Output_section* found = NULL;
  4197. uint64_t found_lma = 0;
  4198. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  4199. this->lowest_load_address_in_list(&this->output_lists_[i], &found,
  4200. &found_lma);
  4201. return found;
  4202. }
  4203. // Look through a list for a section with a lower load address.
  4204. void
  4205. Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
  4206. Output_section** found,
  4207. uint64_t* found_lma) const
  4208. {
  4209. for (Output_data_list::const_iterator p = pdl->begin();
  4210. p != pdl->end();
  4211. ++p)
  4212. {
  4213. if (!(*p)->is_section())
  4214. continue;
  4215. Output_section* os = static_cast<Output_section*>(*p);
  4216. uint64_t lma = (os->has_load_address()
  4217. ? os->load_address()
  4218. : os->address());
  4219. if (*found == NULL || lma < *found_lma)
  4220. {
  4221. *found = os;
  4222. *found_lma = lma;
  4223. }
  4224. }
  4225. }
  4226. // Write the segment data into *OPHDR.
  4227. template<int size, bool big_endian>
  4228. void
  4229. Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
  4230. {
  4231. ophdr->put_p_type(this->type_);
  4232. ophdr->put_p_offset(this->offset_);
  4233. ophdr->put_p_vaddr(this->vaddr_);
  4234. ophdr->put_p_paddr(this->paddr_);
  4235. ophdr->put_p_filesz(this->filesz_);
  4236. ophdr->put_p_memsz(this->memsz_);
  4237. ophdr->put_p_flags(this->flags_);
  4238. ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
  4239. }
  4240. // Write the section headers into V.
  4241. template<int size, bool big_endian>
  4242. unsigned char*
  4243. Output_segment::write_section_headers(const Layout* layout,
  4244. const Stringpool* secnamepool,
  4245. unsigned char* v,
  4246. unsigned int* pshndx) const
  4247. {
  4248. // Every section that is attached to a segment must be attached to a
  4249. // PT_LOAD segment, so we only write out section headers for PT_LOAD
  4250. // segments.
  4251. if (this->type_ != elfcpp::PT_LOAD)
  4252. return v;
  4253. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  4254. {
  4255. const Output_data_list* pdl = &this->output_lists_[i];
  4256. v = this->write_section_headers_list<size, big_endian>(layout,
  4257. secnamepool,
  4258. pdl,
  4259. v, pshndx);
  4260. }
  4261. return v;
  4262. }
  4263. template<int size, bool big_endian>
  4264. unsigned char*
  4265. Output_segment::write_section_headers_list(const Layout* layout,
  4266. const Stringpool* secnamepool,
  4267. const Output_data_list* pdl,
  4268. unsigned char* v,
  4269. unsigned int* pshndx) const
  4270. {
  4271. const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  4272. for (Output_data_list::const_iterator p = pdl->begin();
  4273. p != pdl->end();
  4274. ++p)
  4275. {
  4276. if ((*p)->is_section())
  4277. {
  4278. const Output_section* ps = static_cast<const Output_section*>(*p);
  4279. gold_assert(*pshndx == ps->out_shndx());
  4280. elfcpp::Shdr_write<size, big_endian> oshdr(v);
  4281. ps->write_header(layout, secnamepool, &oshdr);
  4282. v += shdr_size;
  4283. ++*pshndx;
  4284. }
  4285. }
  4286. return v;
  4287. }
  4288. // Print the output sections to the map file.
  4289. void
  4290. Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
  4291. {
  4292. if (this->type() != elfcpp::PT_LOAD)
  4293. return;
  4294. for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
  4295. this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
  4296. }
  4297. // Print an output section list to the map file.
  4298. void
  4299. Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
  4300. const Output_data_list* pdl) const
  4301. {
  4302. for (Output_data_list::const_iterator p = pdl->begin();
  4303. p != pdl->end();
  4304. ++p)
  4305. (*p)->print_to_mapfile(mapfile);
  4306. }
  4307. // Output_file methods.
  4308. Output_file::Output_file(const char* name)
  4309. : name_(name),
  4310. o_(-1),
  4311. file_size_(0),
  4312. base_(NULL),
  4313. map_is_anonymous_(false),
  4314. map_is_allocated_(false),
  4315. is_temporary_(false)
  4316. {
  4317. }
  4318. // Try to open an existing file. Returns false if the file doesn't
  4319. // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
  4320. // NULL, open that file as the base for incremental linking, and
  4321. // copy its contents to the new output file. This routine can
  4322. // be called for incremental updates, in which case WRITABLE should
  4323. // be true, or by the incremental-dump utility, in which case
  4324. // WRITABLE should be false.
  4325. bool
  4326. Output_file::open_base_file(const char* base_name, bool writable)
  4327. {
  4328. // The name "-" means "stdout".
  4329. if (strcmp(this->name_, "-") == 0)
  4330. return false;
  4331. bool use_base_file = base_name != NULL;
  4332. if (!use_base_file)
  4333. base_name = this->name_;
  4334. else if (strcmp(base_name, this->name_) == 0)
  4335. gold_fatal(_("%s: incremental base and output file name are the same"),
  4336. base_name);
  4337. // Don't bother opening files with a size of zero.
  4338. struct stat s;
  4339. if (::stat(base_name, &s) != 0)
  4340. {
  4341. gold_info(_("%s: stat: %s"), base_name, strerror(errno));
  4342. return false;
  4343. }
  4344. if (s.st_size == 0)
  4345. {
  4346. gold_info(_("%s: incremental base file is empty"), base_name);
  4347. return false;
  4348. }
  4349. // If we're using a base file, we want to open it read-only.
  4350. if (use_base_file)
  4351. writable = false;
  4352. int oflags = writable ? O_RDWR : O_RDONLY;
  4353. int o = open_descriptor(-1, base_name, oflags, 0);
  4354. if (o < 0)
  4355. {
  4356. gold_info(_("%s: open: %s"), base_name, strerror(errno));
  4357. return false;
  4358. }
  4359. // If the base file and the output file are different, open a
  4360. // new output file and read the contents from the base file into
  4361. // the newly-mapped region.
  4362. if (use_base_file)
  4363. {
  4364. this->open(s.st_size);
  4365. ssize_t bytes_to_read = s.st_size;
  4366. unsigned char* p = this->base_;
  4367. while (bytes_to_read > 0)
  4368. {
  4369. ssize_t len = ::read(o, p, bytes_to_read);
  4370. if (len < 0)
  4371. {
  4372. gold_info(_("%s: read failed: %s"), base_name, strerror(errno));
  4373. return false;
  4374. }
  4375. if (len == 0)
  4376. {
  4377. gold_info(_("%s: file too short: read only %lld of %lld bytes"),
  4378. base_name,
  4379. static_cast<long long>(s.st_size - bytes_to_read),
  4380. static_cast<long long>(s.st_size));
  4381. return false;
  4382. }
  4383. p += len;
  4384. bytes_to_read -= len;
  4385. }
  4386. ::close(o);
  4387. return true;
  4388. }
  4389. this->o_ = o;
  4390. this->file_size_ = s.st_size;
  4391. if (!this->map_no_anonymous(writable))
  4392. {
  4393. release_descriptor(o, true);
  4394. this->o_ = -1;
  4395. this->file_size_ = 0;
  4396. return false;
  4397. }
  4398. return true;
  4399. }
  4400. // Open the output file.
  4401. void
  4402. Output_file::open(off_t file_size)
  4403. {
  4404. this->file_size_ = file_size;
  4405. // Unlink the file first; otherwise the open() may fail if the file
  4406. // is busy (e.g. it's an executable that's currently being executed).
  4407. //
  4408. // However, the linker may be part of a system where a zero-length
  4409. // file is created for it to write to, with tight permissions (gcc
  4410. // 2.95 did something like this). Unlinking the file would work
  4411. // around those permission controls, so we only unlink if the file
  4412. // has a non-zero size. We also unlink only regular files to avoid
  4413. // trouble with directories/etc.
  4414. //
  4415. // If we fail, continue; this command is merely a best-effort attempt
  4416. // to improve the odds for open().
  4417. // We let the name "-" mean "stdout"
  4418. if (!this->is_temporary_)
  4419. {
  4420. if (strcmp(this->name_, "-") == 0)
  4421. this->o_ = STDOUT_FILENO;
  4422. else
  4423. {
  4424. struct stat s;
  4425. if (::stat(this->name_, &s) == 0
  4426. && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
  4427. {
  4428. if (s.st_size != 0)
  4429. ::unlink(this->name_);
  4430. else if (!parameters->options().relocatable())
  4431. {
  4432. // If we don't unlink the existing file, add execute
  4433. // permission where read permissions already exist
  4434. // and where the umask permits.
  4435. int mask = ::umask(0);
  4436. ::umask(mask);
  4437. s.st_mode |= (s.st_mode & 0444) >> 2;
  4438. ::chmod(this->name_, s.st_mode & ~mask);
  4439. }
  4440. }
  4441. int mode = parameters->options().relocatable() ? 0666 : 0777;
  4442. int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
  4443. mode);
  4444. if (o < 0)
  4445. gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
  4446. this->o_ = o;
  4447. }
  4448. }
  4449. this->map();
  4450. }
  4451. // Resize the output file.
  4452. void
  4453. Output_file::resize(off_t file_size)
  4454. {
  4455. // If the mmap is mapping an anonymous memory buffer, this is easy:
  4456. // just mremap to the new size. If it's mapping to a file, we want
  4457. // to unmap to flush to the file, then remap after growing the file.
  4458. if (this->map_is_anonymous_)
  4459. {
  4460. void* base;
  4461. if (!this->map_is_allocated_)
  4462. {
  4463. base = ::mremap(this->base_, this->file_size_, file_size,
  4464. MREMAP_MAYMOVE);
  4465. if (base == MAP_FAILED)
  4466. gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
  4467. }
  4468. else
  4469. {
  4470. base = realloc(this->base_, file_size);
  4471. if (base == NULL)
  4472. gold_nomem();
  4473. if (file_size > this->file_size_)
  4474. memset(static_cast<char*>(base) + this->file_size_, 0,
  4475. file_size - this->file_size_);
  4476. }
  4477. this->base_ = static_cast<unsigned char*>(base);
  4478. this->file_size_ = file_size;
  4479. }
  4480. else
  4481. {
  4482. this->unmap();
  4483. this->file_size_ = file_size;
  4484. if (!this->map_no_anonymous(true))
  4485. gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
  4486. }
  4487. }
  4488. // Map an anonymous block of memory which will later be written to the
  4489. // file. Return whether the map succeeded.
  4490. bool
  4491. Output_file::map_anonymous()
  4492. {
  4493. void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
  4494. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  4495. if (base == MAP_FAILED)
  4496. {
  4497. base = malloc(this->file_size_);
  4498. if (base == NULL)
  4499. return false;
  4500. memset(base, 0, this->file_size_);
  4501. this->map_is_allocated_ = true;
  4502. }
  4503. this->base_ = static_cast<unsigned char*>(base);
  4504. this->map_is_anonymous_ = true;
  4505. return true;
  4506. }
  4507. // Map the file into memory. Return whether the mapping succeeded.
  4508. // If WRITABLE is true, map with write access.
  4509. bool
  4510. Output_file::map_no_anonymous(bool writable)
  4511. {
  4512. const int o = this->o_;
  4513. // If the output file is not a regular file, don't try to mmap it;
  4514. // instead, we'll mmap a block of memory (an anonymous buffer), and
  4515. // then later write the buffer to the file.
  4516. void* base;
  4517. struct stat statbuf;
  4518. if (o == STDOUT_FILENO || o == STDERR_FILENO
  4519. || ::fstat(o, &statbuf) != 0
  4520. || !S_ISREG(statbuf.st_mode)
  4521. || this->is_temporary_)
  4522. return false;
  4523. // Ensure that we have disk space available for the file. If we
  4524. // don't do this, it is possible that we will call munmap, close,
  4525. // and exit with dirty buffers still in the cache with no assigned
  4526. // disk blocks. If the disk is out of space at that point, the
  4527. // output file will wind up incomplete, but we will have already
  4528. // exited. The alternative to fallocate would be to use fdatasync,
  4529. // but that would be a more significant performance hit.
  4530. if (writable)
  4531. {
  4532. int err = gold_fallocate(o, 0, this->file_size_);
  4533. if (err != 0)
  4534. gold_fatal(_("%s: %s"), this->name_, strerror(err));
  4535. }
  4536. // Map the file into memory.
  4537. int prot = PROT_READ;
  4538. if (writable)
  4539. prot |= PROT_WRITE;
  4540. base = ::mmap(NULL, this->file_size_, prot, MAP_SHARED, o, 0);
  4541. // The mmap call might fail because of file system issues: the file
  4542. // system might not support mmap at all, or it might not support
  4543. // mmap with PROT_WRITE.
  4544. if (base == MAP_FAILED)
  4545. return false;
  4546. this->map_is_anonymous_ = false;
  4547. this->base_ = static_cast<unsigned char*>(base);
  4548. return true;
  4549. }
  4550. // Map the file into memory.
  4551. void
  4552. Output_file::map()
  4553. {
  4554. if (parameters->options().mmap_output_file()
  4555. && this->map_no_anonymous(true))
  4556. return;
  4557. // The mmap call might fail because of file system issues: the file
  4558. // system might not support mmap at all, or it might not support
  4559. // mmap with PROT_WRITE. I'm not sure which errno values we will
  4560. // see in all cases, so if the mmap fails for any reason and we
  4561. // don't care about file contents, try for an anonymous map.
  4562. if (this->map_anonymous())
  4563. return;
  4564. gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
  4565. this->name_, static_cast<unsigned long>(this->file_size_),
  4566. strerror(errno));
  4567. }
  4568. // Unmap the file from memory.
  4569. void
  4570. Output_file::unmap()
  4571. {
  4572. if (this->map_is_anonymous_)
  4573. {
  4574. // We've already written out the data, so there is no reason to
  4575. // waste time unmapping or freeing the memory.
  4576. }
  4577. else
  4578. {
  4579. if (::munmap(this->base_, this->file_size_) < 0)
  4580. gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
  4581. }
  4582. this->base_ = NULL;
  4583. }
  4584. // Close the output file.
  4585. void
  4586. Output_file::close()
  4587. {
  4588. // If the map isn't file-backed, we need to write it now.
  4589. if (this->map_is_anonymous_ && !this->is_temporary_)
  4590. {
  4591. size_t bytes_to_write = this->file_size_;
  4592. size_t offset = 0;
  4593. while (bytes_to_write > 0)
  4594. {
  4595. ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
  4596. bytes_to_write);
  4597. if (bytes_written == 0)
  4598. gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
  4599. else if (bytes_written < 0)
  4600. gold_error(_("%s: write: %s"), this->name_, strerror(errno));
  4601. else
  4602. {
  4603. bytes_to_write -= bytes_written;
  4604. offset += bytes_written;
  4605. }
  4606. }
  4607. }
  4608. this->unmap();
  4609. // We don't close stdout or stderr
  4610. if (this->o_ != STDOUT_FILENO
  4611. && this->o_ != STDERR_FILENO
  4612. && !this->is_temporary_)
  4613. if (::close(this->o_) < 0)
  4614. gold_error(_("%s: close: %s"), this->name_, strerror(errno));
  4615. this->o_ = -1;
  4616. }
  4617. // Instantiate the templates we need. We could use the configure
  4618. // script to restrict this to only the ones for implemented targets.
  4619. #ifdef HAVE_TARGET_32_LITTLE
  4620. template
  4621. off_t
  4622. Output_section::add_input_section<32, false>(
  4623. Layout* layout,
  4624. Sized_relobj_file<32, false>* object,
  4625. unsigned int shndx,
  4626. const char* secname,
  4627. const elfcpp::Shdr<32, false>& shdr,
  4628. unsigned int reloc_shndx,
  4629. bool have_sections_script);
  4630. #endif
  4631. #ifdef HAVE_TARGET_32_BIG
  4632. template
  4633. off_t
  4634. Output_section::add_input_section<32, true>(
  4635. Layout* layout,
  4636. Sized_relobj_file<32, true>* object,
  4637. unsigned int shndx,
  4638. const char* secname,
  4639. const elfcpp::Shdr<32, true>& shdr,
  4640. unsigned int reloc_shndx,
  4641. bool have_sections_script);
  4642. #endif
  4643. #ifdef HAVE_TARGET_64_LITTLE
  4644. template
  4645. off_t
  4646. Output_section::add_input_section<64, false>(
  4647. Layout* layout,
  4648. Sized_relobj_file<64, false>* object,
  4649. unsigned int shndx,
  4650. const char* secname,
  4651. const elfcpp::Shdr<64, false>& shdr,
  4652. unsigned int reloc_shndx,
  4653. bool have_sections_script);
  4654. #endif
  4655. #ifdef HAVE_TARGET_64_BIG
  4656. template
  4657. off_t
  4658. Output_section::add_input_section<64, true>(
  4659. Layout* layout,
  4660. Sized_relobj_file<64, true>* object,
  4661. unsigned int shndx,
  4662. const char* secname,
  4663. const elfcpp::Shdr<64, true>& shdr,
  4664. unsigned int reloc_shndx,
  4665. bool have_sections_script);
  4666. #endif
  4667. #ifdef HAVE_TARGET_32_LITTLE
  4668. template
  4669. class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
  4670. #endif
  4671. #ifdef HAVE_TARGET_32_BIG
  4672. template
  4673. class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
  4674. #endif
  4675. #ifdef HAVE_TARGET_64_LITTLE
  4676. template
  4677. class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
  4678. #endif
  4679. #ifdef HAVE_TARGET_64_BIG
  4680. template
  4681. class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
  4682. #endif
  4683. #ifdef HAVE_TARGET_32_LITTLE
  4684. template
  4685. class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
  4686. #endif
  4687. #ifdef HAVE_TARGET_32_BIG
  4688. template
  4689. class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
  4690. #endif
  4691. #ifdef HAVE_TARGET_64_LITTLE
  4692. template
  4693. class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
  4694. #endif
  4695. #ifdef HAVE_TARGET_64_BIG
  4696. template
  4697. class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
  4698. #endif
  4699. #ifdef HAVE_TARGET_32_LITTLE
  4700. template
  4701. class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
  4702. #endif
  4703. #ifdef HAVE_TARGET_32_BIG
  4704. template
  4705. class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
  4706. #endif
  4707. #ifdef HAVE_TARGET_64_LITTLE
  4708. template
  4709. class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
  4710. #endif
  4711. #ifdef HAVE_TARGET_64_BIG
  4712. template
  4713. class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
  4714. #endif
  4715. #ifdef HAVE_TARGET_32_LITTLE
  4716. template
  4717. class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
  4718. #endif
  4719. #ifdef HAVE_TARGET_32_BIG
  4720. template
  4721. class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
  4722. #endif
  4723. #ifdef HAVE_TARGET_64_LITTLE
  4724. template
  4725. class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
  4726. #endif
  4727. #ifdef HAVE_TARGET_64_BIG
  4728. template
  4729. class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
  4730. #endif
  4731. #ifdef HAVE_TARGET_32_LITTLE
  4732. template
  4733. class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
  4734. #endif
  4735. #ifdef HAVE_TARGET_32_BIG
  4736. template
  4737. class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
  4738. #endif
  4739. #ifdef HAVE_TARGET_64_LITTLE
  4740. template
  4741. class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
  4742. #endif
  4743. #ifdef HAVE_TARGET_64_BIG
  4744. template
  4745. class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
  4746. #endif
  4747. #ifdef HAVE_TARGET_32_LITTLE
  4748. template
  4749. class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
  4750. #endif
  4751. #ifdef HAVE_TARGET_32_BIG
  4752. template
  4753. class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
  4754. #endif
  4755. #ifdef HAVE_TARGET_64_LITTLE
  4756. template
  4757. class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
  4758. #endif
  4759. #ifdef HAVE_TARGET_64_BIG
  4760. template
  4761. class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
  4762. #endif
  4763. #ifdef HAVE_TARGET_32_LITTLE
  4764. template
  4765. class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
  4766. #endif
  4767. #ifdef HAVE_TARGET_32_BIG
  4768. template
  4769. class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
  4770. #endif
  4771. #ifdef HAVE_TARGET_64_LITTLE
  4772. template
  4773. class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
  4774. #endif
  4775. #ifdef HAVE_TARGET_64_BIG
  4776. template
  4777. class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
  4778. #endif
  4779. #ifdef HAVE_TARGET_32_LITTLE
  4780. template
  4781. class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
  4782. #endif
  4783. #ifdef HAVE_TARGET_32_BIG
  4784. template
  4785. class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
  4786. #endif
  4787. #ifdef HAVE_TARGET_64_LITTLE
  4788. template
  4789. class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
  4790. #endif
  4791. #ifdef HAVE_TARGET_64_BIG
  4792. template
  4793. class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
  4794. #endif
  4795. #ifdef HAVE_TARGET_32_LITTLE
  4796. template
  4797. class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
  4798. #endif
  4799. #ifdef HAVE_TARGET_32_BIG
  4800. template
  4801. class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
  4802. #endif
  4803. #ifdef HAVE_TARGET_64_LITTLE
  4804. template
  4805. class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
  4806. #endif
  4807. #ifdef HAVE_TARGET_64_BIG
  4808. template
  4809. class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
  4810. #endif
  4811. #ifdef HAVE_TARGET_32_LITTLE
  4812. template
  4813. class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
  4814. #endif
  4815. #ifdef HAVE_TARGET_32_BIG
  4816. template
  4817. class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
  4818. #endif
  4819. #ifdef HAVE_TARGET_64_LITTLE
  4820. template
  4821. class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
  4822. #endif
  4823. #ifdef HAVE_TARGET_64_BIG
  4824. template
  4825. class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
  4826. #endif
  4827. #ifdef HAVE_TARGET_32_LITTLE
  4828. template
  4829. class Output_data_group<32, false>;
  4830. #endif
  4831. #ifdef HAVE_TARGET_32_BIG
  4832. template
  4833. class Output_data_group<32, true>;
  4834. #endif
  4835. #ifdef HAVE_TARGET_64_LITTLE
  4836. template
  4837. class Output_data_group<64, false>;
  4838. #endif
  4839. #ifdef HAVE_TARGET_64_BIG
  4840. template
  4841. class Output_data_group<64, true>;
  4842. #endif
  4843. template
  4844. class Output_data_got<32, false>;
  4845. template
  4846. class Output_data_got<32, true>;
  4847. template
  4848. class Output_data_got<64, false>;
  4849. template
  4850. class Output_data_got<64, true>;
  4851. } // End namespace gold.