layout.cc 184 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975
  1. // layout.cc -- lay out output file sections for gold
  2. // Copyright (C) 2006-2015 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 <cerrno>
  19. #include <cstring>
  20. #include <algorithm>
  21. #include <iostream>
  22. #include <fstream>
  23. #include <utility>
  24. #include <fcntl.h>
  25. #include <fnmatch.h>
  26. #include <unistd.h>
  27. #include "libiberty.h"
  28. #include "md5.h"
  29. #include "sha1.h"
  30. #include "parameters.h"
  31. #include "options.h"
  32. #include "mapfile.h"
  33. #include "script.h"
  34. #include "script-sections.h"
  35. #include "output.h"
  36. #include "symtab.h"
  37. #include "dynobj.h"
  38. #include "ehframe.h"
  39. #include "gdb-index.h"
  40. #include "compressed_output.h"
  41. #include "reduced_debug_output.h"
  42. #include "object.h"
  43. #include "reloc.h"
  44. #include "descriptors.h"
  45. #include "plugin.h"
  46. #include "incremental.h"
  47. #include "layout.h"
  48. namespace gold
  49. {
  50. // Class Free_list.
  51. // The total number of free lists used.
  52. unsigned int Free_list::num_lists = 0;
  53. // The total number of free list nodes used.
  54. unsigned int Free_list::num_nodes = 0;
  55. // The total number of calls to Free_list::remove.
  56. unsigned int Free_list::num_removes = 0;
  57. // The total number of nodes visited during calls to Free_list::remove.
  58. unsigned int Free_list::num_remove_visits = 0;
  59. // The total number of calls to Free_list::allocate.
  60. unsigned int Free_list::num_allocates = 0;
  61. // The total number of nodes visited during calls to Free_list::allocate.
  62. unsigned int Free_list::num_allocate_visits = 0;
  63. // Initialize the free list. Creates a single free list node that
  64. // describes the entire region of length LEN. If EXTEND is true,
  65. // allocate() is allowed to extend the region beyond its initial
  66. // length.
  67. void
  68. Free_list::init(off_t len, bool extend)
  69. {
  70. this->list_.push_front(Free_list_node(0, len));
  71. this->last_remove_ = this->list_.begin();
  72. this->extend_ = extend;
  73. this->length_ = len;
  74. ++Free_list::num_lists;
  75. ++Free_list::num_nodes;
  76. }
  77. // Remove a chunk from the free list. Because we start with a single
  78. // node that covers the entire section, and remove chunks from it one
  79. // at a time, we do not need to coalesce chunks or handle cases that
  80. // span more than one free node. We expect to remove chunks from the
  81. // free list in order, and we expect to have only a few chunks of free
  82. // space left (corresponding to files that have changed since the last
  83. // incremental link), so a simple linear list should provide sufficient
  84. // performance.
  85. void
  86. Free_list::remove(off_t start, off_t end)
  87. {
  88. if (start == end)
  89. return;
  90. gold_assert(start < end);
  91. ++Free_list::num_removes;
  92. Iterator p = this->last_remove_;
  93. if (p->start_ > start)
  94. p = this->list_.begin();
  95. for (; p != this->list_.end(); ++p)
  96. {
  97. ++Free_list::num_remove_visits;
  98. // Find a node that wholly contains the indicated region.
  99. if (p->start_ <= start && p->end_ >= end)
  100. {
  101. // Case 1: the indicated region spans the whole node.
  102. // Add some fuzz to avoid creating tiny free chunks.
  103. if (p->start_ + 3 >= start && p->end_ <= end + 3)
  104. p = this->list_.erase(p);
  105. // Case 2: remove a chunk from the start of the node.
  106. else if (p->start_ + 3 >= start)
  107. p->start_ = end;
  108. // Case 3: remove a chunk from the end of the node.
  109. else if (p->end_ <= end + 3)
  110. p->end_ = start;
  111. // Case 4: remove a chunk from the middle, and split
  112. // the node into two.
  113. else
  114. {
  115. Free_list_node newnode(p->start_, start);
  116. p->start_ = end;
  117. this->list_.insert(p, newnode);
  118. ++Free_list::num_nodes;
  119. }
  120. this->last_remove_ = p;
  121. return;
  122. }
  123. }
  124. // Did not find a node containing the given chunk. This could happen
  125. // because a small chunk was already removed due to the fuzz.
  126. gold_debug(DEBUG_INCREMENTAL,
  127. "Free_list::remove(%d,%d) not found",
  128. static_cast<int>(start), static_cast<int>(end));
  129. }
  130. // Allocate a chunk of size LEN from the free list. Returns -1ULL
  131. // if a sufficiently large chunk of free space is not found.
  132. // We use a simple first-fit algorithm.
  133. off_t
  134. Free_list::allocate(off_t len, uint64_t align, off_t minoff)
  135. {
  136. gold_debug(DEBUG_INCREMENTAL,
  137. "Free_list::allocate(%08lx, %d, %08lx)",
  138. static_cast<long>(len), static_cast<int>(align),
  139. static_cast<long>(minoff));
  140. if (len == 0)
  141. return align_address(minoff, align);
  142. ++Free_list::num_allocates;
  143. // We usually want to drop free chunks smaller than 4 bytes.
  144. // If we need to guarantee a minimum hole size, though, we need
  145. // to keep track of all free chunks.
  146. const int fuzz = this->min_hole_ > 0 ? 0 : 3;
  147. for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
  148. {
  149. ++Free_list::num_allocate_visits;
  150. off_t start = p->start_ > minoff ? p->start_ : minoff;
  151. start = align_address(start, align);
  152. off_t end = start + len;
  153. if (end > p->end_ && p->end_ == this->length_ && this->extend_)
  154. {
  155. this->length_ = end;
  156. p->end_ = end;
  157. }
  158. if (end == p->end_ || (end <= p->end_ - this->min_hole_))
  159. {
  160. if (p->start_ + fuzz >= start && p->end_ <= end + fuzz)
  161. this->list_.erase(p);
  162. else if (p->start_ + fuzz >= start)
  163. p->start_ = end;
  164. else if (p->end_ <= end + fuzz)
  165. p->end_ = start;
  166. else
  167. {
  168. Free_list_node newnode(p->start_, start);
  169. p->start_ = end;
  170. this->list_.insert(p, newnode);
  171. ++Free_list::num_nodes;
  172. }
  173. return start;
  174. }
  175. }
  176. if (this->extend_)
  177. {
  178. off_t start = align_address(this->length_, align);
  179. this->length_ = start + len;
  180. return start;
  181. }
  182. return -1;
  183. }
  184. // Dump the free list (for debugging).
  185. void
  186. Free_list::dump()
  187. {
  188. gold_info("Free list:\n start end length\n");
  189. for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
  190. gold_info(" %08lx %08lx %08lx", static_cast<long>(p->start_),
  191. static_cast<long>(p->end_),
  192. static_cast<long>(p->end_ - p->start_));
  193. }
  194. // Print the statistics for the free lists.
  195. void
  196. Free_list::print_stats()
  197. {
  198. fprintf(stderr, _("%s: total free lists: %u\n"),
  199. program_name, Free_list::num_lists);
  200. fprintf(stderr, _("%s: total free list nodes: %u\n"),
  201. program_name, Free_list::num_nodes);
  202. fprintf(stderr, _("%s: calls to Free_list::remove: %u\n"),
  203. program_name, Free_list::num_removes);
  204. fprintf(stderr, _("%s: nodes visited: %u\n"),
  205. program_name, Free_list::num_remove_visits);
  206. fprintf(stderr, _("%s: calls to Free_list::allocate: %u\n"),
  207. program_name, Free_list::num_allocates);
  208. fprintf(stderr, _("%s: nodes visited: %u\n"),
  209. program_name, Free_list::num_allocate_visits);
  210. }
  211. // A Hash_task computes the MD5 checksum of an array of char.
  212. class Hash_task : public Task
  213. {
  214. public:
  215. Hash_task(Output_file* of,
  216. size_t offset,
  217. size_t size,
  218. unsigned char* dst,
  219. Task_token* final_blocker)
  220. : of_(of), offset_(offset), size_(size), dst_(dst),
  221. final_blocker_(final_blocker)
  222. { }
  223. void
  224. run(Workqueue*)
  225. {
  226. const unsigned char* iv =
  227. this->of_->get_input_view(this->offset_, this->size_);
  228. md5_buffer(reinterpret_cast<const char*>(iv), this->size_, this->dst_);
  229. this->of_->free_input_view(this->offset_, this->size_, iv);
  230. }
  231. Task_token*
  232. is_runnable()
  233. { return NULL; }
  234. // Unblock FINAL_BLOCKER_ when done.
  235. void
  236. locks(Task_locker* tl)
  237. { tl->add(this, this->final_blocker_); }
  238. std::string
  239. get_name() const
  240. { return "Hash_task"; }
  241. private:
  242. Output_file* of_;
  243. const size_t offset_;
  244. const size_t size_;
  245. unsigned char* const dst_;
  246. Task_token* const final_blocker_;
  247. };
  248. // Layout::Relaxation_debug_check methods.
  249. // Check that sections and special data are in reset states.
  250. // We do not save states for Output_sections and special Output_data.
  251. // So we check that they have not assigned any addresses or offsets.
  252. // clean_up_after_relaxation simply resets their addresses and offsets.
  253. void
  254. Layout::Relaxation_debug_check::check_output_data_for_reset_values(
  255. const Layout::Section_list& sections,
  256. const Layout::Data_list& special_outputs,
  257. const Layout::Data_list& relax_outputs)
  258. {
  259. for(Layout::Section_list::const_iterator p = sections.begin();
  260. p != sections.end();
  261. ++p)
  262. gold_assert((*p)->address_and_file_offset_have_reset_values());
  263. for(Layout::Data_list::const_iterator p = special_outputs.begin();
  264. p != special_outputs.end();
  265. ++p)
  266. gold_assert((*p)->address_and_file_offset_have_reset_values());
  267. gold_assert(relax_outputs.empty());
  268. }
  269. // Save information of SECTIONS for checking later.
  270. void
  271. Layout::Relaxation_debug_check::read_sections(
  272. const Layout::Section_list& sections)
  273. {
  274. for(Layout::Section_list::const_iterator p = sections.begin();
  275. p != sections.end();
  276. ++p)
  277. {
  278. Output_section* os = *p;
  279. Section_info info;
  280. info.output_section = os;
  281. info.address = os->is_address_valid() ? os->address() : 0;
  282. info.data_size = os->is_data_size_valid() ? os->data_size() : -1;
  283. info.offset = os->is_offset_valid()? os->offset() : -1 ;
  284. this->section_infos_.push_back(info);
  285. }
  286. }
  287. // Verify SECTIONS using previously recorded information.
  288. void
  289. Layout::Relaxation_debug_check::verify_sections(
  290. const Layout::Section_list& sections)
  291. {
  292. size_t i = 0;
  293. for(Layout::Section_list::const_iterator p = sections.begin();
  294. p != sections.end();
  295. ++p, ++i)
  296. {
  297. Output_section* os = *p;
  298. uint64_t address = os->is_address_valid() ? os->address() : 0;
  299. off_t data_size = os->is_data_size_valid() ? os->data_size() : -1;
  300. off_t offset = os->is_offset_valid()? os->offset() : -1 ;
  301. if (i >= this->section_infos_.size())
  302. {
  303. gold_fatal("Section_info of %s missing.\n", os->name());
  304. }
  305. const Section_info& info = this->section_infos_[i];
  306. if (os != info.output_section)
  307. gold_fatal("Section order changed. Expecting %s but see %s\n",
  308. info.output_section->name(), os->name());
  309. if (address != info.address
  310. || data_size != info.data_size
  311. || offset != info.offset)
  312. gold_fatal("Section %s changed.\n", os->name());
  313. }
  314. }
  315. // Layout_task_runner methods.
  316. // Lay out the sections. This is called after all the input objects
  317. // have been read.
  318. void
  319. Layout_task_runner::run(Workqueue* workqueue, const Task* task)
  320. {
  321. // See if any of the input definitions violate the One Definition Rule.
  322. // TODO: if this is too slow, do this as a task, rather than inline.
  323. this->symtab_->detect_odr_violations(task, this->options_.output_file_name());
  324. Layout* layout = this->layout_;
  325. off_t file_size = layout->finalize(this->input_objects_,
  326. this->symtab_,
  327. this->target_,
  328. task);
  329. // Now we know the final size of the output file and we know where
  330. // each piece of information goes.
  331. if (this->mapfile_ != NULL)
  332. {
  333. this->mapfile_->print_discarded_sections(this->input_objects_);
  334. layout->print_to_mapfile(this->mapfile_);
  335. }
  336. Output_file* of;
  337. if (layout->incremental_base() == NULL)
  338. {
  339. of = new Output_file(parameters->options().output_file_name());
  340. if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
  341. of->set_is_temporary();
  342. of->open(file_size);
  343. }
  344. else
  345. {
  346. of = layout->incremental_base()->output_file();
  347. // Apply the incremental relocations for symbols whose values
  348. // have changed. We do this before we resize the file and start
  349. // writing anything else to it, so that we can read the old
  350. // incremental information from the file before (possibly)
  351. // overwriting it.
  352. if (parameters->incremental_update())
  353. layout->incremental_base()->apply_incremental_relocs(this->symtab_,
  354. this->layout_,
  355. of);
  356. of->resize(file_size);
  357. }
  358. // Queue up the final set of tasks.
  359. gold::queue_final_tasks(this->options_, this->input_objects_,
  360. this->symtab_, layout, workqueue, of);
  361. }
  362. // Layout methods.
  363. Layout::Layout(int number_of_input_files, Script_options* script_options)
  364. : number_of_input_files_(number_of_input_files),
  365. script_options_(script_options),
  366. namepool_(),
  367. sympool_(),
  368. dynpool_(),
  369. signatures_(),
  370. section_name_map_(),
  371. segment_list_(),
  372. section_list_(),
  373. unattached_section_list_(),
  374. special_output_list_(),
  375. relax_output_list_(),
  376. section_headers_(NULL),
  377. tls_segment_(NULL),
  378. relro_segment_(NULL),
  379. interp_segment_(NULL),
  380. increase_relro_(0),
  381. symtab_section_(NULL),
  382. symtab_xindex_(NULL),
  383. dynsym_section_(NULL),
  384. dynsym_xindex_(NULL),
  385. dynamic_section_(NULL),
  386. dynamic_symbol_(NULL),
  387. dynamic_data_(NULL),
  388. eh_frame_section_(NULL),
  389. eh_frame_data_(NULL),
  390. added_eh_frame_data_(false),
  391. eh_frame_hdr_section_(NULL),
  392. gdb_index_data_(NULL),
  393. build_id_note_(NULL),
  394. debug_abbrev_(NULL),
  395. debug_info_(NULL),
  396. group_signatures_(),
  397. output_file_size_(-1),
  398. have_added_input_section_(false),
  399. sections_are_attached_(false),
  400. input_requires_executable_stack_(false),
  401. input_with_gnu_stack_note_(false),
  402. input_without_gnu_stack_note_(false),
  403. has_static_tls_(false),
  404. any_postprocessing_sections_(false),
  405. resized_signatures_(false),
  406. have_stabstr_section_(false),
  407. section_ordering_specified_(false),
  408. unique_segment_for_sections_specified_(false),
  409. incremental_inputs_(NULL),
  410. record_output_section_data_from_script_(false),
  411. script_output_section_data_list_(),
  412. segment_states_(NULL),
  413. relaxation_debug_check_(NULL),
  414. section_order_map_(),
  415. section_segment_map_(),
  416. input_section_position_(),
  417. input_section_glob_(),
  418. incremental_base_(NULL),
  419. free_list_()
  420. {
  421. // Make space for more than enough segments for a typical file.
  422. // This is just for efficiency--it's OK if we wind up needing more.
  423. this->segment_list_.reserve(12);
  424. // We expect two unattached Output_data objects: the file header and
  425. // the segment headers.
  426. this->special_output_list_.reserve(2);
  427. // Initialize structure needed for an incremental build.
  428. if (parameters->incremental())
  429. this->incremental_inputs_ = new Incremental_inputs;
  430. // The section name pool is worth optimizing in all cases, because
  431. // it is small, but there are often overlaps due to .rel sections.
  432. this->namepool_.set_optimize();
  433. }
  434. // For incremental links, record the base file to be modified.
  435. void
  436. Layout::set_incremental_base(Incremental_binary* base)
  437. {
  438. this->incremental_base_ = base;
  439. this->free_list_.init(base->output_file()->filesize(), true);
  440. }
  441. // Hash a key we use to look up an output section mapping.
  442. size_t
  443. Layout::Hash_key::operator()(const Layout::Key& k) const
  444. {
  445. return k.first + k.second.first + k.second.second;
  446. }
  447. // These are the debug sections that are actually used by gdb.
  448. // Currently, we've checked versions of gdb up to and including 7.4.
  449. // We only check the part of the name that follows ".debug_" or
  450. // ".zdebug_".
  451. static const char* gdb_sections[] =
  452. {
  453. "abbrev",
  454. "addr", // Fission extension
  455. // "aranges", // not used by gdb as of 7.4
  456. "frame",
  457. "gdb_scripts",
  458. "info",
  459. "types",
  460. "line",
  461. "loc",
  462. "macinfo",
  463. "macro",
  464. // "pubnames", // not used by gdb as of 7.4
  465. // "pubtypes", // not used by gdb as of 7.4
  466. // "gnu_pubnames", // Fission extension
  467. // "gnu_pubtypes", // Fission extension
  468. "ranges",
  469. "str",
  470. "str_offsets",
  471. };
  472. // This is the minimum set of sections needed for line numbers.
  473. static const char* lines_only_debug_sections[] =
  474. {
  475. "abbrev",
  476. // "addr", // Fission extension
  477. // "aranges", // not used by gdb as of 7.4
  478. // "frame",
  479. // "gdb_scripts",
  480. "info",
  481. // "types",
  482. "line",
  483. // "loc",
  484. // "macinfo",
  485. // "macro",
  486. // "pubnames", // not used by gdb as of 7.4
  487. // "pubtypes", // not used by gdb as of 7.4
  488. // "gnu_pubnames", // Fission extension
  489. // "gnu_pubtypes", // Fission extension
  490. // "ranges",
  491. "str",
  492. "str_offsets", // Fission extension
  493. };
  494. // These sections are the DWARF fast-lookup tables, and are not needed
  495. // when building a .gdb_index section.
  496. static const char* gdb_fast_lookup_sections[] =
  497. {
  498. "aranges",
  499. "pubnames",
  500. "gnu_pubnames",
  501. "pubtypes",
  502. "gnu_pubtypes",
  503. };
  504. // Returns whether the given debug section is in the list of
  505. // debug-sections-used-by-some-version-of-gdb. SUFFIX is the
  506. // portion of the name following ".debug_" or ".zdebug_".
  507. static inline bool
  508. is_gdb_debug_section(const char* suffix)
  509. {
  510. // We can do this faster: binary search or a hashtable. But why bother?
  511. for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
  512. if (strcmp(suffix, gdb_sections[i]) == 0)
  513. return true;
  514. return false;
  515. }
  516. // Returns whether the given section is needed for lines-only debugging.
  517. static inline bool
  518. is_lines_only_debug_section(const char* suffix)
  519. {
  520. // We can do this faster: binary search or a hashtable. But why bother?
  521. for (size_t i = 0;
  522. i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections);
  523. ++i)
  524. if (strcmp(suffix, lines_only_debug_sections[i]) == 0)
  525. return true;
  526. return false;
  527. }
  528. // Returns whether the given section is a fast-lookup section that
  529. // will not be needed when building a .gdb_index section.
  530. static inline bool
  531. is_gdb_fast_lookup_section(const char* suffix)
  532. {
  533. // We can do this faster: binary search or a hashtable. But why bother?
  534. for (size_t i = 0;
  535. i < sizeof(gdb_fast_lookup_sections)/sizeof(*gdb_fast_lookup_sections);
  536. ++i)
  537. if (strcmp(suffix, gdb_fast_lookup_sections[i]) == 0)
  538. return true;
  539. return false;
  540. }
  541. // Sometimes we compress sections. This is typically done for
  542. // sections that are not part of normal program execution (such as
  543. // .debug_* sections), and where the readers of these sections know
  544. // how to deal with compressed sections. This routine doesn't say for
  545. // certain whether we'll compress -- it depends on commandline options
  546. // as well -- just whether this section is a candidate for compression.
  547. // (The Output_compressed_section class decides whether to compress
  548. // a given section, and picks the name of the compressed section.)
  549. static bool
  550. is_compressible_debug_section(const char* secname)
  551. {
  552. return (is_prefix_of(".debug", secname));
  553. }
  554. // We may see compressed debug sections in input files. Return TRUE
  555. // if this is the name of a compressed debug section.
  556. bool
  557. is_compressed_debug_section(const char* secname)
  558. {
  559. return (is_prefix_of(".zdebug", secname));
  560. }
  561. std::string
  562. corresponding_uncompressed_section_name(std::string secname)
  563. {
  564. gold_assert(secname[0] == '.' && secname[1] == 'z');
  565. std::string ret(".");
  566. ret.append(secname, 2, std::string::npos);
  567. return ret;
  568. }
  569. // Whether to include this section in the link.
  570. template<int size, bool big_endian>
  571. bool
  572. Layout::include_section(Sized_relobj_file<size, big_endian>*, const char* name,
  573. const elfcpp::Shdr<size, big_endian>& shdr)
  574. {
  575. if (!parameters->options().relocatable()
  576. && (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE))
  577. return false;
  578. elfcpp::Elf_Word sh_type = shdr.get_sh_type();
  579. if ((sh_type >= elfcpp::SHT_LOOS && sh_type <= elfcpp::SHT_HIOS)
  580. || (sh_type >= elfcpp::SHT_LOPROC && sh_type <= elfcpp::SHT_HIPROC))
  581. return parameters->target().should_include_section(sh_type);
  582. switch (sh_type)
  583. {
  584. case elfcpp::SHT_NULL:
  585. case elfcpp::SHT_SYMTAB:
  586. case elfcpp::SHT_DYNSYM:
  587. case elfcpp::SHT_HASH:
  588. case elfcpp::SHT_DYNAMIC:
  589. case elfcpp::SHT_SYMTAB_SHNDX:
  590. return false;
  591. case elfcpp::SHT_STRTAB:
  592. // Discard the sections which have special meanings in the ELF
  593. // ABI. Keep others (e.g., .stabstr). We could also do this by
  594. // checking the sh_link fields of the appropriate sections.
  595. return (strcmp(name, ".dynstr") != 0
  596. && strcmp(name, ".strtab") != 0
  597. && strcmp(name, ".shstrtab") != 0);
  598. case elfcpp::SHT_RELA:
  599. case elfcpp::SHT_REL:
  600. case elfcpp::SHT_GROUP:
  601. // If we are emitting relocations these should be handled
  602. // elsewhere.
  603. gold_assert(!parameters->options().relocatable());
  604. return false;
  605. case elfcpp::SHT_PROGBITS:
  606. if (parameters->options().strip_debug()
  607. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  608. {
  609. if (is_debug_info_section(name))
  610. return false;
  611. }
  612. if (parameters->options().strip_debug_non_line()
  613. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  614. {
  615. // Debugging sections can only be recognized by name.
  616. if (is_prefix_of(".debug_", name)
  617. && !is_lines_only_debug_section(name + 7))
  618. return false;
  619. if (is_prefix_of(".zdebug_", name)
  620. && !is_lines_only_debug_section(name + 8))
  621. return false;
  622. }
  623. if (parameters->options().strip_debug_gdb()
  624. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  625. {
  626. // Debugging sections can only be recognized by name.
  627. if (is_prefix_of(".debug_", name)
  628. && !is_gdb_debug_section(name + 7))
  629. return false;
  630. if (is_prefix_of(".zdebug_", name)
  631. && !is_gdb_debug_section(name + 8))
  632. return false;
  633. }
  634. if (parameters->options().gdb_index()
  635. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  636. {
  637. // When building .gdb_index, we can strip .debug_pubnames,
  638. // .debug_pubtypes, and .debug_aranges sections.
  639. if (is_prefix_of(".debug_", name)
  640. && is_gdb_fast_lookup_section(name + 7))
  641. return false;
  642. if (is_prefix_of(".zdebug_", name)
  643. && is_gdb_fast_lookup_section(name + 8))
  644. return false;
  645. }
  646. if (parameters->options().strip_lto_sections()
  647. && !parameters->options().relocatable()
  648. && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  649. {
  650. // Ignore LTO sections containing intermediate code.
  651. if (is_prefix_of(".gnu.lto_", name))
  652. return false;
  653. }
  654. // The GNU linker strips .gnu_debuglink sections, so we do too.
  655. // This is a feature used to keep debugging information in
  656. // separate files.
  657. if (strcmp(name, ".gnu_debuglink") == 0)
  658. return false;
  659. return true;
  660. default:
  661. return true;
  662. }
  663. }
  664. // Return an output section named NAME, or NULL if there is none.
  665. Output_section*
  666. Layout::find_output_section(const char* name) const
  667. {
  668. for (Section_list::const_iterator p = this->section_list_.begin();
  669. p != this->section_list_.end();
  670. ++p)
  671. if (strcmp((*p)->name(), name) == 0)
  672. return *p;
  673. return NULL;
  674. }
  675. // Return an output segment of type TYPE, with segment flags SET set
  676. // and segment flags CLEAR clear. Return NULL if there is none.
  677. Output_segment*
  678. Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
  679. elfcpp::Elf_Word clear) const
  680. {
  681. for (Segment_list::const_iterator p = this->segment_list_.begin();
  682. p != this->segment_list_.end();
  683. ++p)
  684. if (static_cast<elfcpp::PT>((*p)->type()) == type
  685. && ((*p)->flags() & set) == set
  686. && ((*p)->flags() & clear) == 0)
  687. return *p;
  688. return NULL;
  689. }
  690. // When we put a .ctors or .dtors section with more than one word into
  691. // a .init_array or .fini_array section, we need to reverse the words
  692. // in the .ctors/.dtors section. This is because .init_array executes
  693. // constructors front to back, where .ctors executes them back to
  694. // front, and vice-versa for .fini_array/.dtors. Although we do want
  695. // to remap .ctors/.dtors into .init_array/.fini_array because it can
  696. // be more efficient, we don't want to change the order in which
  697. // constructors/destructors are run. This set just keeps track of
  698. // these sections which need to be reversed. It is only changed by
  699. // Layout::layout. It should be a private member of Layout, but that
  700. // would require layout.h to #include object.h to get the definition
  701. // of Section_id.
  702. static Unordered_set<Section_id, Section_id_hash> ctors_sections_in_init_array;
  703. // Return whether OBJECT/SHNDX is a .ctors/.dtors section mapped to a
  704. // .init_array/.fini_array section.
  705. bool
  706. Layout::is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const
  707. {
  708. return (ctors_sections_in_init_array.find(Section_id(relobj, shndx))
  709. != ctors_sections_in_init_array.end());
  710. }
  711. // Return the output section to use for section NAME with type TYPE
  712. // and section flags FLAGS. NAME must be canonicalized in the string
  713. // pool, and NAME_KEY is the key. ORDER is where this should appear
  714. // in the output sections. IS_RELRO is true for a relro section.
  715. Output_section*
  716. Layout::get_output_section(const char* name, Stringpool::Key name_key,
  717. elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
  718. Output_section_order order, bool is_relro)
  719. {
  720. elfcpp::Elf_Word lookup_type = type;
  721. // For lookup purposes, treat INIT_ARRAY, FINI_ARRAY, and
  722. // PREINIT_ARRAY like PROGBITS. This ensures that we combine
  723. // .init_array, .fini_array, and .preinit_array sections by name
  724. // whatever their type in the input file. We do this because the
  725. // types are not always right in the input files.
  726. if (lookup_type == elfcpp::SHT_INIT_ARRAY
  727. || lookup_type == elfcpp::SHT_FINI_ARRAY
  728. || lookup_type == elfcpp::SHT_PREINIT_ARRAY)
  729. lookup_type = elfcpp::SHT_PROGBITS;
  730. elfcpp::Elf_Xword lookup_flags = flags;
  731. // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
  732. // read-write with read-only sections. Some other ELF linkers do
  733. // not do this. FIXME: Perhaps there should be an option
  734. // controlling this.
  735. lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
  736. const Key key(name_key, std::make_pair(lookup_type, lookup_flags));
  737. const std::pair<Key, Output_section*> v(key, NULL);
  738. std::pair<Section_name_map::iterator, bool> ins(
  739. this->section_name_map_.insert(v));
  740. if (!ins.second)
  741. return ins.first->second;
  742. else
  743. {
  744. // This is the first time we've seen this name/type/flags
  745. // combination. For compatibility with the GNU linker, we
  746. // combine sections with contents and zero flags with sections
  747. // with non-zero flags. This is a workaround for cases where
  748. // assembler code forgets to set section flags. FIXME: Perhaps
  749. // there should be an option to control this.
  750. Output_section* os = NULL;
  751. if (lookup_type == elfcpp::SHT_PROGBITS)
  752. {
  753. if (flags == 0)
  754. {
  755. Output_section* same_name = this->find_output_section(name);
  756. if (same_name != NULL
  757. && (same_name->type() == elfcpp::SHT_PROGBITS
  758. || same_name->type() == elfcpp::SHT_INIT_ARRAY
  759. || same_name->type() == elfcpp::SHT_FINI_ARRAY
  760. || same_name->type() == elfcpp::SHT_PREINIT_ARRAY)
  761. && (same_name->flags() & elfcpp::SHF_TLS) == 0)
  762. os = same_name;
  763. }
  764. else if ((flags & elfcpp::SHF_TLS) == 0)
  765. {
  766. elfcpp::Elf_Xword zero_flags = 0;
  767. const Key zero_key(name_key, std::make_pair(lookup_type,
  768. zero_flags));
  769. Section_name_map::iterator p =
  770. this->section_name_map_.find(zero_key);
  771. if (p != this->section_name_map_.end())
  772. os = p->second;
  773. }
  774. }
  775. if (os == NULL)
  776. os = this->make_output_section(name, type, flags, order, is_relro);
  777. ins.first->second = os;
  778. return os;
  779. }
  780. }
  781. // Returns TRUE iff NAME (an input section from RELOBJ) will
  782. // be mapped to an output section that should be KEPT.
  783. bool
  784. Layout::keep_input_section(const Relobj* relobj, const char* name)
  785. {
  786. if (! this->script_options_->saw_sections_clause())
  787. return false;
  788. Script_sections* ss = this->script_options_->script_sections();
  789. const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
  790. Output_section** output_section_slot;
  791. Script_sections::Section_type script_section_type;
  792. bool keep;
  793. name = ss->output_section_name(file_name, name, &output_section_slot,
  794. &script_section_type, &keep);
  795. return name != NULL && keep;
  796. }
  797. // Clear the input section flags that should not be copied to the
  798. // output section.
  799. elfcpp::Elf_Xword
  800. Layout::get_output_section_flags(elfcpp::Elf_Xword input_section_flags)
  801. {
  802. // Some flags in the input section should not be automatically
  803. // copied to the output section.
  804. input_section_flags &= ~ (elfcpp::SHF_INFO_LINK
  805. | elfcpp::SHF_GROUP
  806. | elfcpp::SHF_COMPRESSED
  807. | elfcpp::SHF_MERGE
  808. | elfcpp::SHF_STRINGS);
  809. // We only clear the SHF_LINK_ORDER flag in for
  810. // a non-relocatable link.
  811. if (!parameters->options().relocatable())
  812. input_section_flags &= ~elfcpp::SHF_LINK_ORDER;
  813. return input_section_flags;
  814. }
  815. // Pick the output section to use for section NAME, in input file
  816. // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
  817. // linker created section. IS_INPUT_SECTION is true if we are
  818. // choosing an output section for an input section found in a input
  819. // file. ORDER is where this section should appear in the output
  820. // sections. IS_RELRO is true for a relro section. This will return
  821. // NULL if the input section should be discarded.
  822. Output_section*
  823. Layout::choose_output_section(const Relobj* relobj, const char* name,
  824. elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
  825. bool is_input_section, Output_section_order order,
  826. bool is_relro)
  827. {
  828. // We should not see any input sections after we have attached
  829. // sections to segments.
  830. gold_assert(!is_input_section || !this->sections_are_attached_);
  831. flags = this->get_output_section_flags(flags);
  832. if (this->script_options_->saw_sections_clause())
  833. {
  834. // We are using a SECTIONS clause, so the output section is
  835. // chosen based only on the name.
  836. Script_sections* ss = this->script_options_->script_sections();
  837. const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
  838. Output_section** output_section_slot;
  839. Script_sections::Section_type script_section_type;
  840. const char* orig_name = name;
  841. bool keep;
  842. name = ss->output_section_name(file_name, name, &output_section_slot,
  843. &script_section_type, &keep);
  844. if (name == NULL)
  845. {
  846. gold_debug(DEBUG_SCRIPT, _("Unable to create output section '%s' "
  847. "because it is not allowed by the "
  848. "SECTIONS clause of the linker script"),
  849. orig_name);
  850. // The SECTIONS clause says to discard this input section.
  851. return NULL;
  852. }
  853. // We can only handle script section types ST_NONE and ST_NOLOAD.
  854. switch (script_section_type)
  855. {
  856. case Script_sections::ST_NONE:
  857. break;
  858. case Script_sections::ST_NOLOAD:
  859. flags &= elfcpp::SHF_ALLOC;
  860. break;
  861. default:
  862. gold_unreachable();
  863. }
  864. // If this is an orphan section--one not mentioned in the linker
  865. // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
  866. // default processing below.
  867. if (output_section_slot != NULL)
  868. {
  869. if (*output_section_slot != NULL)
  870. {
  871. (*output_section_slot)->update_flags_for_input_section(flags);
  872. return *output_section_slot;
  873. }
  874. // We don't put sections found in the linker script into
  875. // SECTION_NAME_MAP_. That keeps us from getting confused
  876. // if an orphan section is mapped to a section with the same
  877. // name as one in the linker script.
  878. name = this->namepool_.add(name, false, NULL);
  879. Output_section* os = this->make_output_section(name, type, flags,
  880. order, is_relro);
  881. os->set_found_in_sections_clause();
  882. // Special handling for NOLOAD sections.
  883. if (script_section_type == Script_sections::ST_NOLOAD)
  884. {
  885. os->set_is_noload();
  886. // The constructor of Output_section sets addresses of non-ALLOC
  887. // sections to 0 by default. We don't want that for NOLOAD
  888. // sections even if they have no SHF_ALLOC flag.
  889. if ((os->flags() & elfcpp::SHF_ALLOC) == 0
  890. && os->is_address_valid())
  891. {
  892. gold_assert(os->address() == 0
  893. && !os->is_offset_valid()
  894. && !os->is_data_size_valid());
  895. os->reset_address_and_file_offset();
  896. }
  897. }
  898. *output_section_slot = os;
  899. return os;
  900. }
  901. }
  902. // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
  903. size_t len = strlen(name);
  904. std::string uncompressed_name;
  905. // Compressed debug sections should be mapped to the corresponding
  906. // uncompressed section.
  907. if (is_compressed_debug_section(name))
  908. {
  909. uncompressed_name =
  910. corresponding_uncompressed_section_name(std::string(name, len));
  911. name = uncompressed_name.c_str();
  912. len = uncompressed_name.length();
  913. }
  914. // Turn NAME from the name of the input section into the name of the
  915. // output section.
  916. if (is_input_section
  917. && !this->script_options_->saw_sections_clause()
  918. && !parameters->options().relocatable())
  919. {
  920. const char *orig_name = name;
  921. name = parameters->target().output_section_name(relobj, name, &len);
  922. if (name == NULL)
  923. name = Layout::output_section_name(relobj, orig_name, &len);
  924. }
  925. Stringpool::Key name_key;
  926. name = this->namepool_.add_with_length(name, len, true, &name_key);
  927. // Find or make the output section. The output section is selected
  928. // based on the section name, type, and flags.
  929. return this->get_output_section(name, name_key, type, flags, order, is_relro);
  930. }
  931. // For incremental links, record the initial fixed layout of a section
  932. // from the base file, and return a pointer to the Output_section.
  933. template<int size, bool big_endian>
  934. Output_section*
  935. Layout::init_fixed_output_section(const char* name,
  936. elfcpp::Shdr<size, big_endian>& shdr)
  937. {
  938. unsigned int sh_type = shdr.get_sh_type();
  939. // We preserve the layout of PROGBITS, NOBITS, INIT_ARRAY, FINI_ARRAY,
  940. // PRE_INIT_ARRAY, and NOTE sections.
  941. // All others will be created from scratch and reallocated.
  942. if (!can_incremental_update(sh_type))
  943. return NULL;
  944. // If we're generating a .gdb_index section, we need to regenerate
  945. // it from scratch.
  946. if (parameters->options().gdb_index()
  947. && sh_type == elfcpp::SHT_PROGBITS
  948. && strcmp(name, ".gdb_index") == 0)
  949. return NULL;
  950. typename elfcpp::Elf_types<size>::Elf_Addr sh_addr = shdr.get_sh_addr();
  951. typename elfcpp::Elf_types<size>::Elf_Off sh_offset = shdr.get_sh_offset();
  952. typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
  953. typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
  954. typename elfcpp::Elf_types<size>::Elf_WXword sh_addralign =
  955. shdr.get_sh_addralign();
  956. // Make the output section.
  957. Stringpool::Key name_key;
  958. name = this->namepool_.add(name, true, &name_key);
  959. Output_section* os = this->get_output_section(name, name_key, sh_type,
  960. sh_flags, ORDER_INVALID, false);
  961. os->set_fixed_layout(sh_addr, sh_offset, sh_size, sh_addralign);
  962. if (sh_type != elfcpp::SHT_NOBITS)
  963. this->free_list_.remove(sh_offset, sh_offset + sh_size);
  964. return os;
  965. }
  966. // Return the index by which an input section should be ordered. This
  967. // is used to sort some .text sections, for compatibility with GNU ld.
  968. int
  969. Layout::special_ordering_of_input_section(const char* name)
  970. {
  971. // The GNU linker has some special handling for some sections that
  972. // wind up in the .text section. Sections that start with these
  973. // prefixes must appear first, and must appear in the order listed
  974. // here.
  975. static const char* const text_section_sort[] =
  976. {
  977. ".text.unlikely",
  978. ".text.exit",
  979. ".text.startup",
  980. ".text.hot"
  981. };
  982. for (size_t i = 0;
  983. i < sizeof(text_section_sort) / sizeof(text_section_sort[0]);
  984. i++)
  985. if (is_prefix_of(text_section_sort[i], name))
  986. return i;
  987. return -1;
  988. }
  989. // Return the output section to use for input section SHNDX, with name
  990. // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
  991. // index of a relocation section which applies to this section, or 0
  992. // if none, or -1U if more than one. RELOC_TYPE is the type of the
  993. // relocation section if there is one. Set *OFF to the offset of this
  994. // input section without the output section. Return NULL if the
  995. // section should be discarded. Set *OFF to -1 if the section
  996. // contents should not be written directly to the output file, but
  997. // will instead receive special handling.
  998. template<int size, bool big_endian>
  999. Output_section*
  1000. Layout::layout(Sized_relobj_file<size, big_endian>* object, unsigned int shndx,
  1001. const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
  1002. unsigned int reloc_shndx, unsigned int, off_t* off)
  1003. {
  1004. *off = 0;
  1005. if (!this->include_section(object, name, shdr))
  1006. return NULL;
  1007. elfcpp::Elf_Word sh_type = shdr.get_sh_type();
  1008. // In a relocatable link a grouped section must not be combined with
  1009. // any other sections.
  1010. Output_section* os;
  1011. if (parameters->options().relocatable()
  1012. && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
  1013. {
  1014. // Some flags in the input section should not be automatically
  1015. // copied to the output section.
  1016. elfcpp::Elf_Xword flags = (shdr.get_sh_flags()
  1017. & ~ elfcpp::SHF_COMPRESSED);
  1018. name = this->namepool_.add(name, true, NULL);
  1019. os = this->make_output_section(name, sh_type, flags,
  1020. ORDER_INVALID, false);
  1021. }
  1022. else
  1023. {
  1024. // Plugins can choose to place one or more subsets of sections in
  1025. // unique segments and this is done by mapping these section subsets
  1026. // to unique output sections. Check if this section needs to be
  1027. // remapped to a unique output section.
  1028. Section_segment_map::iterator it
  1029. = this->section_segment_map_.find(Const_section_id(object, shndx));
  1030. if (it == this->section_segment_map_.end())
  1031. {
  1032. os = this->choose_output_section(object, name, sh_type,
  1033. shdr.get_sh_flags(), true,
  1034. ORDER_INVALID, false);
  1035. }
  1036. else
  1037. {
  1038. // We know the name of the output section, directly call
  1039. // get_output_section here by-passing choose_output_section.
  1040. elfcpp::Elf_Xword flags
  1041. = this->get_output_section_flags(shdr.get_sh_flags());
  1042. const char* os_name = it->second->name;
  1043. Stringpool::Key name_key;
  1044. os_name = this->namepool_.add(os_name, true, &name_key);
  1045. os = this->get_output_section(os_name, name_key, sh_type, flags,
  1046. ORDER_INVALID, false);
  1047. if (!os->is_unique_segment())
  1048. {
  1049. os->set_is_unique_segment();
  1050. os->set_extra_segment_flags(it->second->flags);
  1051. os->set_segment_alignment(it->second->align);
  1052. }
  1053. }
  1054. if (os == NULL)
  1055. return NULL;
  1056. }
  1057. // By default the GNU linker sorts input sections whose names match
  1058. // .ctors.*, .dtors.*, .init_array.*, or .fini_array.*. The
  1059. // sections are sorted by name. This is used to implement
  1060. // constructor priority ordering. We are compatible. When we put
  1061. // .ctor sections in .init_array and .dtor sections in .fini_array,
  1062. // we must also sort plain .ctor and .dtor sections.
  1063. if (!this->script_options_->saw_sections_clause()
  1064. && !parameters->options().relocatable()
  1065. && (is_prefix_of(".ctors.", name)
  1066. || is_prefix_of(".dtors.", name)
  1067. || is_prefix_of(".init_array.", name)
  1068. || is_prefix_of(".fini_array.", name)
  1069. || (parameters->options().ctors_in_init_array()
  1070. && (strcmp(name, ".ctors") == 0
  1071. || strcmp(name, ".dtors") == 0))))
  1072. os->set_must_sort_attached_input_sections();
  1073. // By default the GNU linker sorts some special text sections ahead
  1074. // of others. We are compatible.
  1075. if (parameters->options().text_reorder()
  1076. && !this->script_options_->saw_sections_clause()
  1077. && !this->is_section_ordering_specified()
  1078. && !parameters->options().relocatable()
  1079. && Layout::special_ordering_of_input_section(name) >= 0)
  1080. os->set_must_sort_attached_input_sections();
  1081. // If this is a .ctors or .ctors.* section being mapped to a
  1082. // .init_array section, or a .dtors or .dtors.* section being mapped
  1083. // to a .fini_array section, we will need to reverse the words if
  1084. // there is more than one. Record this section for later. See
  1085. // ctors_sections_in_init_array above.
  1086. if (!this->script_options_->saw_sections_clause()
  1087. && !parameters->options().relocatable()
  1088. && shdr.get_sh_size() > size / 8
  1089. && (((strcmp(name, ".ctors") == 0
  1090. || is_prefix_of(".ctors.", name))
  1091. && strcmp(os->name(), ".init_array") == 0)
  1092. || ((strcmp(name, ".dtors") == 0
  1093. || is_prefix_of(".dtors.", name))
  1094. && strcmp(os->name(), ".fini_array") == 0)))
  1095. ctors_sections_in_init_array.insert(Section_id(object, shndx));
  1096. // FIXME: Handle SHF_LINK_ORDER somewhere.
  1097. elfcpp::Elf_Xword orig_flags = os->flags();
  1098. *off = os->add_input_section(this, object, shndx, name, shdr, reloc_shndx,
  1099. this->script_options_->saw_sections_clause());
  1100. // If the flags changed, we may have to change the order.
  1101. if ((orig_flags & elfcpp::SHF_ALLOC) != 0)
  1102. {
  1103. orig_flags &= (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
  1104. elfcpp::Elf_Xword new_flags =
  1105. os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
  1106. if (orig_flags != new_flags)
  1107. os->set_order(this->default_section_order(os, false));
  1108. }
  1109. this->have_added_input_section_ = true;
  1110. return os;
  1111. }
  1112. // Maps section SECN to SEGMENT s.
  1113. void
  1114. Layout::insert_section_segment_map(Const_section_id secn,
  1115. Unique_segment_info *s)
  1116. {
  1117. gold_assert(this->unique_segment_for_sections_specified_);
  1118. this->section_segment_map_[secn] = s;
  1119. }
  1120. // Handle a relocation section when doing a relocatable link.
  1121. template<int size, bool big_endian>
  1122. Output_section*
  1123. Layout::layout_reloc(Sized_relobj_file<size, big_endian>* object,
  1124. unsigned int,
  1125. const elfcpp::Shdr<size, big_endian>& shdr,
  1126. Output_section* data_section,
  1127. Relocatable_relocs* rr)
  1128. {
  1129. gold_assert(parameters->options().relocatable()
  1130. || parameters->options().emit_relocs());
  1131. int sh_type = shdr.get_sh_type();
  1132. std::string name;
  1133. if (sh_type == elfcpp::SHT_REL)
  1134. name = ".rel";
  1135. else if (sh_type == elfcpp::SHT_RELA)
  1136. name = ".rela";
  1137. else
  1138. gold_unreachable();
  1139. name += data_section->name();
  1140. // In a relocatable link relocs for a grouped section must not be
  1141. // combined with other reloc sections.
  1142. Output_section* os;
  1143. if (!parameters->options().relocatable()
  1144. || (data_section->flags() & elfcpp::SHF_GROUP) == 0)
  1145. os = this->choose_output_section(object, name.c_str(), sh_type,
  1146. shdr.get_sh_flags(), false,
  1147. ORDER_INVALID, false);
  1148. else
  1149. {
  1150. const char* n = this->namepool_.add(name.c_str(), true, NULL);
  1151. os = this->make_output_section(n, sh_type, shdr.get_sh_flags(),
  1152. ORDER_INVALID, false);
  1153. }
  1154. os->set_should_link_to_symtab();
  1155. os->set_info_section(data_section);
  1156. Output_section_data* posd;
  1157. if (sh_type == elfcpp::SHT_REL)
  1158. {
  1159. os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
  1160. posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
  1161. size,
  1162. big_endian>(rr);
  1163. }
  1164. else if (sh_type == elfcpp::SHT_RELA)
  1165. {
  1166. os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
  1167. posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
  1168. size,
  1169. big_endian>(rr);
  1170. }
  1171. else
  1172. gold_unreachable();
  1173. os->add_output_section_data(posd);
  1174. rr->set_output_data(posd);
  1175. return os;
  1176. }
  1177. // Handle a group section when doing a relocatable link.
  1178. template<int size, bool big_endian>
  1179. void
  1180. Layout::layout_group(Symbol_table* symtab,
  1181. Sized_relobj_file<size, big_endian>* object,
  1182. unsigned int,
  1183. const char* group_section_name,
  1184. const char* signature,
  1185. const elfcpp::Shdr<size, big_endian>& shdr,
  1186. elfcpp::Elf_Word flags,
  1187. std::vector<unsigned int>* shndxes)
  1188. {
  1189. gold_assert(parameters->options().relocatable());
  1190. gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
  1191. group_section_name = this->namepool_.add(group_section_name, true, NULL);
  1192. Output_section* os = this->make_output_section(group_section_name,
  1193. elfcpp::SHT_GROUP,
  1194. shdr.get_sh_flags(),
  1195. ORDER_INVALID, false);
  1196. // We need to find a symbol with the signature in the symbol table.
  1197. // If we don't find one now, we need to look again later.
  1198. Symbol* sym = symtab->lookup(signature, NULL);
  1199. if (sym != NULL)
  1200. os->set_info_symndx(sym);
  1201. else
  1202. {
  1203. // Reserve some space to minimize reallocations.
  1204. if (this->group_signatures_.empty())
  1205. this->group_signatures_.reserve(this->number_of_input_files_ * 16);
  1206. // We will wind up using a symbol whose name is the signature.
  1207. // So just put the signature in the symbol name pool to save it.
  1208. signature = symtab->canonicalize_name(signature);
  1209. this->group_signatures_.push_back(Group_signature(os, signature));
  1210. }
  1211. os->set_should_link_to_symtab();
  1212. os->set_entsize(4);
  1213. section_size_type entry_count =
  1214. convert_to_section_size_type(shdr.get_sh_size() / 4);
  1215. Output_section_data* posd =
  1216. new Output_data_group<size, big_endian>(object, entry_count, flags,
  1217. shndxes);
  1218. os->add_output_section_data(posd);
  1219. }
  1220. // Special GNU handling of sections name .eh_frame. They will
  1221. // normally hold exception frame data as defined by the C++ ABI
  1222. // (http://codesourcery.com/cxx-abi/).
  1223. template<int size, bool big_endian>
  1224. Output_section*
  1225. Layout::layout_eh_frame(Sized_relobj_file<size, big_endian>* object,
  1226. const unsigned char* symbols,
  1227. off_t symbols_size,
  1228. const unsigned char* symbol_names,
  1229. off_t symbol_names_size,
  1230. unsigned int shndx,
  1231. const elfcpp::Shdr<size, big_endian>& shdr,
  1232. unsigned int reloc_shndx, unsigned int reloc_type,
  1233. off_t* off)
  1234. {
  1235. gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS
  1236. || shdr.get_sh_type() == elfcpp::SHT_X86_64_UNWIND);
  1237. gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
  1238. Output_section* os = this->make_eh_frame_section(object);
  1239. if (os == NULL)
  1240. return NULL;
  1241. gold_assert(this->eh_frame_section_ == os);
  1242. elfcpp::Elf_Xword orig_flags = os->flags();
  1243. Eh_frame::Eh_frame_section_disposition disp =
  1244. Eh_frame::EH_UNRECOGNIZED_SECTION;
  1245. if (!parameters->incremental())
  1246. {
  1247. disp = this->eh_frame_data_->add_ehframe_input_section(object,
  1248. symbols,
  1249. symbols_size,
  1250. symbol_names,
  1251. symbol_names_size,
  1252. shndx,
  1253. reloc_shndx,
  1254. reloc_type);
  1255. }
  1256. if (disp == Eh_frame::EH_OPTIMIZABLE_SECTION)
  1257. {
  1258. os->update_flags_for_input_section(shdr.get_sh_flags());
  1259. // A writable .eh_frame section is a RELRO section.
  1260. if ((orig_flags & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR))
  1261. != (os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR)))
  1262. {
  1263. os->set_is_relro();
  1264. os->set_order(ORDER_RELRO);
  1265. }
  1266. *off = -1;
  1267. return os;
  1268. }
  1269. if (disp == Eh_frame::EH_END_MARKER_SECTION && !this->added_eh_frame_data_)
  1270. {
  1271. // We found the end marker section, so now we can add the set of
  1272. // optimized sections to the output section. We need to postpone
  1273. // adding this until we've found a section we can optimize so that
  1274. // the .eh_frame section in crtbeginT.o winds up at the start of
  1275. // the output section.
  1276. os->add_output_section_data(this->eh_frame_data_);
  1277. this->added_eh_frame_data_ = true;
  1278. }
  1279. // We couldn't handle this .eh_frame section for some reason.
  1280. // Add it as a normal section.
  1281. bool saw_sections_clause = this->script_options_->saw_sections_clause();
  1282. *off = os->add_input_section(this, object, shndx, ".eh_frame", shdr,
  1283. reloc_shndx, saw_sections_clause);
  1284. this->have_added_input_section_ = true;
  1285. if ((orig_flags & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR))
  1286. != (os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR)))
  1287. os->set_order(this->default_section_order(os, false));
  1288. return os;
  1289. }
  1290. void
  1291. Layout::finalize_eh_frame_section()
  1292. {
  1293. // If we never found an end marker section, we need to add the
  1294. // optimized eh sections to the output section now.
  1295. if (!parameters->incremental()
  1296. && this->eh_frame_section_ != NULL
  1297. && !this->added_eh_frame_data_)
  1298. {
  1299. this->eh_frame_section_->add_output_section_data(this->eh_frame_data_);
  1300. this->added_eh_frame_data_ = true;
  1301. }
  1302. }
  1303. // Create and return the magic .eh_frame section. Create
  1304. // .eh_frame_hdr also if appropriate. OBJECT is the object with the
  1305. // input .eh_frame section; it may be NULL.
  1306. Output_section*
  1307. Layout::make_eh_frame_section(const Relobj* object)
  1308. {
  1309. // FIXME: On x86_64, this could use SHT_X86_64_UNWIND rather than
  1310. // SHT_PROGBITS.
  1311. Output_section* os = this->choose_output_section(object, ".eh_frame",
  1312. elfcpp::SHT_PROGBITS,
  1313. elfcpp::SHF_ALLOC, false,
  1314. ORDER_EHFRAME, false);
  1315. if (os == NULL)
  1316. return NULL;
  1317. if (this->eh_frame_section_ == NULL)
  1318. {
  1319. this->eh_frame_section_ = os;
  1320. this->eh_frame_data_ = new Eh_frame();
  1321. // For incremental linking, we do not optimize .eh_frame sections
  1322. // or create a .eh_frame_hdr section.
  1323. if (parameters->options().eh_frame_hdr() && !parameters->incremental())
  1324. {
  1325. Output_section* hdr_os =
  1326. this->choose_output_section(NULL, ".eh_frame_hdr",
  1327. elfcpp::SHT_PROGBITS,
  1328. elfcpp::SHF_ALLOC, false,
  1329. ORDER_EHFRAME, false);
  1330. if (hdr_os != NULL)
  1331. {
  1332. Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
  1333. this->eh_frame_data_);
  1334. hdr_os->add_output_section_data(hdr_posd);
  1335. hdr_os->set_after_input_sections();
  1336. if (!this->script_options_->saw_phdrs_clause())
  1337. {
  1338. Output_segment* hdr_oseg;
  1339. hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
  1340. elfcpp::PF_R);
  1341. hdr_oseg->add_output_section_to_nonload(hdr_os,
  1342. elfcpp::PF_R);
  1343. }
  1344. this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
  1345. }
  1346. }
  1347. }
  1348. return os;
  1349. }
  1350. // Add an exception frame for a PLT. This is called from target code.
  1351. void
  1352. Layout::add_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
  1353. size_t cie_length, const unsigned char* fde_data,
  1354. size_t fde_length)
  1355. {
  1356. if (parameters->incremental())
  1357. {
  1358. // FIXME: Maybe this could work some day....
  1359. return;
  1360. }
  1361. Output_section* os = this->make_eh_frame_section(NULL);
  1362. if (os == NULL)
  1363. return;
  1364. this->eh_frame_data_->add_ehframe_for_plt(plt, cie_data, cie_length,
  1365. fde_data, fde_length);
  1366. if (!this->added_eh_frame_data_)
  1367. {
  1368. os->add_output_section_data(this->eh_frame_data_);
  1369. this->added_eh_frame_data_ = true;
  1370. }
  1371. }
  1372. // Scan a .debug_info or .debug_types section, and add summary
  1373. // information to the .gdb_index section.
  1374. template<int size, bool big_endian>
  1375. void
  1376. Layout::add_to_gdb_index(bool is_type_unit,
  1377. Sized_relobj<size, big_endian>* object,
  1378. const unsigned char* symbols,
  1379. off_t symbols_size,
  1380. unsigned int shndx,
  1381. unsigned int reloc_shndx,
  1382. unsigned int reloc_type)
  1383. {
  1384. if (this->gdb_index_data_ == NULL)
  1385. {
  1386. Output_section* os = this->choose_output_section(NULL, ".gdb_index",
  1387. elfcpp::SHT_PROGBITS, 0,
  1388. false, ORDER_INVALID,
  1389. false);
  1390. if (os == NULL)
  1391. return;
  1392. this->gdb_index_data_ = new Gdb_index(os);
  1393. os->add_output_section_data(this->gdb_index_data_);
  1394. os->set_after_input_sections();
  1395. }
  1396. this->gdb_index_data_->scan_debug_info(is_type_unit, object, symbols,
  1397. symbols_size, shndx, reloc_shndx,
  1398. reloc_type);
  1399. }
  1400. // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
  1401. // the output section.
  1402. Output_section*
  1403. Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
  1404. elfcpp::Elf_Xword flags,
  1405. Output_section_data* posd,
  1406. Output_section_order order, bool is_relro)
  1407. {
  1408. Output_section* os = this->choose_output_section(NULL, name, type, flags,
  1409. false, order, is_relro);
  1410. if (os != NULL)
  1411. os->add_output_section_data(posd);
  1412. return os;
  1413. }
  1414. // Map section flags to segment flags.
  1415. elfcpp::Elf_Word
  1416. Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
  1417. {
  1418. elfcpp::Elf_Word ret = elfcpp::PF_R;
  1419. if ((flags & elfcpp::SHF_WRITE) != 0)
  1420. ret |= elfcpp::PF_W;
  1421. if ((flags & elfcpp::SHF_EXECINSTR) != 0)
  1422. ret |= elfcpp::PF_X;
  1423. return ret;
  1424. }
  1425. // Make a new Output_section, and attach it to segments as
  1426. // appropriate. ORDER is the order in which this section should
  1427. // appear in the output segment. IS_RELRO is true if this is a relro
  1428. // (read-only after relocations) section.
  1429. Output_section*
  1430. Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
  1431. elfcpp::Elf_Xword flags,
  1432. Output_section_order order, bool is_relro)
  1433. {
  1434. Output_section* os;
  1435. if ((flags & elfcpp::SHF_ALLOC) == 0
  1436. && strcmp(parameters->options().compress_debug_sections(), "none") != 0
  1437. && is_compressible_debug_section(name))
  1438. os = new Output_compressed_section(&parameters->options(), name, type,
  1439. flags);
  1440. else if ((flags & elfcpp::SHF_ALLOC) == 0
  1441. && parameters->options().strip_debug_non_line()
  1442. && strcmp(".debug_abbrev", name) == 0)
  1443. {
  1444. os = this->debug_abbrev_ = new Output_reduced_debug_abbrev_section(
  1445. name, type, flags);
  1446. if (this->debug_info_)
  1447. this->debug_info_->set_abbreviations(this->debug_abbrev_);
  1448. }
  1449. else if ((flags & elfcpp::SHF_ALLOC) == 0
  1450. && parameters->options().strip_debug_non_line()
  1451. && strcmp(".debug_info", name) == 0)
  1452. {
  1453. os = this->debug_info_ = new Output_reduced_debug_info_section(
  1454. name, type, flags);
  1455. if (this->debug_abbrev_)
  1456. this->debug_info_->set_abbreviations(this->debug_abbrev_);
  1457. }
  1458. else
  1459. {
  1460. // Sometimes .init_array*, .preinit_array* and .fini_array* do
  1461. // not have correct section types. Force them here.
  1462. if (type == elfcpp::SHT_PROGBITS)
  1463. {
  1464. if (is_prefix_of(".init_array", name))
  1465. type = elfcpp::SHT_INIT_ARRAY;
  1466. else if (is_prefix_of(".preinit_array", name))
  1467. type = elfcpp::SHT_PREINIT_ARRAY;
  1468. else if (is_prefix_of(".fini_array", name))
  1469. type = elfcpp::SHT_FINI_ARRAY;
  1470. }
  1471. // FIXME: const_cast is ugly.
  1472. Target* target = const_cast<Target*>(&parameters->target());
  1473. os = target->make_output_section(name, type, flags);
  1474. }
  1475. // With -z relro, we have to recognize the special sections by name.
  1476. // There is no other way.
  1477. bool is_relro_local = false;
  1478. if (!this->script_options_->saw_sections_clause()
  1479. && parameters->options().relro()
  1480. && (flags & elfcpp::SHF_ALLOC) != 0
  1481. && (flags & elfcpp::SHF_WRITE) != 0)
  1482. {
  1483. if (type == elfcpp::SHT_PROGBITS)
  1484. {
  1485. if ((flags & elfcpp::SHF_TLS) != 0)
  1486. is_relro = true;
  1487. else if (strcmp(name, ".data.rel.ro") == 0)
  1488. is_relro = true;
  1489. else if (strcmp(name, ".data.rel.ro.local") == 0)
  1490. {
  1491. is_relro = true;
  1492. is_relro_local = true;
  1493. }
  1494. else if (strcmp(name, ".ctors") == 0
  1495. || strcmp(name, ".dtors") == 0
  1496. || strcmp(name, ".jcr") == 0)
  1497. is_relro = true;
  1498. }
  1499. else if (type == elfcpp::SHT_INIT_ARRAY
  1500. || type == elfcpp::SHT_FINI_ARRAY
  1501. || type == elfcpp::SHT_PREINIT_ARRAY)
  1502. is_relro = true;
  1503. }
  1504. if (is_relro)
  1505. os->set_is_relro();
  1506. if (order == ORDER_INVALID && (flags & elfcpp::SHF_ALLOC) != 0)
  1507. order = this->default_section_order(os, is_relro_local);
  1508. os->set_order(order);
  1509. parameters->target().new_output_section(os);
  1510. this->section_list_.push_back(os);
  1511. // The GNU linker by default sorts some sections by priority, so we
  1512. // do the same. We need to know that this might happen before we
  1513. // attach any input sections.
  1514. if (!this->script_options_->saw_sections_clause()
  1515. && !parameters->options().relocatable()
  1516. && (strcmp(name, ".init_array") == 0
  1517. || strcmp(name, ".fini_array") == 0
  1518. || (!parameters->options().ctors_in_init_array()
  1519. && (strcmp(name, ".ctors") == 0
  1520. || strcmp(name, ".dtors") == 0))))
  1521. os->set_may_sort_attached_input_sections();
  1522. // The GNU linker by default sorts .text.{unlikely,exit,startup,hot}
  1523. // sections before other .text sections. We are compatible. We
  1524. // need to know that this might happen before we attach any input
  1525. // sections.
  1526. if (parameters->options().text_reorder()
  1527. && !this->script_options_->saw_sections_clause()
  1528. && !this->is_section_ordering_specified()
  1529. && !parameters->options().relocatable()
  1530. && strcmp(name, ".text") == 0)
  1531. os->set_may_sort_attached_input_sections();
  1532. // GNU linker sorts section by name with --sort-section=name.
  1533. if (strcmp(parameters->options().sort_section(), "name") == 0)
  1534. os->set_must_sort_attached_input_sections();
  1535. // Check for .stab*str sections, as .stab* sections need to link to
  1536. // them.
  1537. if (type == elfcpp::SHT_STRTAB
  1538. && !this->have_stabstr_section_
  1539. && strncmp(name, ".stab", 5) == 0
  1540. && strcmp(name + strlen(name) - 3, "str") == 0)
  1541. this->have_stabstr_section_ = true;
  1542. // During a full incremental link, we add patch space to most
  1543. // PROGBITS and NOBITS sections. Flag those that may be
  1544. // arbitrarily padded.
  1545. if ((type == elfcpp::SHT_PROGBITS || type == elfcpp::SHT_NOBITS)
  1546. && order != ORDER_INTERP
  1547. && order != ORDER_INIT
  1548. && order != ORDER_PLT
  1549. && order != ORDER_FINI
  1550. && order != ORDER_RELRO_LAST
  1551. && order != ORDER_NON_RELRO_FIRST
  1552. && strcmp(name, ".eh_frame") != 0
  1553. && strcmp(name, ".ctors") != 0
  1554. && strcmp(name, ".dtors") != 0
  1555. && strcmp(name, ".jcr") != 0)
  1556. {
  1557. os->set_is_patch_space_allowed();
  1558. // Certain sections require "holes" to be filled with
  1559. // specific fill patterns. These fill patterns may have
  1560. // a minimum size, so we must prevent allocations from the
  1561. // free list that leave a hole smaller than the minimum.
  1562. if (strcmp(name, ".debug_info") == 0)
  1563. os->set_free_space_fill(new Output_fill_debug_info(false));
  1564. else if (strcmp(name, ".debug_types") == 0)
  1565. os->set_free_space_fill(new Output_fill_debug_info(true));
  1566. else if (strcmp(name, ".debug_line") == 0)
  1567. os->set_free_space_fill(new Output_fill_debug_line());
  1568. }
  1569. // If we have already attached the sections to segments, then we
  1570. // need to attach this one now. This happens for sections created
  1571. // directly by the linker.
  1572. if (this->sections_are_attached_)
  1573. this->attach_section_to_segment(&parameters->target(), os);
  1574. return os;
  1575. }
  1576. // Return the default order in which a section should be placed in an
  1577. // output segment. This function captures a lot of the ideas in
  1578. // ld/scripttempl/elf.sc in the GNU linker. Note that the order of a
  1579. // linker created section is normally set when the section is created;
  1580. // this function is used for input sections.
  1581. Output_section_order
  1582. Layout::default_section_order(Output_section* os, bool is_relro_local)
  1583. {
  1584. gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
  1585. bool is_write = (os->flags() & elfcpp::SHF_WRITE) != 0;
  1586. bool is_execinstr = (os->flags() & elfcpp::SHF_EXECINSTR) != 0;
  1587. bool is_bss = false;
  1588. switch (os->type())
  1589. {
  1590. default:
  1591. case elfcpp::SHT_PROGBITS:
  1592. break;
  1593. case elfcpp::SHT_NOBITS:
  1594. is_bss = true;
  1595. break;
  1596. case elfcpp::SHT_RELA:
  1597. case elfcpp::SHT_REL:
  1598. if (!is_write)
  1599. return ORDER_DYNAMIC_RELOCS;
  1600. break;
  1601. case elfcpp::SHT_HASH:
  1602. case elfcpp::SHT_DYNAMIC:
  1603. case elfcpp::SHT_SHLIB:
  1604. case elfcpp::SHT_DYNSYM:
  1605. case elfcpp::SHT_GNU_HASH:
  1606. case elfcpp::SHT_GNU_verdef:
  1607. case elfcpp::SHT_GNU_verneed:
  1608. case elfcpp::SHT_GNU_versym:
  1609. if (!is_write)
  1610. return ORDER_DYNAMIC_LINKER;
  1611. break;
  1612. case elfcpp::SHT_NOTE:
  1613. return is_write ? ORDER_RW_NOTE : ORDER_RO_NOTE;
  1614. }
  1615. if ((os->flags() & elfcpp::SHF_TLS) != 0)
  1616. return is_bss ? ORDER_TLS_BSS : ORDER_TLS_DATA;
  1617. if (!is_bss && !is_write)
  1618. {
  1619. if (is_execinstr)
  1620. {
  1621. if (strcmp(os->name(), ".init") == 0)
  1622. return ORDER_INIT;
  1623. else if (strcmp(os->name(), ".fini") == 0)
  1624. return ORDER_FINI;
  1625. }
  1626. return is_execinstr ? ORDER_TEXT : ORDER_READONLY;
  1627. }
  1628. if (os->is_relro())
  1629. return is_relro_local ? ORDER_RELRO_LOCAL : ORDER_RELRO;
  1630. if (os->is_small_section())
  1631. return is_bss ? ORDER_SMALL_BSS : ORDER_SMALL_DATA;
  1632. if (os->is_large_section())
  1633. return is_bss ? ORDER_LARGE_BSS : ORDER_LARGE_DATA;
  1634. return is_bss ? ORDER_BSS : ORDER_DATA;
  1635. }
  1636. // Attach output sections to segments. This is called after we have
  1637. // seen all the input sections.
  1638. void
  1639. Layout::attach_sections_to_segments(const Target* target)
  1640. {
  1641. for (Section_list::iterator p = this->section_list_.begin();
  1642. p != this->section_list_.end();
  1643. ++p)
  1644. this->attach_section_to_segment(target, *p);
  1645. this->sections_are_attached_ = true;
  1646. }
  1647. // Attach an output section to a segment.
  1648. void
  1649. Layout::attach_section_to_segment(const Target* target, Output_section* os)
  1650. {
  1651. if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
  1652. this->unattached_section_list_.push_back(os);
  1653. else
  1654. this->attach_allocated_section_to_segment(target, os);
  1655. }
  1656. // Attach an allocated output section to a segment.
  1657. void
  1658. Layout::attach_allocated_section_to_segment(const Target* target,
  1659. Output_section* os)
  1660. {
  1661. elfcpp::Elf_Xword flags = os->flags();
  1662. gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
  1663. if (parameters->options().relocatable())
  1664. return;
  1665. // If we have a SECTIONS clause, we can't handle the attachment to
  1666. // segments until after we've seen all the sections.
  1667. if (this->script_options_->saw_sections_clause())
  1668. return;
  1669. gold_assert(!this->script_options_->saw_phdrs_clause());
  1670. // This output section goes into a PT_LOAD segment.
  1671. elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
  1672. // If this output section's segment has extra flags that need to be set,
  1673. // coming from a linker plugin, do that.
  1674. seg_flags |= os->extra_segment_flags();
  1675. // Check for --section-start.
  1676. uint64_t addr;
  1677. bool is_address_set = parameters->options().section_start(os->name(), &addr);
  1678. // In general the only thing we really care about for PT_LOAD
  1679. // segments is whether or not they are writable or executable,
  1680. // so that is how we search for them.
  1681. // Large data sections also go into their own PT_LOAD segment.
  1682. // People who need segments sorted on some other basis will
  1683. // have to use a linker script.
  1684. Segment_list::const_iterator p;
  1685. if (!os->is_unique_segment())
  1686. {
  1687. for (p = this->segment_list_.begin();
  1688. p != this->segment_list_.end();
  1689. ++p)
  1690. {
  1691. if ((*p)->type() != elfcpp::PT_LOAD)
  1692. continue;
  1693. if ((*p)->is_unique_segment())
  1694. continue;
  1695. if (!parameters->options().omagic()
  1696. && ((*p)->flags() & elfcpp::PF_W) != (seg_flags & elfcpp::PF_W))
  1697. continue;
  1698. if ((target->isolate_execinstr() || parameters->options().rosegment())
  1699. && ((*p)->flags() & elfcpp::PF_X) != (seg_flags & elfcpp::PF_X))
  1700. continue;
  1701. // If -Tbss was specified, we need to separate the data and BSS
  1702. // segments.
  1703. if (parameters->options().user_set_Tbss())
  1704. {
  1705. if ((os->type() == elfcpp::SHT_NOBITS)
  1706. == (*p)->has_any_data_sections())
  1707. continue;
  1708. }
  1709. if (os->is_large_data_section() && !(*p)->is_large_data_segment())
  1710. continue;
  1711. if (is_address_set)
  1712. {
  1713. if ((*p)->are_addresses_set())
  1714. continue;
  1715. (*p)->add_initial_output_data(os);
  1716. (*p)->update_flags_for_output_section(seg_flags);
  1717. (*p)->set_addresses(addr, addr);
  1718. break;
  1719. }
  1720. (*p)->add_output_section_to_load(this, os, seg_flags);
  1721. break;
  1722. }
  1723. }
  1724. if (p == this->segment_list_.end()
  1725. || os->is_unique_segment())
  1726. {
  1727. Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
  1728. seg_flags);
  1729. if (os->is_large_data_section())
  1730. oseg->set_is_large_data_segment();
  1731. oseg->add_output_section_to_load(this, os, seg_flags);
  1732. if (is_address_set)
  1733. oseg->set_addresses(addr, addr);
  1734. // Check if segment should be marked unique. For segments marked
  1735. // unique by linker plugins, set the new alignment if specified.
  1736. if (os->is_unique_segment())
  1737. {
  1738. oseg->set_is_unique_segment();
  1739. if (os->segment_alignment() != 0)
  1740. oseg->set_minimum_p_align(os->segment_alignment());
  1741. }
  1742. }
  1743. // If we see a loadable SHT_NOTE section, we create a PT_NOTE
  1744. // segment.
  1745. if (os->type() == elfcpp::SHT_NOTE)
  1746. {
  1747. // See if we already have an equivalent PT_NOTE segment.
  1748. for (p = this->segment_list_.begin();
  1749. p != segment_list_.end();
  1750. ++p)
  1751. {
  1752. if ((*p)->type() == elfcpp::PT_NOTE
  1753. && (((*p)->flags() & elfcpp::PF_W)
  1754. == (seg_flags & elfcpp::PF_W)))
  1755. {
  1756. (*p)->add_output_section_to_nonload(os, seg_flags);
  1757. break;
  1758. }
  1759. }
  1760. if (p == this->segment_list_.end())
  1761. {
  1762. Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
  1763. seg_flags);
  1764. oseg->add_output_section_to_nonload(os, seg_flags);
  1765. }
  1766. }
  1767. // If we see a loadable SHF_TLS section, we create a PT_TLS
  1768. // segment. There can only be one such segment.
  1769. if ((flags & elfcpp::SHF_TLS) != 0)
  1770. {
  1771. if (this->tls_segment_ == NULL)
  1772. this->make_output_segment(elfcpp::PT_TLS, seg_flags);
  1773. this->tls_segment_->add_output_section_to_nonload(os, seg_flags);
  1774. }
  1775. // If -z relro is in effect, and we see a relro section, we create a
  1776. // PT_GNU_RELRO segment. There can only be one such segment.
  1777. if (os->is_relro() && parameters->options().relro())
  1778. {
  1779. gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W));
  1780. if (this->relro_segment_ == NULL)
  1781. this->make_output_segment(elfcpp::PT_GNU_RELRO, seg_flags);
  1782. this->relro_segment_->add_output_section_to_nonload(os, seg_flags);
  1783. }
  1784. // If we see a section named .interp, put it into a PT_INTERP
  1785. // segment. This seems broken to me, but this is what GNU ld does,
  1786. // and glibc expects it.
  1787. if (strcmp(os->name(), ".interp") == 0
  1788. && !this->script_options_->saw_phdrs_clause())
  1789. {
  1790. if (this->interp_segment_ == NULL)
  1791. this->make_output_segment(elfcpp::PT_INTERP, seg_flags);
  1792. else
  1793. gold_warning(_("multiple '.interp' sections in input files "
  1794. "may cause confusing PT_INTERP segment"));
  1795. this->interp_segment_->add_output_section_to_nonload(os, seg_flags);
  1796. }
  1797. }
  1798. // Make an output section for a script.
  1799. Output_section*
  1800. Layout::make_output_section_for_script(
  1801. const char* name,
  1802. Script_sections::Section_type section_type)
  1803. {
  1804. name = this->namepool_.add(name, false, NULL);
  1805. elfcpp::Elf_Xword sh_flags = elfcpp::SHF_ALLOC;
  1806. if (section_type == Script_sections::ST_NOLOAD)
  1807. sh_flags = 0;
  1808. Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
  1809. sh_flags, ORDER_INVALID,
  1810. false);
  1811. os->set_found_in_sections_clause();
  1812. if (section_type == Script_sections::ST_NOLOAD)
  1813. os->set_is_noload();
  1814. return os;
  1815. }
  1816. // Return the number of segments we expect to see.
  1817. size_t
  1818. Layout::expected_segment_count() const
  1819. {
  1820. size_t ret = this->segment_list_.size();
  1821. // If we didn't see a SECTIONS clause in a linker script, we should
  1822. // already have the complete list of segments. Otherwise we ask the
  1823. // SECTIONS clause how many segments it expects, and add in the ones
  1824. // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
  1825. if (!this->script_options_->saw_sections_clause())
  1826. return ret;
  1827. else
  1828. {
  1829. const Script_sections* ss = this->script_options_->script_sections();
  1830. return ret + ss->expected_segment_count(this);
  1831. }
  1832. }
  1833. // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
  1834. // is whether we saw a .note.GNU-stack section in the object file.
  1835. // GNU_STACK_FLAGS is the section flags. The flags give the
  1836. // protection required for stack memory. We record this in an
  1837. // executable as a PT_GNU_STACK segment. If an object file does not
  1838. // have a .note.GNU-stack segment, we must assume that it is an old
  1839. // object. On some targets that will force an executable stack.
  1840. void
  1841. Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
  1842. const Object* obj)
  1843. {
  1844. if (!seen_gnu_stack)
  1845. {
  1846. this->input_without_gnu_stack_note_ = true;
  1847. if (parameters->options().warn_execstack()
  1848. && parameters->target().is_default_stack_executable())
  1849. gold_warning(_("%s: missing .note.GNU-stack section"
  1850. " implies executable stack"),
  1851. obj->name().c_str());
  1852. }
  1853. else
  1854. {
  1855. this->input_with_gnu_stack_note_ = true;
  1856. if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
  1857. {
  1858. this->input_requires_executable_stack_ = true;
  1859. if (parameters->options().warn_execstack())
  1860. gold_warning(_("%s: requires executable stack"),
  1861. obj->name().c_str());
  1862. }
  1863. }
  1864. }
  1865. // Create automatic note sections.
  1866. void
  1867. Layout::create_notes()
  1868. {
  1869. this->create_gold_note();
  1870. this->create_executable_stack_info();
  1871. this->create_build_id();
  1872. }
  1873. // Create the dynamic sections which are needed before we read the
  1874. // relocs.
  1875. void
  1876. Layout::create_initial_dynamic_sections(Symbol_table* symtab)
  1877. {
  1878. if (parameters->doing_static_link())
  1879. return;
  1880. this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
  1881. elfcpp::SHT_DYNAMIC,
  1882. (elfcpp::SHF_ALLOC
  1883. | elfcpp::SHF_WRITE),
  1884. false, ORDER_RELRO,
  1885. true);
  1886. // A linker script may discard .dynamic, so check for NULL.
  1887. if (this->dynamic_section_ != NULL)
  1888. {
  1889. this->dynamic_symbol_ =
  1890. symtab->define_in_output_data("_DYNAMIC", NULL,
  1891. Symbol_table::PREDEFINED,
  1892. this->dynamic_section_, 0, 0,
  1893. elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
  1894. elfcpp::STV_HIDDEN, 0, false, false);
  1895. this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
  1896. this->dynamic_section_->add_output_section_data(this->dynamic_data_);
  1897. }
  1898. }
  1899. // For each output section whose name can be represented as C symbol,
  1900. // define __start and __stop symbols for the section. This is a GNU
  1901. // extension.
  1902. void
  1903. Layout::define_section_symbols(Symbol_table* symtab)
  1904. {
  1905. for (Section_list::const_iterator p = this->section_list_.begin();
  1906. p != this->section_list_.end();
  1907. ++p)
  1908. {
  1909. const char* const name = (*p)->name();
  1910. if (is_cident(name))
  1911. {
  1912. const std::string name_string(name);
  1913. const std::string start_name(cident_section_start_prefix
  1914. + name_string);
  1915. const std::string stop_name(cident_section_stop_prefix
  1916. + name_string);
  1917. symtab->define_in_output_data(start_name.c_str(),
  1918. NULL, // version
  1919. Symbol_table::PREDEFINED,
  1920. *p,
  1921. 0, // value
  1922. 0, // symsize
  1923. elfcpp::STT_NOTYPE,
  1924. elfcpp::STB_GLOBAL,
  1925. elfcpp::STV_DEFAULT,
  1926. 0, // nonvis
  1927. false, // offset_is_from_end
  1928. true); // only_if_ref
  1929. symtab->define_in_output_data(stop_name.c_str(),
  1930. NULL, // version
  1931. Symbol_table::PREDEFINED,
  1932. *p,
  1933. 0, // value
  1934. 0, // symsize
  1935. elfcpp::STT_NOTYPE,
  1936. elfcpp::STB_GLOBAL,
  1937. elfcpp::STV_DEFAULT,
  1938. 0, // nonvis
  1939. true, // offset_is_from_end
  1940. true); // only_if_ref
  1941. }
  1942. }
  1943. }
  1944. // Define symbols for group signatures.
  1945. void
  1946. Layout::define_group_signatures(Symbol_table* symtab)
  1947. {
  1948. for (Group_signatures::iterator p = this->group_signatures_.begin();
  1949. p != this->group_signatures_.end();
  1950. ++p)
  1951. {
  1952. Symbol* sym = symtab->lookup(p->signature, NULL);
  1953. if (sym != NULL)
  1954. p->section->set_info_symndx(sym);
  1955. else
  1956. {
  1957. // Force the name of the group section to the group
  1958. // signature, and use the group's section symbol as the
  1959. // signature symbol.
  1960. if (strcmp(p->section->name(), p->signature) != 0)
  1961. {
  1962. const char* name = this->namepool_.add(p->signature,
  1963. true, NULL);
  1964. p->section->set_name(name);
  1965. }
  1966. p->section->set_needs_symtab_index();
  1967. p->section->set_info_section_symndx(p->section);
  1968. }
  1969. }
  1970. this->group_signatures_.clear();
  1971. }
  1972. // Find the first read-only PT_LOAD segment, creating one if
  1973. // necessary.
  1974. Output_segment*
  1975. Layout::find_first_load_seg(const Target* target)
  1976. {
  1977. Output_segment* best = NULL;
  1978. for (Segment_list::const_iterator p = this->segment_list_.begin();
  1979. p != this->segment_list_.end();
  1980. ++p)
  1981. {
  1982. if ((*p)->type() == elfcpp::PT_LOAD
  1983. && ((*p)->flags() & elfcpp::PF_R) != 0
  1984. && (parameters->options().omagic()
  1985. || ((*p)->flags() & elfcpp::PF_W) == 0)
  1986. && (!target->isolate_execinstr()
  1987. || ((*p)->flags() & elfcpp::PF_X) == 0))
  1988. {
  1989. if (best == NULL || this->segment_precedes(*p, best))
  1990. best = *p;
  1991. }
  1992. }
  1993. if (best != NULL)
  1994. return best;
  1995. gold_assert(!this->script_options_->saw_phdrs_clause());
  1996. Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
  1997. elfcpp::PF_R);
  1998. return load_seg;
  1999. }
  2000. // Save states of all current output segments. Store saved states
  2001. // in SEGMENT_STATES.
  2002. void
  2003. Layout::save_segments(Segment_states* segment_states)
  2004. {
  2005. for (Segment_list::const_iterator p = this->segment_list_.begin();
  2006. p != this->segment_list_.end();
  2007. ++p)
  2008. {
  2009. Output_segment* segment = *p;
  2010. // Shallow copy.
  2011. Output_segment* copy = new Output_segment(*segment);
  2012. (*segment_states)[segment] = copy;
  2013. }
  2014. }
  2015. // Restore states of output segments and delete any segment not found in
  2016. // SEGMENT_STATES.
  2017. void
  2018. Layout::restore_segments(const Segment_states* segment_states)
  2019. {
  2020. // Go through the segment list and remove any segment added in the
  2021. // relaxation loop.
  2022. this->tls_segment_ = NULL;
  2023. this->relro_segment_ = NULL;
  2024. Segment_list::iterator list_iter = this->segment_list_.begin();
  2025. while (list_iter != this->segment_list_.end())
  2026. {
  2027. Output_segment* segment = *list_iter;
  2028. Segment_states::const_iterator states_iter =
  2029. segment_states->find(segment);
  2030. if (states_iter != segment_states->end())
  2031. {
  2032. const Output_segment* copy = states_iter->second;
  2033. // Shallow copy to restore states.
  2034. *segment = *copy;
  2035. // Also fix up TLS and RELRO segment pointers as appropriate.
  2036. if (segment->type() == elfcpp::PT_TLS)
  2037. this->tls_segment_ = segment;
  2038. else if (segment->type() == elfcpp::PT_GNU_RELRO)
  2039. this->relro_segment_ = segment;
  2040. ++list_iter;
  2041. }
  2042. else
  2043. {
  2044. list_iter = this->segment_list_.erase(list_iter);
  2045. // This is a segment created during section layout. It should be
  2046. // safe to remove it since we should have removed all pointers to it.
  2047. delete segment;
  2048. }
  2049. }
  2050. }
  2051. // Clean up after relaxation so that sections can be laid out again.
  2052. void
  2053. Layout::clean_up_after_relaxation()
  2054. {
  2055. // Restore the segments to point state just prior to the relaxation loop.
  2056. Script_sections* script_section = this->script_options_->script_sections();
  2057. script_section->release_segments();
  2058. this->restore_segments(this->segment_states_);
  2059. // Reset section addresses and file offsets
  2060. for (Section_list::iterator p = this->section_list_.begin();
  2061. p != this->section_list_.end();
  2062. ++p)
  2063. {
  2064. (*p)->restore_states();
  2065. // If an input section changes size because of relaxation,
  2066. // we need to adjust the section offsets of all input sections.
  2067. // after such a section.
  2068. if ((*p)->section_offsets_need_adjustment())
  2069. (*p)->adjust_section_offsets();
  2070. (*p)->reset_address_and_file_offset();
  2071. }
  2072. // Reset special output object address and file offsets.
  2073. for (Data_list::iterator p = this->special_output_list_.begin();
  2074. p != this->special_output_list_.end();
  2075. ++p)
  2076. (*p)->reset_address_and_file_offset();
  2077. // A linker script may have created some output section data objects.
  2078. // They are useless now.
  2079. for (Output_section_data_list::const_iterator p =
  2080. this->script_output_section_data_list_.begin();
  2081. p != this->script_output_section_data_list_.end();
  2082. ++p)
  2083. delete *p;
  2084. this->script_output_section_data_list_.clear();
  2085. // Special-case fill output objects are recreated each time through
  2086. // the relaxation loop.
  2087. this->reset_relax_output();
  2088. }
  2089. void
  2090. Layout::reset_relax_output()
  2091. {
  2092. for (Data_list::const_iterator p = this->relax_output_list_.begin();
  2093. p != this->relax_output_list_.end();
  2094. ++p)
  2095. delete *p;
  2096. this->relax_output_list_.clear();
  2097. }
  2098. // Prepare for relaxation.
  2099. void
  2100. Layout::prepare_for_relaxation()
  2101. {
  2102. // Create an relaxation debug check if in debugging mode.
  2103. if (is_debugging_enabled(DEBUG_RELAXATION))
  2104. this->relaxation_debug_check_ = new Relaxation_debug_check();
  2105. // Save segment states.
  2106. this->segment_states_ = new Segment_states();
  2107. this->save_segments(this->segment_states_);
  2108. for(Section_list::const_iterator p = this->section_list_.begin();
  2109. p != this->section_list_.end();
  2110. ++p)
  2111. (*p)->save_states();
  2112. if (is_debugging_enabled(DEBUG_RELAXATION))
  2113. this->relaxation_debug_check_->check_output_data_for_reset_values(
  2114. this->section_list_, this->special_output_list_,
  2115. this->relax_output_list_);
  2116. // Also enable recording of output section data from scripts.
  2117. this->record_output_section_data_from_script_ = true;
  2118. }
  2119. // If the user set the address of the text segment, that may not be
  2120. // compatible with putting the segment headers and file headers into
  2121. // that segment. For isolate_execinstr() targets, it's the rodata
  2122. // segment rather than text where we might put the headers.
  2123. static inline bool
  2124. load_seg_unusable_for_headers(const Target* target)
  2125. {
  2126. const General_options& options = parameters->options();
  2127. if (target->isolate_execinstr())
  2128. return (options.user_set_Trodata_segment()
  2129. && options.Trodata_segment() % target->abi_pagesize() != 0);
  2130. else
  2131. return (options.user_set_Ttext()
  2132. && options.Ttext() % target->abi_pagesize() != 0);
  2133. }
  2134. // Relaxation loop body: If target has no relaxation, this runs only once
  2135. // Otherwise, the target relaxation hook is called at the end of
  2136. // each iteration. If the hook returns true, it means re-layout of
  2137. // section is required.
  2138. //
  2139. // The number of segments created by a linking script without a PHDRS
  2140. // clause may be affected by section sizes and alignments. There is
  2141. // a remote chance that relaxation causes different number of PT_LOAD
  2142. // segments are created and sections are attached to different segments.
  2143. // Therefore, we always throw away all segments created during section
  2144. // layout. In order to be able to restart the section layout, we keep
  2145. // a copy of the segment list right before the relaxation loop and use
  2146. // that to restore the segments.
  2147. //
  2148. // PASS is the current relaxation pass number.
  2149. // SYMTAB is a symbol table.
  2150. // PLOAD_SEG is the address of a pointer for the load segment.
  2151. // PHDR_SEG is a pointer to the PHDR segment.
  2152. // SEGMENT_HEADERS points to the output segment header.
  2153. // FILE_HEADER points to the output file header.
  2154. // PSHNDX is the address to store the output section index.
  2155. off_t inline
  2156. Layout::relaxation_loop_body(
  2157. int pass,
  2158. Target* target,
  2159. Symbol_table* symtab,
  2160. Output_segment** pload_seg,
  2161. Output_segment* phdr_seg,
  2162. Output_segment_headers* segment_headers,
  2163. Output_file_header* file_header,
  2164. unsigned int* pshndx)
  2165. {
  2166. // If this is not the first iteration, we need to clean up after
  2167. // relaxation so that we can lay out the sections again.
  2168. if (pass != 0)
  2169. this->clean_up_after_relaxation();
  2170. // If there is a SECTIONS clause, put all the input sections into
  2171. // the required order.
  2172. Output_segment* load_seg;
  2173. if (this->script_options_->saw_sections_clause())
  2174. load_seg = this->set_section_addresses_from_script(symtab);
  2175. else if (parameters->options().relocatable())
  2176. load_seg = NULL;
  2177. else
  2178. load_seg = this->find_first_load_seg(target);
  2179. if (parameters->options().oformat_enum()
  2180. != General_options::OBJECT_FORMAT_ELF)
  2181. load_seg = NULL;
  2182. if (load_seg_unusable_for_headers(target))
  2183. {
  2184. load_seg = NULL;
  2185. phdr_seg = NULL;
  2186. }
  2187. gold_assert(phdr_seg == NULL
  2188. || load_seg != NULL
  2189. || this->script_options_->saw_sections_clause());
  2190. // If the address of the load segment we found has been set by
  2191. // --section-start rather than by a script, then adjust the VMA and
  2192. // LMA downward if possible to include the file and section headers.
  2193. uint64_t header_gap = 0;
  2194. if (load_seg != NULL
  2195. && load_seg->are_addresses_set()
  2196. && !this->script_options_->saw_sections_clause()
  2197. && !parameters->options().relocatable())
  2198. {
  2199. file_header->finalize_data_size();
  2200. segment_headers->finalize_data_size();
  2201. size_t sizeof_headers = (file_header->data_size()
  2202. + segment_headers->data_size());
  2203. const uint64_t abi_pagesize = target->abi_pagesize();
  2204. uint64_t hdr_paddr = load_seg->paddr() - sizeof_headers;
  2205. hdr_paddr &= ~(abi_pagesize - 1);
  2206. uint64_t subtract = load_seg->paddr() - hdr_paddr;
  2207. if (load_seg->paddr() < subtract || load_seg->vaddr() < subtract)
  2208. load_seg = NULL;
  2209. else
  2210. {
  2211. load_seg->set_addresses(load_seg->vaddr() - subtract,
  2212. load_seg->paddr() - subtract);
  2213. header_gap = subtract - sizeof_headers;
  2214. }
  2215. }
  2216. // Lay out the segment headers.
  2217. if (!parameters->options().relocatable())
  2218. {
  2219. gold_assert(segment_headers != NULL);
  2220. if (header_gap != 0 && load_seg != NULL)
  2221. {
  2222. Output_data_zero_fill* z = new Output_data_zero_fill(header_gap, 1);
  2223. load_seg->add_initial_output_data(z);
  2224. }
  2225. if (load_seg != NULL)
  2226. load_seg->add_initial_output_data(segment_headers);
  2227. if (phdr_seg != NULL)
  2228. phdr_seg->add_initial_output_data(segment_headers);
  2229. }
  2230. // Lay out the file header.
  2231. if (load_seg != NULL)
  2232. load_seg->add_initial_output_data(file_header);
  2233. if (this->script_options_->saw_phdrs_clause()
  2234. && !parameters->options().relocatable())
  2235. {
  2236. // Support use of FILEHDRS and PHDRS attachments in a PHDRS
  2237. // clause in a linker script.
  2238. Script_sections* ss = this->script_options_->script_sections();
  2239. ss->put_headers_in_phdrs(file_header, segment_headers);
  2240. }
  2241. // We set the output section indexes in set_segment_offsets and
  2242. // set_section_indexes.
  2243. *pshndx = 1;
  2244. // Set the file offsets of all the segments, and all the sections
  2245. // they contain.
  2246. off_t off;
  2247. if (!parameters->options().relocatable())
  2248. off = this->set_segment_offsets(target, load_seg, pshndx);
  2249. else
  2250. off = this->set_relocatable_section_offsets(file_header, pshndx);
  2251. // Verify that the dummy relaxation does not change anything.
  2252. if (is_debugging_enabled(DEBUG_RELAXATION))
  2253. {
  2254. if (pass == 0)
  2255. this->relaxation_debug_check_->read_sections(this->section_list_);
  2256. else
  2257. this->relaxation_debug_check_->verify_sections(this->section_list_);
  2258. }
  2259. *pload_seg = load_seg;
  2260. return off;
  2261. }
  2262. // Search the list of patterns and find the postion of the given section
  2263. // name in the output section. If the section name matches a glob
  2264. // pattern and a non-glob name, then the non-glob position takes
  2265. // precedence. Return 0 if no match is found.
  2266. unsigned int
  2267. Layout::find_section_order_index(const std::string& section_name)
  2268. {
  2269. Unordered_map<std::string, unsigned int>::iterator map_it;
  2270. map_it = this->input_section_position_.find(section_name);
  2271. if (map_it != this->input_section_position_.end())
  2272. return map_it->second;
  2273. // Absolute match failed. Linear search the glob patterns.
  2274. std::vector<std::string>::iterator it;
  2275. for (it = this->input_section_glob_.begin();
  2276. it != this->input_section_glob_.end();
  2277. ++it)
  2278. {
  2279. if (fnmatch((*it).c_str(), section_name.c_str(), FNM_NOESCAPE) == 0)
  2280. {
  2281. map_it = this->input_section_position_.find(*it);
  2282. gold_assert(map_it != this->input_section_position_.end());
  2283. return map_it->second;
  2284. }
  2285. }
  2286. return 0;
  2287. }
  2288. // Read the sequence of input sections from the file specified with
  2289. // option --section-ordering-file.
  2290. void
  2291. Layout::read_layout_from_file()
  2292. {
  2293. const char* filename = parameters->options().section_ordering_file();
  2294. std::ifstream in;
  2295. std::string line;
  2296. in.open(filename);
  2297. if (!in)
  2298. gold_fatal(_("unable to open --section-ordering-file file %s: %s"),
  2299. filename, strerror(errno));
  2300. std::getline(in, line); // this chops off the trailing \n, if any
  2301. unsigned int position = 1;
  2302. this->set_section_ordering_specified();
  2303. while (in)
  2304. {
  2305. if (!line.empty() && line[line.length() - 1] == '\r') // Windows
  2306. line.resize(line.length() - 1);
  2307. // Ignore comments, beginning with '#'
  2308. if (line[0] == '#')
  2309. {
  2310. std::getline(in, line);
  2311. continue;
  2312. }
  2313. this->input_section_position_[line] = position;
  2314. // Store all glob patterns in a vector.
  2315. if (is_wildcard_string(line.c_str()))
  2316. this->input_section_glob_.push_back(line);
  2317. position++;
  2318. std::getline(in, line);
  2319. }
  2320. }
  2321. // Finalize the layout. When this is called, we have created all the
  2322. // output sections and all the output segments which are based on
  2323. // input sections. We have several things to do, and we have to do
  2324. // them in the right order, so that we get the right results correctly
  2325. // and efficiently.
  2326. // 1) Finalize the list of output segments and create the segment
  2327. // table header.
  2328. // 2) Finalize the dynamic symbol table and associated sections.
  2329. // 3) Determine the final file offset of all the output segments.
  2330. // 4) Determine the final file offset of all the SHF_ALLOC output
  2331. // sections.
  2332. // 5) Create the symbol table sections and the section name table
  2333. // section.
  2334. // 6) Finalize the symbol table: set symbol values to their final
  2335. // value and make a final determination of which symbols are going
  2336. // into the output symbol table.
  2337. // 7) Create the section table header.
  2338. // 8) Determine the final file offset of all the output sections which
  2339. // are not SHF_ALLOC, including the section table header.
  2340. // 9) Finalize the ELF file header.
  2341. // This function returns the size of the output file.
  2342. off_t
  2343. Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
  2344. Target* target, const Task* task)
  2345. {
  2346. target->finalize_sections(this, input_objects, symtab);
  2347. this->count_local_symbols(task, input_objects);
  2348. this->link_stabs_sections();
  2349. Output_segment* phdr_seg = NULL;
  2350. if (!parameters->options().relocatable() && !parameters->doing_static_link())
  2351. {
  2352. // There was a dynamic object in the link. We need to create
  2353. // some information for the dynamic linker.
  2354. // Create the PT_PHDR segment which will hold the program
  2355. // headers.
  2356. if (!this->script_options_->saw_phdrs_clause())
  2357. phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
  2358. // Create the dynamic symbol table, including the hash table.
  2359. Output_section* dynstr;
  2360. std::vector<Symbol*> dynamic_symbols;
  2361. unsigned int local_dynamic_count;
  2362. Versions versions(*this->script_options()->version_script_info(),
  2363. &this->dynpool_);
  2364. this->create_dynamic_symtab(input_objects, symtab, &dynstr,
  2365. &local_dynamic_count, &dynamic_symbols,
  2366. &versions);
  2367. // Create the .interp section to hold the name of the
  2368. // interpreter, and put it in a PT_INTERP segment. Don't do it
  2369. // if we saw a .interp section in an input file.
  2370. if ((!parameters->options().shared()
  2371. || parameters->options().dynamic_linker() != NULL)
  2372. && this->interp_segment_ == NULL)
  2373. this->create_interp(target);
  2374. // Finish the .dynamic section to hold the dynamic data, and put
  2375. // it in a PT_DYNAMIC segment.
  2376. this->finish_dynamic_section(input_objects, symtab);
  2377. // We should have added everything we need to the dynamic string
  2378. // table.
  2379. this->dynpool_.set_string_offsets();
  2380. // Create the version sections. We can't do this until the
  2381. // dynamic string table is complete.
  2382. this->create_version_sections(&versions, symtab, local_dynamic_count,
  2383. dynamic_symbols, dynstr);
  2384. // Set the size of the _DYNAMIC symbol. We can't do this until
  2385. // after we call create_version_sections.
  2386. this->set_dynamic_symbol_size(symtab);
  2387. }
  2388. // Create segment headers.
  2389. Output_segment_headers* segment_headers =
  2390. (parameters->options().relocatable()
  2391. ? NULL
  2392. : new Output_segment_headers(this->segment_list_));
  2393. // Lay out the file header.
  2394. Output_file_header* file_header = new Output_file_header(target, symtab,
  2395. segment_headers);
  2396. this->special_output_list_.push_back(file_header);
  2397. if (segment_headers != NULL)
  2398. this->special_output_list_.push_back(segment_headers);
  2399. // Find approriate places for orphan output sections if we are using
  2400. // a linker script.
  2401. if (this->script_options_->saw_sections_clause())
  2402. this->place_orphan_sections_in_script();
  2403. Output_segment* load_seg;
  2404. off_t off;
  2405. unsigned int shndx;
  2406. int pass = 0;
  2407. // Take a snapshot of the section layout as needed.
  2408. if (target->may_relax())
  2409. this->prepare_for_relaxation();
  2410. // Run the relaxation loop to lay out sections.
  2411. do
  2412. {
  2413. off = this->relaxation_loop_body(pass, target, symtab, &load_seg,
  2414. phdr_seg, segment_headers, file_header,
  2415. &shndx);
  2416. pass++;
  2417. }
  2418. while (target->may_relax()
  2419. && target->relax(pass, input_objects, symtab, this, task));
  2420. // If there is a load segment that contains the file and program headers,
  2421. // provide a symbol __ehdr_start pointing there.
  2422. // A program can use this to examine itself robustly.
  2423. Symbol *ehdr_start = symtab->lookup("__ehdr_start");
  2424. if (ehdr_start != NULL && ehdr_start->is_predefined())
  2425. {
  2426. if (load_seg != NULL)
  2427. ehdr_start->set_output_segment(load_seg, Symbol::SEGMENT_START);
  2428. else
  2429. ehdr_start->set_undefined();
  2430. }
  2431. // Set the file offsets of all the non-data sections we've seen so
  2432. // far which don't have to wait for the input sections. We need
  2433. // this in order to finalize local symbols in non-allocated
  2434. // sections.
  2435. off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
  2436. // Set the section indexes of all unallocated sections seen so far,
  2437. // in case any of them are somehow referenced by a symbol.
  2438. shndx = this->set_section_indexes(shndx);
  2439. // Create the symbol table sections.
  2440. this->create_symtab_sections(input_objects, symtab, shndx, &off);
  2441. if (!parameters->doing_static_link())
  2442. this->assign_local_dynsym_offsets(input_objects);
  2443. // Process any symbol assignments from a linker script. This must
  2444. // be called after the symbol table has been finalized.
  2445. this->script_options_->finalize_symbols(symtab, this);
  2446. // Create the incremental inputs sections.
  2447. if (this->incremental_inputs_)
  2448. {
  2449. this->incremental_inputs_->finalize();
  2450. this->create_incremental_info_sections(symtab);
  2451. }
  2452. // Create the .shstrtab section.
  2453. Output_section* shstrtab_section = this->create_shstrtab();
  2454. // Set the file offsets of the rest of the non-data sections which
  2455. // don't have to wait for the input sections.
  2456. off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
  2457. // Now that all sections have been created, set the section indexes
  2458. // for any sections which haven't been done yet.
  2459. shndx = this->set_section_indexes(shndx);
  2460. // Create the section table header.
  2461. this->create_shdrs(shstrtab_section, &off);
  2462. // If there are no sections which require postprocessing, we can
  2463. // handle the section names now, and avoid a resize later.
  2464. if (!this->any_postprocessing_sections_)
  2465. {
  2466. off = this->set_section_offsets(off,
  2467. POSTPROCESSING_SECTIONS_PASS);
  2468. off =
  2469. this->set_section_offsets(off,
  2470. STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
  2471. }
  2472. file_header->set_section_info(this->section_headers_, shstrtab_section);
  2473. // Now we know exactly where everything goes in the output file
  2474. // (except for non-allocated sections which require postprocessing).
  2475. Output_data::layout_complete();
  2476. this->output_file_size_ = off;
  2477. return off;
  2478. }
  2479. // Create a note header following the format defined in the ELF ABI.
  2480. // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
  2481. // of the section to create, DESCSZ is the size of the descriptor.
  2482. // ALLOCATE is true if the section should be allocated in memory.
  2483. // This returns the new note section. It sets *TRAILING_PADDING to
  2484. // the number of trailing zero bytes required.
  2485. Output_section*
  2486. Layout::create_note(const char* name, int note_type,
  2487. const char* section_name, size_t descsz,
  2488. bool allocate, size_t* trailing_padding)
  2489. {
  2490. // Authorities all agree that the values in a .note field should
  2491. // be aligned on 4-byte boundaries for 32-bit binaries. However,
  2492. // they differ on what the alignment is for 64-bit binaries.
  2493. // The GABI says unambiguously they take 8-byte alignment:
  2494. // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
  2495. // Other documentation says alignment should always be 4 bytes:
  2496. // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
  2497. // GNU ld and GNU readelf both support the latter (at least as of
  2498. // version 2.16.91), and glibc always generates the latter for
  2499. // .note.ABI-tag (as of version 1.6), so that's the one we go with
  2500. // here.
  2501. #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
  2502. const int size = parameters->target().get_size();
  2503. #else
  2504. const int size = 32;
  2505. #endif
  2506. // The contents of the .note section.
  2507. size_t namesz = strlen(name) + 1;
  2508. size_t aligned_namesz = align_address(namesz, size / 8);
  2509. size_t aligned_descsz = align_address(descsz, size / 8);
  2510. size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
  2511. unsigned char* buffer = new unsigned char[notehdrsz];
  2512. memset(buffer, 0, notehdrsz);
  2513. bool is_big_endian = parameters->target().is_big_endian();
  2514. if (size == 32)
  2515. {
  2516. if (!is_big_endian)
  2517. {
  2518. elfcpp::Swap<32, false>::writeval(buffer, namesz);
  2519. elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
  2520. elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
  2521. }
  2522. else
  2523. {
  2524. elfcpp::Swap<32, true>::writeval(buffer, namesz);
  2525. elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
  2526. elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
  2527. }
  2528. }
  2529. else if (size == 64)
  2530. {
  2531. if (!is_big_endian)
  2532. {
  2533. elfcpp::Swap<64, false>::writeval(buffer, namesz);
  2534. elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
  2535. elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
  2536. }
  2537. else
  2538. {
  2539. elfcpp::Swap<64, true>::writeval(buffer, namesz);
  2540. elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
  2541. elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
  2542. }
  2543. }
  2544. else
  2545. gold_unreachable();
  2546. memcpy(buffer + 3 * (size / 8), name, namesz);
  2547. elfcpp::Elf_Xword flags = 0;
  2548. Output_section_order order = ORDER_INVALID;
  2549. if (allocate)
  2550. {
  2551. flags = elfcpp::SHF_ALLOC;
  2552. order = ORDER_RO_NOTE;
  2553. }
  2554. Output_section* os = this->choose_output_section(NULL, section_name,
  2555. elfcpp::SHT_NOTE,
  2556. flags, false, order, false);
  2557. if (os == NULL)
  2558. return NULL;
  2559. Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
  2560. size / 8,
  2561. "** note header");
  2562. os->add_output_section_data(posd);
  2563. *trailing_padding = aligned_descsz - descsz;
  2564. return os;
  2565. }
  2566. // For an executable or shared library, create a note to record the
  2567. // version of gold used to create the binary.
  2568. void
  2569. Layout::create_gold_note()
  2570. {
  2571. if (parameters->options().relocatable()
  2572. || parameters->incremental_update())
  2573. return;
  2574. std::string desc = std::string("gold ") + gold::get_version_string();
  2575. size_t trailing_padding;
  2576. Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
  2577. ".note.gnu.gold-version", desc.size(),
  2578. false, &trailing_padding);
  2579. if (os == NULL)
  2580. return;
  2581. Output_section_data* posd = new Output_data_const(desc, 4);
  2582. os->add_output_section_data(posd);
  2583. if (trailing_padding > 0)
  2584. {
  2585. posd = new Output_data_zero_fill(trailing_padding, 0);
  2586. os->add_output_section_data(posd);
  2587. }
  2588. }
  2589. // Record whether the stack should be executable. This can be set
  2590. // from the command line using the -z execstack or -z noexecstack
  2591. // options. Otherwise, if any input file has a .note.GNU-stack
  2592. // section with the SHF_EXECINSTR flag set, the stack should be
  2593. // executable. Otherwise, if at least one input file a
  2594. // .note.GNU-stack section, and some input file has no .note.GNU-stack
  2595. // section, we use the target default for whether the stack should be
  2596. // executable. Otherwise, we don't generate a stack note. When
  2597. // generating a object file, we create a .note.GNU-stack section with
  2598. // the appropriate marking. When generating an executable or shared
  2599. // library, we create a PT_GNU_STACK segment.
  2600. void
  2601. Layout::create_executable_stack_info()
  2602. {
  2603. bool is_stack_executable;
  2604. if (parameters->options().is_execstack_set())
  2605. {
  2606. is_stack_executable = parameters->options().is_stack_executable();
  2607. if (!is_stack_executable
  2608. && this->input_requires_executable_stack_
  2609. && parameters->options().warn_execstack())
  2610. gold_warning(_("one or more inputs require executable stack, "
  2611. "but -z noexecstack was given"));
  2612. }
  2613. else if (!this->input_with_gnu_stack_note_)
  2614. return;
  2615. else
  2616. {
  2617. if (this->input_requires_executable_stack_)
  2618. is_stack_executable = true;
  2619. else if (this->input_without_gnu_stack_note_)
  2620. is_stack_executable =
  2621. parameters->target().is_default_stack_executable();
  2622. else
  2623. is_stack_executable = false;
  2624. }
  2625. if (parameters->options().relocatable())
  2626. {
  2627. const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
  2628. elfcpp::Elf_Xword flags = 0;
  2629. if (is_stack_executable)
  2630. flags |= elfcpp::SHF_EXECINSTR;
  2631. this->make_output_section(name, elfcpp::SHT_PROGBITS, flags,
  2632. ORDER_INVALID, false);
  2633. }
  2634. else
  2635. {
  2636. if (this->script_options_->saw_phdrs_clause())
  2637. return;
  2638. int flags = elfcpp::PF_R | elfcpp::PF_W;
  2639. if (is_stack_executable)
  2640. flags |= elfcpp::PF_X;
  2641. this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
  2642. }
  2643. }
  2644. // If --build-id was used, set up the build ID note.
  2645. void
  2646. Layout::create_build_id()
  2647. {
  2648. if (!parameters->options().user_set_build_id())
  2649. return;
  2650. const char* style = parameters->options().build_id();
  2651. if (strcmp(style, "none") == 0)
  2652. return;
  2653. // Set DESCSZ to the size of the note descriptor. When possible,
  2654. // set DESC to the note descriptor contents.
  2655. size_t descsz;
  2656. std::string desc;
  2657. if (strcmp(style, "md5") == 0)
  2658. descsz = 128 / 8;
  2659. else if ((strcmp(style, "sha1") == 0) || (strcmp(style, "tree") == 0))
  2660. descsz = 160 / 8;
  2661. else if (strcmp(style, "uuid") == 0)
  2662. {
  2663. const size_t uuidsz = 128 / 8;
  2664. char buffer[uuidsz];
  2665. memset(buffer, 0, uuidsz);
  2666. int descriptor = open_descriptor(-1, "/dev/urandom", O_RDONLY);
  2667. if (descriptor < 0)
  2668. gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
  2669. strerror(errno));
  2670. else
  2671. {
  2672. ssize_t got = ::read(descriptor, buffer, uuidsz);
  2673. release_descriptor(descriptor, true);
  2674. if (got < 0)
  2675. gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
  2676. else if (static_cast<size_t>(got) != uuidsz)
  2677. gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
  2678. uuidsz, got);
  2679. }
  2680. desc.assign(buffer, uuidsz);
  2681. descsz = uuidsz;
  2682. }
  2683. else if (strncmp(style, "0x", 2) == 0)
  2684. {
  2685. hex_init();
  2686. const char* p = style + 2;
  2687. while (*p != '\0')
  2688. {
  2689. if (hex_p(p[0]) && hex_p(p[1]))
  2690. {
  2691. char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
  2692. desc += c;
  2693. p += 2;
  2694. }
  2695. else if (*p == '-' || *p == ':')
  2696. ++p;
  2697. else
  2698. gold_fatal(_("--build-id argument '%s' not a valid hex number"),
  2699. style);
  2700. }
  2701. descsz = desc.size();
  2702. }
  2703. else
  2704. gold_fatal(_("unrecognized --build-id argument '%s'"), style);
  2705. // Create the note.
  2706. size_t trailing_padding;
  2707. Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
  2708. ".note.gnu.build-id", descsz, true,
  2709. &trailing_padding);
  2710. if (os == NULL)
  2711. return;
  2712. if (!desc.empty())
  2713. {
  2714. // We know the value already, so we fill it in now.
  2715. gold_assert(desc.size() == descsz);
  2716. Output_section_data* posd = new Output_data_const(desc, 4);
  2717. os->add_output_section_data(posd);
  2718. if (trailing_padding != 0)
  2719. {
  2720. posd = new Output_data_zero_fill(trailing_padding, 0);
  2721. os->add_output_section_data(posd);
  2722. }
  2723. }
  2724. else
  2725. {
  2726. // We need to compute a checksum after we have completed the
  2727. // link.
  2728. gold_assert(trailing_padding == 0);
  2729. this->build_id_note_ = new Output_data_zero_fill(descsz, 4);
  2730. os->add_output_section_data(this->build_id_note_);
  2731. }
  2732. }
  2733. // If we have both .stabXX and .stabXXstr sections, then the sh_link
  2734. // field of the former should point to the latter. I'm not sure who
  2735. // started this, but the GNU linker does it, and some tools depend
  2736. // upon it.
  2737. void
  2738. Layout::link_stabs_sections()
  2739. {
  2740. if (!this->have_stabstr_section_)
  2741. return;
  2742. for (Section_list::iterator p = this->section_list_.begin();
  2743. p != this->section_list_.end();
  2744. ++p)
  2745. {
  2746. if ((*p)->type() != elfcpp::SHT_STRTAB)
  2747. continue;
  2748. const char* name = (*p)->name();
  2749. if (strncmp(name, ".stab", 5) != 0)
  2750. continue;
  2751. size_t len = strlen(name);
  2752. if (strcmp(name + len - 3, "str") != 0)
  2753. continue;
  2754. std::string stab_name(name, len - 3);
  2755. Output_section* stab_sec;
  2756. stab_sec = this->find_output_section(stab_name.c_str());
  2757. if (stab_sec != NULL)
  2758. stab_sec->set_link_section(*p);
  2759. }
  2760. }
  2761. // Create .gnu_incremental_inputs and related sections needed
  2762. // for the next run of incremental linking to check what has changed.
  2763. void
  2764. Layout::create_incremental_info_sections(Symbol_table* symtab)
  2765. {
  2766. Incremental_inputs* incr = this->incremental_inputs_;
  2767. gold_assert(incr != NULL);
  2768. // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
  2769. incr->create_data_sections(symtab);
  2770. // Add the .gnu_incremental_inputs section.
  2771. const char* incremental_inputs_name =
  2772. this->namepool_.add(".gnu_incremental_inputs", false, NULL);
  2773. Output_section* incremental_inputs_os =
  2774. this->make_output_section(incremental_inputs_name,
  2775. elfcpp::SHT_GNU_INCREMENTAL_INPUTS, 0,
  2776. ORDER_INVALID, false);
  2777. incremental_inputs_os->add_output_section_data(incr->inputs_section());
  2778. // Add the .gnu_incremental_symtab section.
  2779. const char* incremental_symtab_name =
  2780. this->namepool_.add(".gnu_incremental_symtab", false, NULL);
  2781. Output_section* incremental_symtab_os =
  2782. this->make_output_section(incremental_symtab_name,
  2783. elfcpp::SHT_GNU_INCREMENTAL_SYMTAB, 0,
  2784. ORDER_INVALID, false);
  2785. incremental_symtab_os->add_output_section_data(incr->symtab_section());
  2786. incremental_symtab_os->set_entsize(4);
  2787. // Add the .gnu_incremental_relocs section.
  2788. const char* incremental_relocs_name =
  2789. this->namepool_.add(".gnu_incremental_relocs", false, NULL);
  2790. Output_section* incremental_relocs_os =
  2791. this->make_output_section(incremental_relocs_name,
  2792. elfcpp::SHT_GNU_INCREMENTAL_RELOCS, 0,
  2793. ORDER_INVALID, false);
  2794. incremental_relocs_os->add_output_section_data(incr->relocs_section());
  2795. incremental_relocs_os->set_entsize(incr->relocs_entsize());
  2796. // Add the .gnu_incremental_got_plt section.
  2797. const char* incremental_got_plt_name =
  2798. this->namepool_.add(".gnu_incremental_got_plt", false, NULL);
  2799. Output_section* incremental_got_plt_os =
  2800. this->make_output_section(incremental_got_plt_name,
  2801. elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT, 0,
  2802. ORDER_INVALID, false);
  2803. incremental_got_plt_os->add_output_section_data(incr->got_plt_section());
  2804. // Add the .gnu_incremental_strtab section.
  2805. const char* incremental_strtab_name =
  2806. this->namepool_.add(".gnu_incremental_strtab", false, NULL);
  2807. Output_section* incremental_strtab_os = this->make_output_section(incremental_strtab_name,
  2808. elfcpp::SHT_STRTAB, 0,
  2809. ORDER_INVALID, false);
  2810. Output_data_strtab* strtab_data =
  2811. new Output_data_strtab(incr->get_stringpool());
  2812. incremental_strtab_os->add_output_section_data(strtab_data);
  2813. incremental_inputs_os->set_after_input_sections();
  2814. incremental_symtab_os->set_after_input_sections();
  2815. incremental_relocs_os->set_after_input_sections();
  2816. incremental_got_plt_os->set_after_input_sections();
  2817. incremental_inputs_os->set_link_section(incremental_strtab_os);
  2818. incremental_symtab_os->set_link_section(incremental_inputs_os);
  2819. incremental_relocs_os->set_link_section(incremental_inputs_os);
  2820. incremental_got_plt_os->set_link_section(incremental_inputs_os);
  2821. }
  2822. // Return whether SEG1 should be before SEG2 in the output file. This
  2823. // is based entirely on the segment type and flags. When this is
  2824. // called the segment addresses have normally not yet been set.
  2825. bool
  2826. Layout::segment_precedes(const Output_segment* seg1,
  2827. const Output_segment* seg2)
  2828. {
  2829. elfcpp::Elf_Word type1 = seg1->type();
  2830. elfcpp::Elf_Word type2 = seg2->type();
  2831. // The single PT_PHDR segment is required to precede any loadable
  2832. // segment. We simply make it always first.
  2833. if (type1 == elfcpp::PT_PHDR)
  2834. {
  2835. gold_assert(type2 != elfcpp::PT_PHDR);
  2836. return true;
  2837. }
  2838. if (type2 == elfcpp::PT_PHDR)
  2839. return false;
  2840. // The single PT_INTERP segment is required to precede any loadable
  2841. // segment. We simply make it always second.
  2842. if (type1 == elfcpp::PT_INTERP)
  2843. {
  2844. gold_assert(type2 != elfcpp::PT_INTERP);
  2845. return true;
  2846. }
  2847. if (type2 == elfcpp::PT_INTERP)
  2848. return false;
  2849. // We then put PT_LOAD segments before any other segments.
  2850. if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
  2851. return true;
  2852. if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
  2853. return false;
  2854. // We put the PT_TLS segment last except for the PT_GNU_RELRO
  2855. // segment, because that is where the dynamic linker expects to find
  2856. // it (this is just for efficiency; other positions would also work
  2857. // correctly).
  2858. if (type1 == elfcpp::PT_TLS
  2859. && type2 != elfcpp::PT_TLS
  2860. && type2 != elfcpp::PT_GNU_RELRO)
  2861. return false;
  2862. if (type2 == elfcpp::PT_TLS
  2863. && type1 != elfcpp::PT_TLS
  2864. && type1 != elfcpp::PT_GNU_RELRO)
  2865. return true;
  2866. // We put the PT_GNU_RELRO segment last, because that is where the
  2867. // dynamic linker expects to find it (as with PT_TLS, this is just
  2868. // for efficiency).
  2869. if (type1 == elfcpp::PT_GNU_RELRO && type2 != elfcpp::PT_GNU_RELRO)
  2870. return false;
  2871. if (type2 == elfcpp::PT_GNU_RELRO && type1 != elfcpp::PT_GNU_RELRO)
  2872. return true;
  2873. const elfcpp::Elf_Word flags1 = seg1->flags();
  2874. const elfcpp::Elf_Word flags2 = seg2->flags();
  2875. // The order of non-PT_LOAD segments is unimportant. We simply sort
  2876. // by the numeric segment type and flags values. There should not
  2877. // be more than one segment with the same type and flags, except
  2878. // when a linker script specifies such.
  2879. if (type1 != elfcpp::PT_LOAD)
  2880. {
  2881. if (type1 != type2)
  2882. return type1 < type2;
  2883. gold_assert(flags1 != flags2
  2884. || this->script_options_->saw_phdrs_clause());
  2885. return flags1 < flags2;
  2886. }
  2887. // If the addresses are set already, sort by load address.
  2888. if (seg1->are_addresses_set())
  2889. {
  2890. if (!seg2->are_addresses_set())
  2891. return true;
  2892. unsigned int section_count1 = seg1->output_section_count();
  2893. unsigned int section_count2 = seg2->output_section_count();
  2894. if (section_count1 == 0 && section_count2 > 0)
  2895. return true;
  2896. if (section_count1 > 0 && section_count2 == 0)
  2897. return false;
  2898. uint64_t paddr1 = (seg1->are_addresses_set()
  2899. ? seg1->paddr()
  2900. : seg1->first_section_load_address());
  2901. uint64_t paddr2 = (seg2->are_addresses_set()
  2902. ? seg2->paddr()
  2903. : seg2->first_section_load_address());
  2904. if (paddr1 != paddr2)
  2905. return paddr1 < paddr2;
  2906. }
  2907. else if (seg2->are_addresses_set())
  2908. return false;
  2909. // A segment which holds large data comes after a segment which does
  2910. // not hold large data.
  2911. if (seg1->is_large_data_segment())
  2912. {
  2913. if (!seg2->is_large_data_segment())
  2914. return false;
  2915. }
  2916. else if (seg2->is_large_data_segment())
  2917. return true;
  2918. // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
  2919. // segments come before writable segments. Then writable segments
  2920. // with data come before writable segments without data. Then
  2921. // executable segments come before non-executable segments. Then
  2922. // the unlikely case of a non-readable segment comes before the
  2923. // normal case of a readable segment. If there are multiple
  2924. // segments with the same type and flags, we require that the
  2925. // address be set, and we sort by virtual address and then physical
  2926. // address.
  2927. if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
  2928. return (flags1 & elfcpp::PF_W) == 0;
  2929. if ((flags1 & elfcpp::PF_W) != 0
  2930. && seg1->has_any_data_sections() != seg2->has_any_data_sections())
  2931. return seg1->has_any_data_sections();
  2932. if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
  2933. return (flags1 & elfcpp::PF_X) != 0;
  2934. if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
  2935. return (flags1 & elfcpp::PF_R) == 0;
  2936. // We shouldn't get here--we shouldn't create segments which we
  2937. // can't distinguish. Unless of course we are using a weird linker
  2938. // script or overlapping --section-start options. We could also get
  2939. // here if plugins want unique segments for subsets of sections.
  2940. gold_assert(this->script_options_->saw_phdrs_clause()
  2941. || parameters->options().any_section_start()
  2942. || this->is_unique_segment_for_sections_specified());
  2943. return false;
  2944. }
  2945. // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
  2946. static off_t
  2947. align_file_offset(off_t off, uint64_t addr, uint64_t abi_pagesize)
  2948. {
  2949. uint64_t unsigned_off = off;
  2950. uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
  2951. | (addr & (abi_pagesize - 1)));
  2952. if (aligned_off < unsigned_off)
  2953. aligned_off += abi_pagesize;
  2954. return aligned_off;
  2955. }
  2956. // On targets where the text segment contains only executable code,
  2957. // a non-executable segment is never the text segment.
  2958. static inline bool
  2959. is_text_segment(const Target* target, const Output_segment* seg)
  2960. {
  2961. elfcpp::Elf_Xword flags = seg->flags();
  2962. if ((flags & elfcpp::PF_W) != 0)
  2963. return false;
  2964. if ((flags & elfcpp::PF_X) == 0)
  2965. return !target->isolate_execinstr();
  2966. return true;
  2967. }
  2968. // Set the file offsets of all the segments, and all the sections they
  2969. // contain. They have all been created. LOAD_SEG must be be laid out
  2970. // first. Return the offset of the data to follow.
  2971. off_t
  2972. Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
  2973. unsigned int* pshndx)
  2974. {
  2975. // Sort them into the final order. We use a stable sort so that we
  2976. // don't randomize the order of indistinguishable segments created
  2977. // by linker scripts.
  2978. std::stable_sort(this->segment_list_.begin(), this->segment_list_.end(),
  2979. Layout::Compare_segments(this));
  2980. // Find the PT_LOAD segments, and set their addresses and offsets
  2981. // and their section's addresses and offsets.
  2982. uint64_t start_addr;
  2983. if (parameters->options().user_set_Ttext())
  2984. start_addr = parameters->options().Ttext();
  2985. else if (parameters->options().output_is_position_independent())
  2986. start_addr = 0;
  2987. else
  2988. start_addr = target->default_text_segment_address();
  2989. uint64_t addr = start_addr;
  2990. off_t off = 0;
  2991. // If LOAD_SEG is NULL, then the file header and segment headers
  2992. // will not be loadable. But they still need to be at offset 0 in
  2993. // the file. Set their offsets now.
  2994. if (load_seg == NULL)
  2995. {
  2996. for (Data_list::iterator p = this->special_output_list_.begin();
  2997. p != this->special_output_list_.end();
  2998. ++p)
  2999. {
  3000. off = align_address(off, (*p)->addralign());
  3001. (*p)->set_address_and_file_offset(0, off);
  3002. off += (*p)->data_size();
  3003. }
  3004. }
  3005. unsigned int increase_relro = this->increase_relro_;
  3006. if (this->script_options_->saw_sections_clause())
  3007. increase_relro = 0;
  3008. const bool check_sections = parameters->options().check_sections();
  3009. Output_segment* last_load_segment = NULL;
  3010. unsigned int shndx_begin = *pshndx;
  3011. unsigned int shndx_load_seg = *pshndx;
  3012. for (Segment_list::iterator p = this->segment_list_.begin();
  3013. p != this->segment_list_.end();
  3014. ++p)
  3015. {
  3016. if ((*p)->type() == elfcpp::PT_LOAD)
  3017. {
  3018. if (target->isolate_execinstr())
  3019. {
  3020. // When we hit the segment that should contain the
  3021. // file headers, reset the file offset so we place
  3022. // it and subsequent segments appropriately.
  3023. // We'll fix up the preceding segments below.
  3024. if (load_seg == *p)
  3025. {
  3026. if (off == 0)
  3027. load_seg = NULL;
  3028. else
  3029. {
  3030. off = 0;
  3031. shndx_load_seg = *pshndx;
  3032. }
  3033. }
  3034. }
  3035. else
  3036. {
  3037. // Verify that the file headers fall into the first segment.
  3038. if (load_seg != NULL && load_seg != *p)
  3039. gold_unreachable();
  3040. load_seg = NULL;
  3041. }
  3042. bool are_addresses_set = (*p)->are_addresses_set();
  3043. if (are_addresses_set)
  3044. {
  3045. // When it comes to setting file offsets, we care about
  3046. // the physical address.
  3047. addr = (*p)->paddr();
  3048. }
  3049. else if (parameters->options().user_set_Ttext()
  3050. && (parameters->options().omagic()
  3051. || is_text_segment(target, *p)))
  3052. {
  3053. are_addresses_set = true;
  3054. }
  3055. else if (parameters->options().user_set_Trodata_segment()
  3056. && ((*p)->flags() & (elfcpp::PF_W | elfcpp::PF_X)) == 0)
  3057. {
  3058. addr = parameters->options().Trodata_segment();
  3059. are_addresses_set = true;
  3060. }
  3061. else if (parameters->options().user_set_Tdata()
  3062. && ((*p)->flags() & elfcpp::PF_W) != 0
  3063. && (!parameters->options().user_set_Tbss()
  3064. || (*p)->has_any_data_sections()))
  3065. {
  3066. addr = parameters->options().Tdata();
  3067. are_addresses_set = true;
  3068. }
  3069. else if (parameters->options().user_set_Tbss()
  3070. && ((*p)->flags() & elfcpp::PF_W) != 0
  3071. && !(*p)->has_any_data_sections())
  3072. {
  3073. addr = parameters->options().Tbss();
  3074. are_addresses_set = true;
  3075. }
  3076. uint64_t orig_addr = addr;
  3077. uint64_t orig_off = off;
  3078. uint64_t aligned_addr = 0;
  3079. uint64_t abi_pagesize = target->abi_pagesize();
  3080. uint64_t common_pagesize = target->common_pagesize();
  3081. if (!parameters->options().nmagic()
  3082. && !parameters->options().omagic())
  3083. (*p)->set_minimum_p_align(abi_pagesize);
  3084. if (!are_addresses_set)
  3085. {
  3086. // Skip the address forward one page, maintaining the same
  3087. // position within the page. This lets us store both segments
  3088. // overlapping on a single page in the file, but the loader will
  3089. // put them on different pages in memory. We will revisit this
  3090. // decision once we know the size of the segment.
  3091. uint64_t max_align = (*p)->maximum_alignment();
  3092. if (max_align > abi_pagesize)
  3093. addr = align_address(addr, max_align);
  3094. aligned_addr = addr;
  3095. if (load_seg == *p)
  3096. {
  3097. // This is the segment that will contain the file
  3098. // headers, so its offset will have to be exactly zero.
  3099. gold_assert(orig_off == 0);
  3100. // If the target wants a fixed minimum distance from the
  3101. // text segment to the read-only segment, move up now.
  3102. uint64_t min_addr =
  3103. start_addr + (parameters->options().user_set_rosegment_gap()
  3104. ? parameters->options().rosegment_gap()
  3105. : target->rosegment_gap());
  3106. if (addr < min_addr)
  3107. addr = min_addr;
  3108. // But this is not the first segment! To make its
  3109. // address congruent with its offset, that address better
  3110. // be aligned to the ABI-mandated page size.
  3111. addr = align_address(addr, abi_pagesize);
  3112. aligned_addr = addr;
  3113. }
  3114. else
  3115. {
  3116. if ((addr & (abi_pagesize - 1)) != 0)
  3117. addr = addr + abi_pagesize;
  3118. off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
  3119. }
  3120. }
  3121. if (!parameters->options().nmagic()
  3122. && !parameters->options().omagic())
  3123. {
  3124. // Here we are also taking care of the case when
  3125. // the maximum segment alignment is larger than the page size.
  3126. off = align_file_offset(off, addr,
  3127. std::max(abi_pagesize,
  3128. (*p)->maximum_alignment()));
  3129. }
  3130. else
  3131. {
  3132. // This is -N or -n with a section script which prevents
  3133. // us from using a load segment. We need to ensure that
  3134. // the file offset is aligned to the alignment of the
  3135. // segment. This is because the linker script
  3136. // implicitly assumed a zero offset. If we don't align
  3137. // here, then the alignment of the sections in the
  3138. // linker script may not match the alignment of the
  3139. // sections in the set_section_addresses call below,
  3140. // causing an error about dot moving backward.
  3141. off = align_address(off, (*p)->maximum_alignment());
  3142. }
  3143. unsigned int shndx_hold = *pshndx;
  3144. bool has_relro = false;
  3145. uint64_t new_addr = (*p)->set_section_addresses(target, this,
  3146. false, addr,
  3147. &increase_relro,
  3148. &has_relro,
  3149. &off, pshndx);
  3150. // Now that we know the size of this segment, we may be able
  3151. // to save a page in memory, at the cost of wasting some
  3152. // file space, by instead aligning to the start of a new
  3153. // page. Here we use the real machine page size rather than
  3154. // the ABI mandated page size. If the segment has been
  3155. // aligned so that the relro data ends at a page boundary,
  3156. // we do not try to realign it.
  3157. if (!are_addresses_set
  3158. && !has_relro
  3159. && aligned_addr != addr
  3160. && !parameters->incremental())
  3161. {
  3162. uint64_t first_off = (common_pagesize
  3163. - (aligned_addr
  3164. & (common_pagesize - 1)));
  3165. uint64_t last_off = new_addr & (common_pagesize - 1);
  3166. if (first_off > 0
  3167. && last_off > 0
  3168. && ((aligned_addr & ~ (common_pagesize - 1))
  3169. != (new_addr & ~ (common_pagesize - 1)))
  3170. && first_off + last_off <= common_pagesize)
  3171. {
  3172. *pshndx = shndx_hold;
  3173. addr = align_address(aligned_addr, common_pagesize);
  3174. addr = align_address(addr, (*p)->maximum_alignment());
  3175. if ((addr & (abi_pagesize - 1)) != 0)
  3176. addr = addr + abi_pagesize;
  3177. off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
  3178. off = align_file_offset(off, addr, abi_pagesize);
  3179. increase_relro = this->increase_relro_;
  3180. if (this->script_options_->saw_sections_clause())
  3181. increase_relro = 0;
  3182. has_relro = false;
  3183. new_addr = (*p)->set_section_addresses(target, this,
  3184. true, addr,
  3185. &increase_relro,
  3186. &has_relro,
  3187. &off, pshndx);
  3188. }
  3189. }
  3190. addr = new_addr;
  3191. // Implement --check-sections. We know that the segments
  3192. // are sorted by LMA.
  3193. if (check_sections && last_load_segment != NULL)
  3194. {
  3195. gold_assert(last_load_segment->paddr() <= (*p)->paddr());
  3196. if (last_load_segment->paddr() + last_load_segment->memsz()
  3197. > (*p)->paddr())
  3198. {
  3199. unsigned long long lb1 = last_load_segment->paddr();
  3200. unsigned long long le1 = lb1 + last_load_segment->memsz();
  3201. unsigned long long lb2 = (*p)->paddr();
  3202. unsigned long long le2 = lb2 + (*p)->memsz();
  3203. gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
  3204. "[0x%llx -> 0x%llx]"),
  3205. lb1, le1, lb2, le2);
  3206. }
  3207. }
  3208. last_load_segment = *p;
  3209. }
  3210. }
  3211. if (load_seg != NULL && target->isolate_execinstr())
  3212. {
  3213. // Process the early segments again, setting their file offsets
  3214. // so they land after the segments starting at LOAD_SEG.
  3215. off = align_file_offset(off, 0, target->abi_pagesize());
  3216. this->reset_relax_output();
  3217. for (Segment_list::iterator p = this->segment_list_.begin();
  3218. *p != load_seg;
  3219. ++p)
  3220. {
  3221. if ((*p)->type() == elfcpp::PT_LOAD)
  3222. {
  3223. // We repeat the whole job of assigning addresses and
  3224. // offsets, but we really only want to change the offsets and
  3225. // must ensure that the addresses all come out the same as
  3226. // they did the first time through.
  3227. bool has_relro = false;
  3228. const uint64_t old_addr = (*p)->vaddr();
  3229. const uint64_t old_end = old_addr + (*p)->memsz();
  3230. uint64_t new_addr = (*p)->set_section_addresses(target, this,
  3231. true, old_addr,
  3232. &increase_relro,
  3233. &has_relro,
  3234. &off,
  3235. &shndx_begin);
  3236. gold_assert(new_addr == old_end);
  3237. }
  3238. }
  3239. gold_assert(shndx_begin == shndx_load_seg);
  3240. }
  3241. // Handle the non-PT_LOAD segments, setting their offsets from their
  3242. // section's offsets.
  3243. for (Segment_list::iterator p = this->segment_list_.begin();
  3244. p != this->segment_list_.end();
  3245. ++p)
  3246. {
  3247. if ((*p)->type() != elfcpp::PT_LOAD)
  3248. (*p)->set_offset((*p)->type() == elfcpp::PT_GNU_RELRO
  3249. ? increase_relro
  3250. : 0);
  3251. }
  3252. // Set the TLS offsets for each section in the PT_TLS segment.
  3253. if (this->tls_segment_ != NULL)
  3254. this->tls_segment_->set_tls_offsets();
  3255. return off;
  3256. }
  3257. // Set the offsets of all the allocated sections when doing a
  3258. // relocatable link. This does the same jobs as set_segment_offsets,
  3259. // only for a relocatable link.
  3260. off_t
  3261. Layout::set_relocatable_section_offsets(Output_data* file_header,
  3262. unsigned int* pshndx)
  3263. {
  3264. off_t off = 0;
  3265. file_header->set_address_and_file_offset(0, 0);
  3266. off += file_header->data_size();
  3267. for (Section_list::iterator p = this->section_list_.begin();
  3268. p != this->section_list_.end();
  3269. ++p)
  3270. {
  3271. // We skip unallocated sections here, except that group sections
  3272. // have to come first.
  3273. if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
  3274. && (*p)->type() != elfcpp::SHT_GROUP)
  3275. continue;
  3276. off = align_address(off, (*p)->addralign());
  3277. // The linker script might have set the address.
  3278. if (!(*p)->is_address_valid())
  3279. (*p)->set_address(0);
  3280. (*p)->set_file_offset(off);
  3281. (*p)->finalize_data_size();
  3282. if ((*p)->type() != elfcpp::SHT_NOBITS)
  3283. off += (*p)->data_size();
  3284. (*p)->set_out_shndx(*pshndx);
  3285. ++*pshndx;
  3286. }
  3287. return off;
  3288. }
  3289. // Set the file offset of all the sections not associated with a
  3290. // segment.
  3291. off_t
  3292. Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
  3293. {
  3294. off_t startoff = off;
  3295. off_t maxoff = off;
  3296. for (Section_list::iterator p = this->unattached_section_list_.begin();
  3297. p != this->unattached_section_list_.end();
  3298. ++p)
  3299. {
  3300. // The symtab section is handled in create_symtab_sections.
  3301. if (*p == this->symtab_section_)
  3302. continue;
  3303. // If we've already set the data size, don't set it again.
  3304. if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
  3305. continue;
  3306. if (pass == BEFORE_INPUT_SECTIONS_PASS
  3307. && (*p)->requires_postprocessing())
  3308. {
  3309. (*p)->create_postprocessing_buffer();
  3310. this->any_postprocessing_sections_ = true;
  3311. }
  3312. if (pass == BEFORE_INPUT_SECTIONS_PASS
  3313. && (*p)->after_input_sections())
  3314. continue;
  3315. else if (pass == POSTPROCESSING_SECTIONS_PASS
  3316. && (!(*p)->after_input_sections()
  3317. || (*p)->type() == elfcpp::SHT_STRTAB))
  3318. continue;
  3319. else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
  3320. && (!(*p)->after_input_sections()
  3321. || (*p)->type() != elfcpp::SHT_STRTAB))
  3322. continue;
  3323. if (!parameters->incremental_update())
  3324. {
  3325. off = align_address(off, (*p)->addralign());
  3326. (*p)->set_file_offset(off);
  3327. (*p)->finalize_data_size();
  3328. }
  3329. else
  3330. {
  3331. // Incremental update: allocate file space from free list.
  3332. (*p)->pre_finalize_data_size();
  3333. off_t current_size = (*p)->current_data_size();
  3334. off = this->allocate(current_size, (*p)->addralign(), startoff);
  3335. if (off == -1)
  3336. {
  3337. if (is_debugging_enabled(DEBUG_INCREMENTAL))
  3338. this->free_list_.dump();
  3339. gold_assert((*p)->output_section() != NULL);
  3340. gold_fallback(_("out of patch space for section %s; "
  3341. "relink with --incremental-full"),
  3342. (*p)->output_section()->name());
  3343. }
  3344. (*p)->set_file_offset(off);
  3345. (*p)->finalize_data_size();
  3346. if ((*p)->data_size() > current_size)
  3347. {
  3348. gold_assert((*p)->output_section() != NULL);
  3349. gold_fallback(_("%s: section changed size; "
  3350. "relink with --incremental-full"),
  3351. (*p)->output_section()->name());
  3352. }
  3353. gold_debug(DEBUG_INCREMENTAL,
  3354. "set_section_offsets: %08lx %08lx %s",
  3355. static_cast<long>(off),
  3356. static_cast<long>((*p)->data_size()),
  3357. ((*p)->output_section() != NULL
  3358. ? (*p)->output_section()->name() : "(special)"));
  3359. }
  3360. off += (*p)->data_size();
  3361. if (off > maxoff)
  3362. maxoff = off;
  3363. // At this point the name must be set.
  3364. if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
  3365. this->namepool_.add((*p)->name(), false, NULL);
  3366. }
  3367. return maxoff;
  3368. }
  3369. // Set the section indexes of all the sections not associated with a
  3370. // segment.
  3371. unsigned int
  3372. Layout::set_section_indexes(unsigned int shndx)
  3373. {
  3374. for (Section_list::iterator p = this->unattached_section_list_.begin();
  3375. p != this->unattached_section_list_.end();
  3376. ++p)
  3377. {
  3378. if (!(*p)->has_out_shndx())
  3379. {
  3380. (*p)->set_out_shndx(shndx);
  3381. ++shndx;
  3382. }
  3383. }
  3384. return shndx;
  3385. }
  3386. // Set the section addresses according to the linker script. This is
  3387. // only called when we see a SECTIONS clause. This returns the
  3388. // program segment which should hold the file header and segment
  3389. // headers, if any. It will return NULL if they should not be in a
  3390. // segment.
  3391. Output_segment*
  3392. Layout::set_section_addresses_from_script(Symbol_table* symtab)
  3393. {
  3394. Script_sections* ss = this->script_options_->script_sections();
  3395. gold_assert(ss->saw_sections_clause());
  3396. return this->script_options_->set_section_addresses(symtab, this);
  3397. }
  3398. // Place the orphan sections in the linker script.
  3399. void
  3400. Layout::place_orphan_sections_in_script()
  3401. {
  3402. Script_sections* ss = this->script_options_->script_sections();
  3403. gold_assert(ss->saw_sections_clause());
  3404. // Place each orphaned output section in the script.
  3405. for (Section_list::iterator p = this->section_list_.begin();
  3406. p != this->section_list_.end();
  3407. ++p)
  3408. {
  3409. if (!(*p)->found_in_sections_clause())
  3410. ss->place_orphan(*p);
  3411. }
  3412. }
  3413. // Count the local symbols in the regular symbol table and the dynamic
  3414. // symbol table, and build the respective string pools.
  3415. void
  3416. Layout::count_local_symbols(const Task* task,
  3417. const Input_objects* input_objects)
  3418. {
  3419. // First, figure out an upper bound on the number of symbols we'll
  3420. // be inserting into each pool. This helps us create the pools with
  3421. // the right size, to avoid unnecessary hashtable resizing.
  3422. unsigned int symbol_count = 0;
  3423. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  3424. p != input_objects->relobj_end();
  3425. ++p)
  3426. symbol_count += (*p)->local_symbol_count();
  3427. // Go from "upper bound" to "estimate." We overcount for two
  3428. // reasons: we double-count symbols that occur in more than one
  3429. // object file, and we count symbols that are dropped from the
  3430. // output. Add it all together and assume we overcount by 100%.
  3431. symbol_count /= 2;
  3432. // We assume all symbols will go into both the sympool and dynpool.
  3433. this->sympool_.reserve(symbol_count);
  3434. this->dynpool_.reserve(symbol_count);
  3435. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  3436. p != input_objects->relobj_end();
  3437. ++p)
  3438. {
  3439. Task_lock_obj<Object> tlo(task, *p);
  3440. (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
  3441. }
  3442. }
  3443. // Create the symbol table sections. Here we also set the final
  3444. // values of the symbols. At this point all the loadable sections are
  3445. // fully laid out. SHNUM is the number of sections so far.
  3446. void
  3447. Layout::create_symtab_sections(const Input_objects* input_objects,
  3448. Symbol_table* symtab,
  3449. unsigned int shnum,
  3450. off_t* poff)
  3451. {
  3452. int symsize;
  3453. unsigned int align;
  3454. if (parameters->target().get_size() == 32)
  3455. {
  3456. symsize = elfcpp::Elf_sizes<32>::sym_size;
  3457. align = 4;
  3458. }
  3459. else if (parameters->target().get_size() == 64)
  3460. {
  3461. symsize = elfcpp::Elf_sizes<64>::sym_size;
  3462. align = 8;
  3463. }
  3464. else
  3465. gold_unreachable();
  3466. // Compute file offsets relative to the start of the symtab section.
  3467. off_t off = 0;
  3468. // Save space for the dummy symbol at the start of the section. We
  3469. // never bother to write this out--it will just be left as zero.
  3470. off += symsize;
  3471. unsigned int local_symbol_index = 1;
  3472. // Add STT_SECTION symbols for each Output section which needs one.
  3473. for (Section_list::iterator p = this->section_list_.begin();
  3474. p != this->section_list_.end();
  3475. ++p)
  3476. {
  3477. if (!(*p)->needs_symtab_index())
  3478. (*p)->set_symtab_index(-1U);
  3479. else
  3480. {
  3481. (*p)->set_symtab_index(local_symbol_index);
  3482. ++local_symbol_index;
  3483. off += symsize;
  3484. }
  3485. }
  3486. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  3487. p != input_objects->relobj_end();
  3488. ++p)
  3489. {
  3490. unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
  3491. off, symtab);
  3492. off += (index - local_symbol_index) * symsize;
  3493. local_symbol_index = index;
  3494. }
  3495. unsigned int local_symcount = local_symbol_index;
  3496. gold_assert(static_cast<off_t>(local_symcount * symsize) == off);
  3497. off_t dynoff;
  3498. size_t dyn_global_index;
  3499. size_t dyncount;
  3500. if (this->dynsym_section_ == NULL)
  3501. {
  3502. dynoff = 0;
  3503. dyn_global_index = 0;
  3504. dyncount = 0;
  3505. }
  3506. else
  3507. {
  3508. dyn_global_index = this->dynsym_section_->info();
  3509. off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
  3510. dynoff = this->dynsym_section_->offset() + locsize;
  3511. dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
  3512. gold_assert(static_cast<off_t>(dyncount * symsize)
  3513. == this->dynsym_section_->data_size() - locsize);
  3514. }
  3515. off_t global_off = off;
  3516. off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
  3517. &this->sympool_, &local_symcount);
  3518. if (!parameters->options().strip_all())
  3519. {
  3520. this->sympool_.set_string_offsets();
  3521. const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
  3522. Output_section* osymtab = this->make_output_section(symtab_name,
  3523. elfcpp::SHT_SYMTAB,
  3524. 0, ORDER_INVALID,
  3525. false);
  3526. this->symtab_section_ = osymtab;
  3527. Output_section_data* pos = new Output_data_fixed_space(off, align,
  3528. "** symtab");
  3529. osymtab->add_output_section_data(pos);
  3530. // We generate a .symtab_shndx section if we have more than
  3531. // SHN_LORESERVE sections. Technically it is possible that we
  3532. // don't need one, because it is possible that there are no
  3533. // symbols in any of sections with indexes larger than
  3534. // SHN_LORESERVE. That is probably unusual, though, and it is
  3535. // easier to always create one than to compute section indexes
  3536. // twice (once here, once when writing out the symbols).
  3537. if (shnum >= elfcpp::SHN_LORESERVE)
  3538. {
  3539. const char* symtab_xindex_name = this->namepool_.add(".symtab_shndx",
  3540. false, NULL);
  3541. Output_section* osymtab_xindex =
  3542. this->make_output_section(symtab_xindex_name,
  3543. elfcpp::SHT_SYMTAB_SHNDX, 0,
  3544. ORDER_INVALID, false);
  3545. size_t symcount = off / symsize;
  3546. this->symtab_xindex_ = new Output_symtab_xindex(symcount);
  3547. osymtab_xindex->add_output_section_data(this->symtab_xindex_);
  3548. osymtab_xindex->set_link_section(osymtab);
  3549. osymtab_xindex->set_addralign(4);
  3550. osymtab_xindex->set_entsize(4);
  3551. osymtab_xindex->set_after_input_sections();
  3552. // This tells the driver code to wait until the symbol table
  3553. // has written out before writing out the postprocessing
  3554. // sections, including the .symtab_shndx section.
  3555. this->any_postprocessing_sections_ = true;
  3556. }
  3557. const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
  3558. Output_section* ostrtab = this->make_output_section(strtab_name,
  3559. elfcpp::SHT_STRTAB,
  3560. 0, ORDER_INVALID,
  3561. false);
  3562. Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
  3563. ostrtab->add_output_section_data(pstr);
  3564. off_t symtab_off;
  3565. if (!parameters->incremental_update())
  3566. symtab_off = align_address(*poff, align);
  3567. else
  3568. {
  3569. symtab_off = this->allocate(off, align, *poff);
  3570. if (off == -1)
  3571. gold_fallback(_("out of patch space for symbol table; "
  3572. "relink with --incremental-full"));
  3573. gold_debug(DEBUG_INCREMENTAL,
  3574. "create_symtab_sections: %08lx %08lx .symtab",
  3575. static_cast<long>(symtab_off),
  3576. static_cast<long>(off));
  3577. }
  3578. symtab->set_file_offset(symtab_off + global_off);
  3579. osymtab->set_file_offset(symtab_off);
  3580. osymtab->finalize_data_size();
  3581. osymtab->set_link_section(ostrtab);
  3582. osymtab->set_info(local_symcount);
  3583. osymtab->set_entsize(symsize);
  3584. if (symtab_off + off > *poff)
  3585. *poff = symtab_off + off;
  3586. }
  3587. }
  3588. // Create the .shstrtab section, which holds the names of the
  3589. // sections. At the time this is called, we have created all the
  3590. // output sections except .shstrtab itself.
  3591. Output_section*
  3592. Layout::create_shstrtab()
  3593. {
  3594. // FIXME: We don't need to create a .shstrtab section if we are
  3595. // stripping everything.
  3596. const char* name = this->namepool_.add(".shstrtab", false, NULL);
  3597. Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0,
  3598. ORDER_INVALID, false);
  3599. if (strcmp(parameters->options().compress_debug_sections(), "none") != 0)
  3600. {
  3601. // We can't write out this section until we've set all the
  3602. // section names, and we don't set the names of compressed
  3603. // output sections until relocations are complete. FIXME: With
  3604. // the current names we use, this is unnecessary.
  3605. os->set_after_input_sections();
  3606. }
  3607. Output_section_data* posd = new Output_data_strtab(&this->namepool_);
  3608. os->add_output_section_data(posd);
  3609. return os;
  3610. }
  3611. // Create the section headers. SIZE is 32 or 64. OFF is the file
  3612. // offset.
  3613. void
  3614. Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff)
  3615. {
  3616. Output_section_headers* oshdrs;
  3617. oshdrs = new Output_section_headers(this,
  3618. &this->segment_list_,
  3619. &this->section_list_,
  3620. &this->unattached_section_list_,
  3621. &this->namepool_,
  3622. shstrtab_section);
  3623. off_t off;
  3624. if (!parameters->incremental_update())
  3625. off = align_address(*poff, oshdrs->addralign());
  3626. else
  3627. {
  3628. oshdrs->pre_finalize_data_size();
  3629. off = this->allocate(oshdrs->data_size(), oshdrs->addralign(), *poff);
  3630. if (off == -1)
  3631. gold_fallback(_("out of patch space for section header table; "
  3632. "relink with --incremental-full"));
  3633. gold_debug(DEBUG_INCREMENTAL,
  3634. "create_shdrs: %08lx %08lx (section header table)",
  3635. static_cast<long>(off),
  3636. static_cast<long>(off + oshdrs->data_size()));
  3637. }
  3638. oshdrs->set_address_and_file_offset(0, off);
  3639. off += oshdrs->data_size();
  3640. if (off > *poff)
  3641. *poff = off;
  3642. this->section_headers_ = oshdrs;
  3643. }
  3644. // Count the allocated sections.
  3645. size_t
  3646. Layout::allocated_output_section_count() const
  3647. {
  3648. size_t section_count = 0;
  3649. for (Segment_list::const_iterator p = this->segment_list_.begin();
  3650. p != this->segment_list_.end();
  3651. ++p)
  3652. section_count += (*p)->output_section_count();
  3653. return section_count;
  3654. }
  3655. // Create the dynamic symbol table.
  3656. void
  3657. Layout::create_dynamic_symtab(const Input_objects* input_objects,
  3658. Symbol_table* symtab,
  3659. Output_section** pdynstr,
  3660. unsigned int* plocal_dynamic_count,
  3661. std::vector<Symbol*>* pdynamic_symbols,
  3662. Versions* pversions)
  3663. {
  3664. // Count all the symbols in the dynamic symbol table, and set the
  3665. // dynamic symbol indexes.
  3666. // Skip symbol 0, which is always all zeroes.
  3667. unsigned int index = 1;
  3668. // Add STT_SECTION symbols for each Output section which needs one.
  3669. for (Section_list::iterator p = this->section_list_.begin();
  3670. p != this->section_list_.end();
  3671. ++p)
  3672. {
  3673. if (!(*p)->needs_dynsym_index())
  3674. (*p)->set_dynsym_index(-1U);
  3675. else
  3676. {
  3677. (*p)->set_dynsym_index(index);
  3678. ++index;
  3679. }
  3680. }
  3681. // Count the local symbols that need to go in the dynamic symbol table,
  3682. // and set the dynamic symbol indexes.
  3683. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  3684. p != input_objects->relobj_end();
  3685. ++p)
  3686. {
  3687. unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
  3688. index = new_index;
  3689. }
  3690. unsigned int local_symcount = index;
  3691. *plocal_dynamic_count = local_symcount;
  3692. index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
  3693. &this->dynpool_, pversions);
  3694. int symsize;
  3695. unsigned int align;
  3696. const int size = parameters->target().get_size();
  3697. if (size == 32)
  3698. {
  3699. symsize = elfcpp::Elf_sizes<32>::sym_size;
  3700. align = 4;
  3701. }
  3702. else if (size == 64)
  3703. {
  3704. symsize = elfcpp::Elf_sizes<64>::sym_size;
  3705. align = 8;
  3706. }
  3707. else
  3708. gold_unreachable();
  3709. // Create the dynamic symbol table section.
  3710. Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
  3711. elfcpp::SHT_DYNSYM,
  3712. elfcpp::SHF_ALLOC,
  3713. false,
  3714. ORDER_DYNAMIC_LINKER,
  3715. false);
  3716. // Check for NULL as a linker script may discard .dynsym.
  3717. if (dynsym != NULL)
  3718. {
  3719. Output_section_data* odata = new Output_data_fixed_space(index * symsize,
  3720. align,
  3721. "** dynsym");
  3722. dynsym->add_output_section_data(odata);
  3723. dynsym->set_info(local_symcount);
  3724. dynsym->set_entsize(symsize);
  3725. dynsym->set_addralign(align);
  3726. this->dynsym_section_ = dynsym;
  3727. }
  3728. Output_data_dynamic* const odyn = this->dynamic_data_;
  3729. if (odyn != NULL)
  3730. {
  3731. odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
  3732. odyn->add_constant(elfcpp::DT_SYMENT, symsize);
  3733. }
  3734. // If there are more than SHN_LORESERVE allocated sections, we
  3735. // create a .dynsym_shndx section. It is possible that we don't
  3736. // need one, because it is possible that there are no dynamic
  3737. // symbols in any of the sections with indexes larger than
  3738. // SHN_LORESERVE. This is probably unusual, though, and at this
  3739. // time we don't know the actual section indexes so it is
  3740. // inconvenient to check.
  3741. if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE)
  3742. {
  3743. Output_section* dynsym_xindex =
  3744. this->choose_output_section(NULL, ".dynsym_shndx",
  3745. elfcpp::SHT_SYMTAB_SHNDX,
  3746. elfcpp::SHF_ALLOC,
  3747. false, ORDER_DYNAMIC_LINKER, false);
  3748. if (dynsym_xindex != NULL)
  3749. {
  3750. this->dynsym_xindex_ = new Output_symtab_xindex(index);
  3751. dynsym_xindex->add_output_section_data(this->dynsym_xindex_);
  3752. dynsym_xindex->set_link_section(dynsym);
  3753. dynsym_xindex->set_addralign(4);
  3754. dynsym_xindex->set_entsize(4);
  3755. dynsym_xindex->set_after_input_sections();
  3756. // This tells the driver code to wait until the symbol table
  3757. // has written out before writing out the postprocessing
  3758. // sections, including the .dynsym_shndx section.
  3759. this->any_postprocessing_sections_ = true;
  3760. }
  3761. }
  3762. // Create the dynamic string table section.
  3763. Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
  3764. elfcpp::SHT_STRTAB,
  3765. elfcpp::SHF_ALLOC,
  3766. false,
  3767. ORDER_DYNAMIC_LINKER,
  3768. false);
  3769. *pdynstr = dynstr;
  3770. if (dynstr != NULL)
  3771. {
  3772. Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
  3773. dynstr->add_output_section_data(strdata);
  3774. if (dynsym != NULL)
  3775. dynsym->set_link_section(dynstr);
  3776. if (this->dynamic_section_ != NULL)
  3777. this->dynamic_section_->set_link_section(dynstr);
  3778. if (odyn != NULL)
  3779. {
  3780. odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
  3781. odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
  3782. }
  3783. }
  3784. // Create the hash tables. The Gnu-style hash table must be
  3785. // built first, because it changes the order of the symbols
  3786. // in the dynamic symbol table.
  3787. if (strcmp(parameters->options().hash_style(), "gnu") == 0
  3788. || strcmp(parameters->options().hash_style(), "both") == 0)
  3789. {
  3790. unsigned char* phash;
  3791. unsigned int hashlen;
  3792. Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount,
  3793. &phash, &hashlen);
  3794. Output_section* hashsec =
  3795. this->choose_output_section(NULL, ".gnu.hash", elfcpp::SHT_GNU_HASH,
  3796. elfcpp::SHF_ALLOC, false,
  3797. ORDER_DYNAMIC_LINKER, false);
  3798. Output_section_data* hashdata = new Output_data_const_buffer(phash,
  3799. hashlen,
  3800. align,
  3801. "** hash");
  3802. if (hashsec != NULL && hashdata != NULL)
  3803. hashsec->add_output_section_data(hashdata);
  3804. if (hashsec != NULL)
  3805. {
  3806. if (dynsym != NULL)
  3807. hashsec->set_link_section(dynsym);
  3808. // For a 64-bit target, the entries in .gnu.hash do not have
  3809. // a uniform size, so we only set the entry size for a
  3810. // 32-bit target.
  3811. if (parameters->target().get_size() == 32)
  3812. hashsec->set_entsize(4);
  3813. if (odyn != NULL)
  3814. odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
  3815. }
  3816. }
  3817. if (strcmp(parameters->options().hash_style(), "sysv") == 0
  3818. || strcmp(parameters->options().hash_style(), "both") == 0)
  3819. {
  3820. unsigned char* phash;
  3821. unsigned int hashlen;
  3822. Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
  3823. &phash, &hashlen);
  3824. Output_section* hashsec =
  3825. this->choose_output_section(NULL, ".hash", elfcpp::SHT_HASH,
  3826. elfcpp::SHF_ALLOC, false,
  3827. ORDER_DYNAMIC_LINKER, false);
  3828. Output_section_data* hashdata = new Output_data_const_buffer(phash,
  3829. hashlen,
  3830. align,
  3831. "** hash");
  3832. if (hashsec != NULL && hashdata != NULL)
  3833. hashsec->add_output_section_data(hashdata);
  3834. if (hashsec != NULL)
  3835. {
  3836. if (dynsym != NULL)
  3837. hashsec->set_link_section(dynsym);
  3838. hashsec->set_entsize(4);
  3839. }
  3840. if (odyn != NULL)
  3841. odyn->add_section_address(elfcpp::DT_HASH, hashsec);
  3842. }
  3843. }
  3844. // Assign offsets to each local portion of the dynamic symbol table.
  3845. void
  3846. Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
  3847. {
  3848. Output_section* dynsym = this->dynsym_section_;
  3849. if (dynsym == NULL)
  3850. return;
  3851. off_t off = dynsym->offset();
  3852. // Skip the dummy symbol at the start of the section.
  3853. off += dynsym->entsize();
  3854. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  3855. p != input_objects->relobj_end();
  3856. ++p)
  3857. {
  3858. unsigned int count = (*p)->set_local_dynsym_offset(off);
  3859. off += count * dynsym->entsize();
  3860. }
  3861. }
  3862. // Create the version sections.
  3863. void
  3864. Layout::create_version_sections(const Versions* versions,
  3865. const Symbol_table* symtab,
  3866. unsigned int local_symcount,
  3867. const std::vector<Symbol*>& dynamic_symbols,
  3868. const Output_section* dynstr)
  3869. {
  3870. if (!versions->any_defs() && !versions->any_needs())
  3871. return;
  3872. switch (parameters->size_and_endianness())
  3873. {
  3874. #ifdef HAVE_TARGET_32_LITTLE
  3875. case Parameters::TARGET_32_LITTLE:
  3876. this->sized_create_version_sections<32, false>(versions, symtab,
  3877. local_symcount,
  3878. dynamic_symbols, dynstr);
  3879. break;
  3880. #endif
  3881. #ifdef HAVE_TARGET_32_BIG
  3882. case Parameters::TARGET_32_BIG:
  3883. this->sized_create_version_sections<32, true>(versions, symtab,
  3884. local_symcount,
  3885. dynamic_symbols, dynstr);
  3886. break;
  3887. #endif
  3888. #ifdef HAVE_TARGET_64_LITTLE
  3889. case Parameters::TARGET_64_LITTLE:
  3890. this->sized_create_version_sections<64, false>(versions, symtab,
  3891. local_symcount,
  3892. dynamic_symbols, dynstr);
  3893. break;
  3894. #endif
  3895. #ifdef HAVE_TARGET_64_BIG
  3896. case Parameters::TARGET_64_BIG:
  3897. this->sized_create_version_sections<64, true>(versions, symtab,
  3898. local_symcount,
  3899. dynamic_symbols, dynstr);
  3900. break;
  3901. #endif
  3902. default:
  3903. gold_unreachable();
  3904. }
  3905. }
  3906. // Create the version sections, sized version.
  3907. template<int size, bool big_endian>
  3908. void
  3909. Layout::sized_create_version_sections(
  3910. const Versions* versions,
  3911. const Symbol_table* symtab,
  3912. unsigned int local_symcount,
  3913. const std::vector<Symbol*>& dynamic_symbols,
  3914. const Output_section* dynstr)
  3915. {
  3916. Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
  3917. elfcpp::SHT_GNU_versym,
  3918. elfcpp::SHF_ALLOC,
  3919. false,
  3920. ORDER_DYNAMIC_LINKER,
  3921. false);
  3922. // Check for NULL since a linker script may discard this section.
  3923. if (vsec != NULL)
  3924. {
  3925. unsigned char* vbuf;
  3926. unsigned int vsize;
  3927. versions->symbol_section_contents<size, big_endian>(symtab,
  3928. &this->dynpool_,
  3929. local_symcount,
  3930. dynamic_symbols,
  3931. &vbuf, &vsize);
  3932. Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2,
  3933. "** versions");
  3934. vsec->add_output_section_data(vdata);
  3935. vsec->set_entsize(2);
  3936. vsec->set_link_section(this->dynsym_section_);
  3937. }
  3938. Output_data_dynamic* const odyn = this->dynamic_data_;
  3939. if (odyn != NULL && vsec != NULL)
  3940. odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
  3941. if (versions->any_defs())
  3942. {
  3943. Output_section* vdsec;
  3944. vdsec = this->choose_output_section(NULL, ".gnu.version_d",
  3945. elfcpp::SHT_GNU_verdef,
  3946. elfcpp::SHF_ALLOC,
  3947. false, ORDER_DYNAMIC_LINKER, false);
  3948. if (vdsec != NULL)
  3949. {
  3950. unsigned char* vdbuf;
  3951. unsigned int vdsize;
  3952. unsigned int vdentries;
  3953. versions->def_section_contents<size, big_endian>(&this->dynpool_,
  3954. &vdbuf, &vdsize,
  3955. &vdentries);
  3956. Output_section_data* vddata =
  3957. new Output_data_const_buffer(vdbuf, vdsize, 4, "** version defs");
  3958. vdsec->add_output_section_data(vddata);
  3959. vdsec->set_link_section(dynstr);
  3960. vdsec->set_info(vdentries);
  3961. if (odyn != NULL)
  3962. {
  3963. odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
  3964. odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
  3965. }
  3966. }
  3967. }
  3968. if (versions->any_needs())
  3969. {
  3970. Output_section* vnsec;
  3971. vnsec = this->choose_output_section(NULL, ".gnu.version_r",
  3972. elfcpp::SHT_GNU_verneed,
  3973. elfcpp::SHF_ALLOC,
  3974. false, ORDER_DYNAMIC_LINKER, false);
  3975. if (vnsec != NULL)
  3976. {
  3977. unsigned char* vnbuf;
  3978. unsigned int vnsize;
  3979. unsigned int vnentries;
  3980. versions->need_section_contents<size, big_endian>(&this->dynpool_,
  3981. &vnbuf, &vnsize,
  3982. &vnentries);
  3983. Output_section_data* vndata =
  3984. new Output_data_const_buffer(vnbuf, vnsize, 4, "** version refs");
  3985. vnsec->add_output_section_data(vndata);
  3986. vnsec->set_link_section(dynstr);
  3987. vnsec->set_info(vnentries);
  3988. if (odyn != NULL)
  3989. {
  3990. odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
  3991. odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
  3992. }
  3993. }
  3994. }
  3995. }
  3996. // Create the .interp section and PT_INTERP segment.
  3997. void
  3998. Layout::create_interp(const Target* target)
  3999. {
  4000. gold_assert(this->interp_segment_ == NULL);
  4001. const char* interp = parameters->options().dynamic_linker();
  4002. if (interp == NULL)
  4003. {
  4004. interp = target->dynamic_linker();
  4005. gold_assert(interp != NULL);
  4006. }
  4007. size_t len = strlen(interp) + 1;
  4008. Output_section_data* odata = new Output_data_const(interp, len, 1);
  4009. Output_section* osec = this->choose_output_section(NULL, ".interp",
  4010. elfcpp::SHT_PROGBITS,
  4011. elfcpp::SHF_ALLOC,
  4012. false, ORDER_INTERP,
  4013. false);
  4014. if (osec != NULL)
  4015. osec->add_output_section_data(odata);
  4016. }
  4017. // Add dynamic tags for the PLT and the dynamic relocs. This is
  4018. // called by the target-specific code. This does nothing if not doing
  4019. // a dynamic link.
  4020. // USE_REL is true for REL relocs rather than RELA relocs.
  4021. // If PLT_GOT is not NULL, then DT_PLTGOT points to it.
  4022. // If PLT_REL is not NULL, it is used for DT_PLTRELSZ, and DT_JMPREL,
  4023. // and we also set DT_PLTREL. We use PLT_REL's output section, since
  4024. // some targets have multiple reloc sections in PLT_REL.
  4025. // If DYN_REL is not NULL, it is used for DT_REL/DT_RELA,
  4026. // DT_RELSZ/DT_RELASZ, DT_RELENT/DT_RELAENT. Again we use the output
  4027. // section.
  4028. // If ADD_DEBUG is true, we add a DT_DEBUG entry when generating an
  4029. // executable.
  4030. void
  4031. Layout::add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
  4032. const Output_data* plt_rel,
  4033. const Output_data_reloc_generic* dyn_rel,
  4034. bool add_debug, bool dynrel_includes_plt)
  4035. {
  4036. Output_data_dynamic* odyn = this->dynamic_data_;
  4037. if (odyn == NULL)
  4038. return;
  4039. if (plt_got != NULL && plt_got->output_section() != NULL)
  4040. odyn->add_section_address(elfcpp::DT_PLTGOT, plt_got);
  4041. if (plt_rel != NULL && plt_rel->output_section() != NULL)
  4042. {
  4043. odyn->add_section_size(elfcpp::DT_PLTRELSZ, plt_rel->output_section());
  4044. odyn->add_section_address(elfcpp::DT_JMPREL, plt_rel->output_section());
  4045. odyn->add_constant(elfcpp::DT_PLTREL,
  4046. use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA);
  4047. }
  4048. if ((dyn_rel != NULL && dyn_rel->output_section() != NULL)
  4049. || (dynrel_includes_plt
  4050. && plt_rel != NULL
  4051. && plt_rel->output_section() != NULL))
  4052. {
  4053. bool have_dyn_rel = dyn_rel != NULL && dyn_rel->output_section() != NULL;
  4054. bool have_plt_rel = plt_rel != NULL && plt_rel->output_section() != NULL;
  4055. odyn->add_section_address(use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA,
  4056. (have_dyn_rel
  4057. ? dyn_rel->output_section()
  4058. : plt_rel->output_section()));
  4059. elfcpp::DT size_tag = use_rel ? elfcpp::DT_RELSZ : elfcpp::DT_RELASZ;
  4060. if (have_dyn_rel && have_plt_rel && dynrel_includes_plt)
  4061. odyn->add_section_size(size_tag,
  4062. dyn_rel->output_section(),
  4063. plt_rel->output_section());
  4064. else if (have_dyn_rel)
  4065. odyn->add_section_size(size_tag, dyn_rel->output_section());
  4066. else
  4067. odyn->add_section_size(size_tag, plt_rel->output_section());
  4068. const int size = parameters->target().get_size();
  4069. elfcpp::DT rel_tag;
  4070. int rel_size;
  4071. if (use_rel)
  4072. {
  4073. rel_tag = elfcpp::DT_RELENT;
  4074. if (size == 32)
  4075. rel_size = Reloc_types<elfcpp::SHT_REL, 32, false>::reloc_size;
  4076. else if (size == 64)
  4077. rel_size = Reloc_types<elfcpp::SHT_REL, 64, false>::reloc_size;
  4078. else
  4079. gold_unreachable();
  4080. }
  4081. else
  4082. {
  4083. rel_tag = elfcpp::DT_RELAENT;
  4084. if (size == 32)
  4085. rel_size = Reloc_types<elfcpp::SHT_RELA, 32, false>::reloc_size;
  4086. else if (size == 64)
  4087. rel_size = Reloc_types<elfcpp::SHT_RELA, 64, false>::reloc_size;
  4088. else
  4089. gold_unreachable();
  4090. }
  4091. odyn->add_constant(rel_tag, rel_size);
  4092. if (parameters->options().combreloc() && have_dyn_rel)
  4093. {
  4094. size_t c = dyn_rel->relative_reloc_count();
  4095. if (c > 0)
  4096. odyn->add_constant((use_rel
  4097. ? elfcpp::DT_RELCOUNT
  4098. : elfcpp::DT_RELACOUNT),
  4099. c);
  4100. }
  4101. }
  4102. if (add_debug && !parameters->options().shared())
  4103. {
  4104. // The value of the DT_DEBUG tag is filled in by the dynamic
  4105. // linker at run time, and used by the debugger.
  4106. odyn->add_constant(elfcpp::DT_DEBUG, 0);
  4107. }
  4108. }
  4109. // Finish the .dynamic section and PT_DYNAMIC segment.
  4110. void
  4111. Layout::finish_dynamic_section(const Input_objects* input_objects,
  4112. const Symbol_table* symtab)
  4113. {
  4114. if (!this->script_options_->saw_phdrs_clause()
  4115. && this->dynamic_section_ != NULL)
  4116. {
  4117. Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
  4118. (elfcpp::PF_R
  4119. | elfcpp::PF_W));
  4120. oseg->add_output_section_to_nonload(this->dynamic_section_,
  4121. elfcpp::PF_R | elfcpp::PF_W);
  4122. }
  4123. Output_data_dynamic* const odyn = this->dynamic_data_;
  4124. if (odyn == NULL)
  4125. return;
  4126. for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
  4127. p != input_objects->dynobj_end();
  4128. ++p)
  4129. {
  4130. if (!(*p)->is_needed() && (*p)->as_needed())
  4131. {
  4132. // This dynamic object was linked with --as-needed, but it
  4133. // is not needed.
  4134. continue;
  4135. }
  4136. odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
  4137. }
  4138. if (parameters->options().shared())
  4139. {
  4140. const char* soname = parameters->options().soname();
  4141. if (soname != NULL)
  4142. odyn->add_string(elfcpp::DT_SONAME, soname);
  4143. }
  4144. Symbol* sym = symtab->lookup(parameters->options().init());
  4145. if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
  4146. odyn->add_symbol(elfcpp::DT_INIT, sym);
  4147. sym = symtab->lookup(parameters->options().fini());
  4148. if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
  4149. odyn->add_symbol(elfcpp::DT_FINI, sym);
  4150. // Look for .init_array, .preinit_array and .fini_array by checking
  4151. // section types.
  4152. for(Layout::Section_list::const_iterator p = this->section_list_.begin();
  4153. p != this->section_list_.end();
  4154. ++p)
  4155. switch((*p)->type())
  4156. {
  4157. case elfcpp::SHT_FINI_ARRAY:
  4158. odyn->add_section_address(elfcpp::DT_FINI_ARRAY, *p);
  4159. odyn->add_section_size(elfcpp::DT_FINI_ARRAYSZ, *p);
  4160. break;
  4161. case elfcpp::SHT_INIT_ARRAY:
  4162. odyn->add_section_address(elfcpp::DT_INIT_ARRAY, *p);
  4163. odyn->add_section_size(elfcpp::DT_INIT_ARRAYSZ, *p);
  4164. break;
  4165. case elfcpp::SHT_PREINIT_ARRAY:
  4166. odyn->add_section_address(elfcpp::DT_PREINIT_ARRAY, *p);
  4167. odyn->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ, *p);
  4168. break;
  4169. default:
  4170. break;
  4171. }
  4172. // Add a DT_RPATH entry if needed.
  4173. const General_options::Dir_list& rpath(parameters->options().rpath());
  4174. if (!rpath.empty())
  4175. {
  4176. std::string rpath_val;
  4177. for (General_options::Dir_list::const_iterator p = rpath.begin();
  4178. p != rpath.end();
  4179. ++p)
  4180. {
  4181. if (rpath_val.empty())
  4182. rpath_val = p->name();
  4183. else
  4184. {
  4185. // Eliminate duplicates.
  4186. General_options::Dir_list::const_iterator q;
  4187. for (q = rpath.begin(); q != p; ++q)
  4188. if (q->name() == p->name())
  4189. break;
  4190. if (q == p)
  4191. {
  4192. rpath_val += ':';
  4193. rpath_val += p->name();
  4194. }
  4195. }
  4196. }
  4197. if (!parameters->options().enable_new_dtags())
  4198. odyn->add_string(elfcpp::DT_RPATH, rpath_val);
  4199. else
  4200. odyn->add_string(elfcpp::DT_RUNPATH, rpath_val);
  4201. }
  4202. // Look for text segments that have dynamic relocations.
  4203. bool have_textrel = false;
  4204. if (!this->script_options_->saw_sections_clause())
  4205. {
  4206. for (Segment_list::const_iterator p = this->segment_list_.begin();
  4207. p != this->segment_list_.end();
  4208. ++p)
  4209. {
  4210. if ((*p)->type() == elfcpp::PT_LOAD
  4211. && ((*p)->flags() & elfcpp::PF_W) == 0
  4212. && (*p)->has_dynamic_reloc())
  4213. {
  4214. have_textrel = true;
  4215. break;
  4216. }
  4217. }
  4218. }
  4219. else
  4220. {
  4221. // We don't know the section -> segment mapping, so we are
  4222. // conservative and just look for readonly sections with
  4223. // relocations. If those sections wind up in writable segments,
  4224. // then we have created an unnecessary DT_TEXTREL entry.
  4225. for (Section_list::const_iterator p = this->section_list_.begin();
  4226. p != this->section_list_.end();
  4227. ++p)
  4228. {
  4229. if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
  4230. && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
  4231. && (*p)->has_dynamic_reloc())
  4232. {
  4233. have_textrel = true;
  4234. break;
  4235. }
  4236. }
  4237. }
  4238. if (parameters->options().filter() != NULL)
  4239. odyn->add_string(elfcpp::DT_FILTER, parameters->options().filter());
  4240. if (parameters->options().any_auxiliary())
  4241. {
  4242. for (options::String_set::const_iterator p =
  4243. parameters->options().auxiliary_begin();
  4244. p != parameters->options().auxiliary_end();
  4245. ++p)
  4246. odyn->add_string(elfcpp::DT_AUXILIARY, *p);
  4247. }
  4248. // Add a DT_FLAGS entry if necessary.
  4249. unsigned int flags = 0;
  4250. if (have_textrel)
  4251. {
  4252. // Add a DT_TEXTREL for compatibility with older loaders.
  4253. odyn->add_constant(elfcpp::DT_TEXTREL, 0);
  4254. flags |= elfcpp::DF_TEXTREL;
  4255. if (parameters->options().text())
  4256. gold_error(_("read-only segment has dynamic relocations"));
  4257. else if (parameters->options().warn_shared_textrel()
  4258. && parameters->options().shared())
  4259. gold_warning(_("shared library text segment is not shareable"));
  4260. }
  4261. if (parameters->options().shared() && this->has_static_tls())
  4262. flags |= elfcpp::DF_STATIC_TLS;
  4263. if (parameters->options().origin())
  4264. flags |= elfcpp::DF_ORIGIN;
  4265. if (parameters->options().Bsymbolic()
  4266. && !parameters->options().have_dynamic_list())
  4267. {
  4268. flags |= elfcpp::DF_SYMBOLIC;
  4269. // Add DT_SYMBOLIC for compatibility with older loaders.
  4270. odyn->add_constant(elfcpp::DT_SYMBOLIC, 0);
  4271. }
  4272. if (parameters->options().now())
  4273. flags |= elfcpp::DF_BIND_NOW;
  4274. if (flags != 0)
  4275. odyn->add_constant(elfcpp::DT_FLAGS, flags);
  4276. flags = 0;
  4277. if (parameters->options().global())
  4278. flags |= elfcpp::DF_1_GLOBAL;
  4279. if (parameters->options().initfirst())
  4280. flags |= elfcpp::DF_1_INITFIRST;
  4281. if (parameters->options().interpose())
  4282. flags |= elfcpp::DF_1_INTERPOSE;
  4283. if (parameters->options().loadfltr())
  4284. flags |= elfcpp::DF_1_LOADFLTR;
  4285. if (parameters->options().nodefaultlib())
  4286. flags |= elfcpp::DF_1_NODEFLIB;
  4287. if (parameters->options().nodelete())
  4288. flags |= elfcpp::DF_1_NODELETE;
  4289. if (parameters->options().nodlopen())
  4290. flags |= elfcpp::DF_1_NOOPEN;
  4291. if (parameters->options().nodump())
  4292. flags |= elfcpp::DF_1_NODUMP;
  4293. if (!parameters->options().shared())
  4294. flags &= ~(elfcpp::DF_1_INITFIRST
  4295. | elfcpp::DF_1_NODELETE
  4296. | elfcpp::DF_1_NOOPEN);
  4297. if (parameters->options().origin())
  4298. flags |= elfcpp::DF_1_ORIGIN;
  4299. if (parameters->options().now())
  4300. flags |= elfcpp::DF_1_NOW;
  4301. if (parameters->options().Bgroup())
  4302. flags |= elfcpp::DF_1_GROUP;
  4303. if (flags != 0)
  4304. odyn->add_constant(elfcpp::DT_FLAGS_1, flags);
  4305. }
  4306. // Set the size of the _DYNAMIC symbol table to be the size of the
  4307. // dynamic data.
  4308. void
  4309. Layout::set_dynamic_symbol_size(const Symbol_table* symtab)
  4310. {
  4311. Output_data_dynamic* const odyn = this->dynamic_data_;
  4312. if (odyn == NULL)
  4313. return;
  4314. odyn->finalize_data_size();
  4315. if (this->dynamic_symbol_ == NULL)
  4316. return;
  4317. off_t data_size = odyn->data_size();
  4318. const int size = parameters->target().get_size();
  4319. if (size == 32)
  4320. symtab->get_sized_symbol<32>(this->dynamic_symbol_)->set_symsize(data_size);
  4321. else if (size == 64)
  4322. symtab->get_sized_symbol<64>(this->dynamic_symbol_)->set_symsize(data_size);
  4323. else
  4324. gold_unreachable();
  4325. }
  4326. // The mapping of input section name prefixes to output section names.
  4327. // In some cases one prefix is itself a prefix of another prefix; in
  4328. // such a case the longer prefix must come first. These prefixes are
  4329. // based on the GNU linker default ELF linker script.
  4330. #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
  4331. #define MAPPING_INIT_EXACT(f, t) { f, 0, t, sizeof(t) - 1 }
  4332. const Layout::Section_name_mapping Layout::section_name_mapping[] =
  4333. {
  4334. MAPPING_INIT(".text.", ".text"),
  4335. MAPPING_INIT(".rodata.", ".rodata"),
  4336. MAPPING_INIT(".data.rel.ro.local.", ".data.rel.ro.local"),
  4337. MAPPING_INIT_EXACT(".data.rel.ro.local", ".data.rel.ro.local"),
  4338. MAPPING_INIT(".data.rel.ro.", ".data.rel.ro"),
  4339. MAPPING_INIT_EXACT(".data.rel.ro", ".data.rel.ro"),
  4340. MAPPING_INIT(".data.", ".data"),
  4341. MAPPING_INIT(".bss.", ".bss"),
  4342. MAPPING_INIT(".tdata.", ".tdata"),
  4343. MAPPING_INIT(".tbss.", ".tbss"),
  4344. MAPPING_INIT(".init_array.", ".init_array"),
  4345. MAPPING_INIT(".fini_array.", ".fini_array"),
  4346. MAPPING_INIT(".sdata.", ".sdata"),
  4347. MAPPING_INIT(".sbss.", ".sbss"),
  4348. // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
  4349. // differently depending on whether it is creating a shared library.
  4350. MAPPING_INIT(".sdata2.", ".sdata"),
  4351. MAPPING_INIT(".sbss2.", ".sbss"),
  4352. MAPPING_INIT(".lrodata.", ".lrodata"),
  4353. MAPPING_INIT(".ldata.", ".ldata"),
  4354. MAPPING_INIT(".lbss.", ".lbss"),
  4355. MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
  4356. MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
  4357. MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
  4358. MAPPING_INIT(".gnu.linkonce.t.", ".text"),
  4359. MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
  4360. MAPPING_INIT(".gnu.linkonce.d.", ".data"),
  4361. MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
  4362. MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
  4363. MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
  4364. MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
  4365. MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
  4366. MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
  4367. MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
  4368. MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
  4369. MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
  4370. MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
  4371. MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
  4372. MAPPING_INIT(".ARM.extab", ".ARM.extab"),
  4373. MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
  4374. MAPPING_INIT(".ARM.exidx", ".ARM.exidx"),
  4375. MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
  4376. };
  4377. #undef MAPPING_INIT
  4378. #undef MAPPING_INIT_EXACT
  4379. const int Layout::section_name_mapping_count =
  4380. (sizeof(Layout::section_name_mapping)
  4381. / sizeof(Layout::section_name_mapping[0]));
  4382. // Choose the output section name to use given an input section name.
  4383. // Set *PLEN to the length of the name. *PLEN is initialized to the
  4384. // length of NAME.
  4385. const char*
  4386. Layout::output_section_name(const Relobj* relobj, const char* name,
  4387. size_t* plen)
  4388. {
  4389. // gcc 4.3 generates the following sorts of section names when it
  4390. // needs a section name specific to a function:
  4391. // .text.FN
  4392. // .rodata.FN
  4393. // .sdata2.FN
  4394. // .data.FN
  4395. // .data.rel.FN
  4396. // .data.rel.local.FN
  4397. // .data.rel.ro.FN
  4398. // .data.rel.ro.local.FN
  4399. // .sdata.FN
  4400. // .bss.FN
  4401. // .sbss.FN
  4402. // .tdata.FN
  4403. // .tbss.FN
  4404. // The GNU linker maps all of those to the part before the .FN,
  4405. // except that .data.rel.local.FN is mapped to .data, and
  4406. // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
  4407. // beginning with .data.rel.ro.local are grouped together.
  4408. // For an anonymous namespace, the string FN can contain a '.'.
  4409. // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
  4410. // GNU linker maps to .rodata.
  4411. // The .data.rel.ro sections are used with -z relro. The sections
  4412. // are recognized by name. We use the same names that the GNU
  4413. // linker does for these sections.
  4414. // It is hard to handle this in a principled way, so we don't even
  4415. // try. We use a table of mappings. If the input section name is
  4416. // not found in the table, we simply use it as the output section
  4417. // name.
  4418. const Section_name_mapping* psnm = section_name_mapping;
  4419. for (int i = 0; i < section_name_mapping_count; ++i, ++psnm)
  4420. {
  4421. if (psnm->fromlen > 0)
  4422. {
  4423. if (strncmp(name, psnm->from, psnm->fromlen) == 0)
  4424. {
  4425. *plen = psnm->tolen;
  4426. return psnm->to;
  4427. }
  4428. }
  4429. else
  4430. {
  4431. if (strcmp(name, psnm->from) == 0)
  4432. {
  4433. *plen = psnm->tolen;
  4434. return psnm->to;
  4435. }
  4436. }
  4437. }
  4438. // As an additional complication, .ctors sections are output in
  4439. // either .ctors or .init_array sections, and .dtors sections are
  4440. // output in either .dtors or .fini_array sections.
  4441. if (is_prefix_of(".ctors.", name) || is_prefix_of(".dtors.", name))
  4442. {
  4443. if (parameters->options().ctors_in_init_array())
  4444. {
  4445. *plen = 11;
  4446. return name[1] == 'c' ? ".init_array" : ".fini_array";
  4447. }
  4448. else
  4449. {
  4450. *plen = 6;
  4451. return name[1] == 'c' ? ".ctors" : ".dtors";
  4452. }
  4453. }
  4454. if (parameters->options().ctors_in_init_array()
  4455. && (strcmp(name, ".ctors") == 0 || strcmp(name, ".dtors") == 0))
  4456. {
  4457. // To make .init_array/.fini_array work with gcc we must exclude
  4458. // .ctors and .dtors sections from the crtbegin and crtend
  4459. // files.
  4460. if (relobj == NULL
  4461. || (!Layout::match_file_name(relobj, "crtbegin")
  4462. && !Layout::match_file_name(relobj, "crtend")))
  4463. {
  4464. *plen = 11;
  4465. return name[1] == 'c' ? ".init_array" : ".fini_array";
  4466. }
  4467. }
  4468. return name;
  4469. }
  4470. // Return true if RELOBJ is an input file whose base name matches
  4471. // FILE_NAME. The base name must have an extension of ".o", and must
  4472. // be exactly FILE_NAME.o or FILE_NAME, one character, ".o". This is
  4473. // to match crtbegin.o as well as crtbeginS.o without getting confused
  4474. // by other possibilities. Overall matching the file name this way is
  4475. // a dreadful hack, but the GNU linker does it in order to better
  4476. // support gcc, and we need to be compatible.
  4477. bool
  4478. Layout::match_file_name(const Relobj* relobj, const char* match)
  4479. {
  4480. const std::string& file_name(relobj->name());
  4481. const char* base_name = lbasename(file_name.c_str());
  4482. size_t match_len = strlen(match);
  4483. if (strncmp(base_name, match, match_len) != 0)
  4484. return false;
  4485. size_t base_len = strlen(base_name);
  4486. if (base_len != match_len + 2 && base_len != match_len + 3)
  4487. return false;
  4488. return memcmp(base_name + base_len - 2, ".o", 2) == 0;
  4489. }
  4490. // Check if a comdat group or .gnu.linkonce section with the given
  4491. // NAME is selected for the link. If there is already a section,
  4492. // *KEPT_SECTION is set to point to the existing section and the
  4493. // function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
  4494. // IS_GROUP_NAME are recorded for this NAME in the layout object,
  4495. // *KEPT_SECTION is set to the internal copy and the function returns
  4496. // true.
  4497. bool
  4498. Layout::find_or_add_kept_section(const std::string& name,
  4499. Relobj* object,
  4500. unsigned int shndx,
  4501. bool is_comdat,
  4502. bool is_group_name,
  4503. Kept_section** kept_section)
  4504. {
  4505. // It's normal to see a couple of entries here, for the x86 thunk
  4506. // sections. If we see more than a few, we're linking a C++
  4507. // program, and we resize to get more space to minimize rehashing.
  4508. if (this->signatures_.size() > 4
  4509. && !this->resized_signatures_)
  4510. {
  4511. reserve_unordered_map(&this->signatures_,
  4512. this->number_of_input_files_ * 64);
  4513. this->resized_signatures_ = true;
  4514. }
  4515. Kept_section candidate;
  4516. std::pair<Signatures::iterator, bool> ins =
  4517. this->signatures_.insert(std::make_pair(name, candidate));
  4518. if (kept_section != NULL)
  4519. *kept_section = &ins.first->second;
  4520. if (ins.second)
  4521. {
  4522. // This is the first time we've seen this signature.
  4523. ins.first->second.set_object(object);
  4524. ins.first->second.set_shndx(shndx);
  4525. if (is_comdat)
  4526. ins.first->second.set_is_comdat();
  4527. if (is_group_name)
  4528. ins.first->second.set_is_group_name();
  4529. return true;
  4530. }
  4531. // We have already seen this signature.
  4532. if (ins.first->second.is_group_name())
  4533. {
  4534. // We've already seen a real section group with this signature.
  4535. // If the kept group is from a plugin object, and we're in the
  4536. // replacement phase, accept the new one as a replacement.
  4537. if (ins.first->second.object() == NULL
  4538. && parameters->options().plugins()->in_replacement_phase())
  4539. {
  4540. ins.first->second.set_object(object);
  4541. ins.first->second.set_shndx(shndx);
  4542. return true;
  4543. }
  4544. return false;
  4545. }
  4546. else if (is_group_name)
  4547. {
  4548. // This is a real section group, and we've already seen a
  4549. // linkonce section with this signature. Record that we've seen
  4550. // a section group, and don't include this section group.
  4551. ins.first->second.set_is_group_name();
  4552. return false;
  4553. }
  4554. else
  4555. {
  4556. // We've already seen a linkonce section and this is a linkonce
  4557. // section. These don't block each other--this may be the same
  4558. // symbol name with different section types.
  4559. return true;
  4560. }
  4561. }
  4562. // Store the allocated sections into the section list.
  4563. void
  4564. Layout::get_allocated_sections(Section_list* section_list) const
  4565. {
  4566. for (Section_list::const_iterator p = this->section_list_.begin();
  4567. p != this->section_list_.end();
  4568. ++p)
  4569. if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
  4570. section_list->push_back(*p);
  4571. }
  4572. // Store the executable sections into the section list.
  4573. void
  4574. Layout::get_executable_sections(Section_list* section_list) const
  4575. {
  4576. for (Section_list::const_iterator p = this->section_list_.begin();
  4577. p != this->section_list_.end();
  4578. ++p)
  4579. if (((*p)->flags() & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
  4580. == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
  4581. section_list->push_back(*p);
  4582. }
  4583. // Create an output segment.
  4584. Output_segment*
  4585. Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
  4586. {
  4587. gold_assert(!parameters->options().relocatable());
  4588. Output_segment* oseg = new Output_segment(type, flags);
  4589. this->segment_list_.push_back(oseg);
  4590. if (type == elfcpp::PT_TLS)
  4591. this->tls_segment_ = oseg;
  4592. else if (type == elfcpp::PT_GNU_RELRO)
  4593. this->relro_segment_ = oseg;
  4594. else if (type == elfcpp::PT_INTERP)
  4595. this->interp_segment_ = oseg;
  4596. return oseg;
  4597. }
  4598. // Return the file offset of the normal symbol table.
  4599. off_t
  4600. Layout::symtab_section_offset() const
  4601. {
  4602. if (this->symtab_section_ != NULL)
  4603. return this->symtab_section_->offset();
  4604. return 0;
  4605. }
  4606. // Return the section index of the normal symbol table. It may have
  4607. // been stripped by the -s/--strip-all option.
  4608. unsigned int
  4609. Layout::symtab_section_shndx() const
  4610. {
  4611. if (this->symtab_section_ != NULL)
  4612. return this->symtab_section_->out_shndx();
  4613. return 0;
  4614. }
  4615. // Write out the Output_sections. Most won't have anything to write,
  4616. // since most of the data will come from input sections which are
  4617. // handled elsewhere. But some Output_sections do have Output_data.
  4618. void
  4619. Layout::write_output_sections(Output_file* of) const
  4620. {
  4621. for (Section_list::const_iterator p = this->section_list_.begin();
  4622. p != this->section_list_.end();
  4623. ++p)
  4624. {
  4625. if (!(*p)->after_input_sections())
  4626. (*p)->write(of);
  4627. }
  4628. }
  4629. // Write out data not associated with a section or the symbol table.
  4630. void
  4631. Layout::write_data(const Symbol_table* symtab, Output_file* of) const
  4632. {
  4633. if (!parameters->options().strip_all())
  4634. {
  4635. const Output_section* symtab_section = this->symtab_section_;
  4636. for (Section_list::const_iterator p = this->section_list_.begin();
  4637. p != this->section_list_.end();
  4638. ++p)
  4639. {
  4640. if ((*p)->needs_symtab_index())
  4641. {
  4642. gold_assert(symtab_section != NULL);
  4643. unsigned int index = (*p)->symtab_index();
  4644. gold_assert(index > 0 && index != -1U);
  4645. off_t off = (symtab_section->offset()
  4646. + index * symtab_section->entsize());
  4647. symtab->write_section_symbol(*p, this->symtab_xindex_, of, off);
  4648. }
  4649. }
  4650. }
  4651. const Output_section* dynsym_section = this->dynsym_section_;
  4652. for (Section_list::const_iterator p = this->section_list_.begin();
  4653. p != this->section_list_.end();
  4654. ++p)
  4655. {
  4656. if ((*p)->needs_dynsym_index())
  4657. {
  4658. gold_assert(dynsym_section != NULL);
  4659. unsigned int index = (*p)->dynsym_index();
  4660. gold_assert(index > 0 && index != -1U);
  4661. off_t off = (dynsym_section->offset()
  4662. + index * dynsym_section->entsize());
  4663. symtab->write_section_symbol(*p, this->dynsym_xindex_, of, off);
  4664. }
  4665. }
  4666. // Write out the Output_data which are not in an Output_section.
  4667. for (Data_list::const_iterator p = this->special_output_list_.begin();
  4668. p != this->special_output_list_.end();
  4669. ++p)
  4670. (*p)->write(of);
  4671. // Write out the Output_data which are not in an Output_section
  4672. // and are regenerated in each iteration of relaxation.
  4673. for (Data_list::const_iterator p = this->relax_output_list_.begin();
  4674. p != this->relax_output_list_.end();
  4675. ++p)
  4676. (*p)->write(of);
  4677. }
  4678. // Write out the Output_sections which can only be written after the
  4679. // input sections are complete.
  4680. void
  4681. Layout::write_sections_after_input_sections(Output_file* of)
  4682. {
  4683. // Determine the final section offsets, and thus the final output
  4684. // file size. Note we finalize the .shstrab last, to allow the
  4685. // after_input_section sections to modify their section-names before
  4686. // writing.
  4687. if (this->any_postprocessing_sections_)
  4688. {
  4689. off_t off = this->output_file_size_;
  4690. off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
  4691. // Now that we've finalized the names, we can finalize the shstrab.
  4692. off =
  4693. this->set_section_offsets(off,
  4694. STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
  4695. if (off > this->output_file_size_)
  4696. {
  4697. of->resize(off);
  4698. this->output_file_size_ = off;
  4699. }
  4700. }
  4701. for (Section_list::const_iterator p = this->section_list_.begin();
  4702. p != this->section_list_.end();
  4703. ++p)
  4704. {
  4705. if ((*p)->after_input_sections())
  4706. (*p)->write(of);
  4707. }
  4708. this->section_headers_->write(of);
  4709. }
  4710. // If a tree-style build ID was requested, the parallel part of that computation
  4711. // is already done, and the final hash-of-hashes is computed here. For other
  4712. // types of build IDs, all the work is done here.
  4713. void
  4714. Layout::write_build_id(Output_file* of, unsigned char* array_of_hashes,
  4715. size_t size_of_hashes) const
  4716. {
  4717. if (this->build_id_note_ == NULL)
  4718. return;
  4719. unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
  4720. this->build_id_note_->data_size());
  4721. if (array_of_hashes == NULL)
  4722. {
  4723. const size_t output_file_size = this->output_file_size();
  4724. const unsigned char* iv = of->get_input_view(0, output_file_size);
  4725. const char* style = parameters->options().build_id();
  4726. // If we get here with style == "tree" then the output must be
  4727. // too small for chunking, and we use SHA-1 in that case.
  4728. if ((strcmp(style, "sha1") == 0) || (strcmp(style, "tree") == 0))
  4729. sha1_buffer(reinterpret_cast<const char*>(iv), output_file_size, ov);
  4730. else if (strcmp(style, "md5") == 0)
  4731. md5_buffer(reinterpret_cast<const char*>(iv), output_file_size, ov);
  4732. else
  4733. gold_unreachable();
  4734. of->free_input_view(0, output_file_size, iv);
  4735. }
  4736. else
  4737. {
  4738. // Non-overlapping substrings of the output file have been hashed.
  4739. // Compute SHA-1 hash of the hashes.
  4740. sha1_buffer(reinterpret_cast<const char*>(array_of_hashes),
  4741. size_of_hashes, ov);
  4742. delete[] array_of_hashes;
  4743. }
  4744. of->write_output_view(this->build_id_note_->offset(),
  4745. this->build_id_note_->data_size(),
  4746. ov);
  4747. }
  4748. // Write out a binary file. This is called after the link is
  4749. // complete. IN is the temporary output file we used to generate the
  4750. // ELF code. We simply walk through the segments, read them from
  4751. // their file offset in IN, and write them to their load address in
  4752. // the output file. FIXME: with a bit more work, we could support
  4753. // S-records and/or Intel hex format here.
  4754. void
  4755. Layout::write_binary(Output_file* in) const
  4756. {
  4757. gold_assert(parameters->options().oformat_enum()
  4758. == General_options::OBJECT_FORMAT_BINARY);
  4759. // Get the size of the binary file.
  4760. uint64_t max_load_address = 0;
  4761. for (Segment_list::const_iterator p = this->segment_list_.begin();
  4762. p != this->segment_list_.end();
  4763. ++p)
  4764. {
  4765. if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
  4766. {
  4767. uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
  4768. if (max_paddr > max_load_address)
  4769. max_load_address = max_paddr;
  4770. }
  4771. }
  4772. Output_file out(parameters->options().output_file_name());
  4773. out.open(max_load_address);
  4774. for (Segment_list::const_iterator p = this->segment_list_.begin();
  4775. p != this->segment_list_.end();
  4776. ++p)
  4777. {
  4778. if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
  4779. {
  4780. const unsigned char* vin = in->get_input_view((*p)->offset(),
  4781. (*p)->filesz());
  4782. unsigned char* vout = out.get_output_view((*p)->paddr(),
  4783. (*p)->filesz());
  4784. memcpy(vout, vin, (*p)->filesz());
  4785. out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
  4786. in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
  4787. }
  4788. }
  4789. out.close();
  4790. }
  4791. // Print the output sections to the map file.
  4792. void
  4793. Layout::print_to_mapfile(Mapfile* mapfile) const
  4794. {
  4795. for (Segment_list::const_iterator p = this->segment_list_.begin();
  4796. p != this->segment_list_.end();
  4797. ++p)
  4798. (*p)->print_sections_to_mapfile(mapfile);
  4799. for (Section_list::const_iterator p = this->unattached_section_list_.begin();
  4800. p != this->unattached_section_list_.end();
  4801. ++p)
  4802. (*p)->print_to_mapfile(mapfile);
  4803. }
  4804. // Print statistical information to stderr. This is used for --stats.
  4805. void
  4806. Layout::print_stats() const
  4807. {
  4808. this->namepool_.print_stats("section name pool");
  4809. this->sympool_.print_stats("output symbol name pool");
  4810. this->dynpool_.print_stats("dynamic name pool");
  4811. for (Section_list::const_iterator p = this->section_list_.begin();
  4812. p != this->section_list_.end();
  4813. ++p)
  4814. (*p)->print_merge_stats();
  4815. }
  4816. // Write_sections_task methods.
  4817. // We can always run this task.
  4818. Task_token*
  4819. Write_sections_task::is_runnable()
  4820. {
  4821. return NULL;
  4822. }
  4823. // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
  4824. // when finished.
  4825. void
  4826. Write_sections_task::locks(Task_locker* tl)
  4827. {
  4828. tl->add(this, this->output_sections_blocker_);
  4829. if (this->input_sections_blocker_ != NULL)
  4830. tl->add(this, this->input_sections_blocker_);
  4831. tl->add(this, this->final_blocker_);
  4832. }
  4833. // Run the task--write out the data.
  4834. void
  4835. Write_sections_task::run(Workqueue*)
  4836. {
  4837. this->layout_->write_output_sections(this->of_);
  4838. }
  4839. // Write_data_task methods.
  4840. // We can always run this task.
  4841. Task_token*
  4842. Write_data_task::is_runnable()
  4843. {
  4844. return NULL;
  4845. }
  4846. // We need to unlock FINAL_BLOCKER when finished.
  4847. void
  4848. Write_data_task::locks(Task_locker* tl)
  4849. {
  4850. tl->add(this, this->final_blocker_);
  4851. }
  4852. // Run the task--write out the data.
  4853. void
  4854. Write_data_task::run(Workqueue*)
  4855. {
  4856. this->layout_->write_data(this->symtab_, this->of_);
  4857. }
  4858. // Write_symbols_task methods.
  4859. // We can always run this task.
  4860. Task_token*
  4861. Write_symbols_task::is_runnable()
  4862. {
  4863. return NULL;
  4864. }
  4865. // We need to unlock FINAL_BLOCKER when finished.
  4866. void
  4867. Write_symbols_task::locks(Task_locker* tl)
  4868. {
  4869. tl->add(this, this->final_blocker_);
  4870. }
  4871. // Run the task--write out the symbols.
  4872. void
  4873. Write_symbols_task::run(Workqueue*)
  4874. {
  4875. this->symtab_->write_globals(this->sympool_, this->dynpool_,
  4876. this->layout_->symtab_xindex(),
  4877. this->layout_->dynsym_xindex(), this->of_);
  4878. }
  4879. // Write_after_input_sections_task methods.
  4880. // We can only run this task after the input sections have completed.
  4881. Task_token*
  4882. Write_after_input_sections_task::is_runnable()
  4883. {
  4884. if (this->input_sections_blocker_->is_blocked())
  4885. return this->input_sections_blocker_;
  4886. return NULL;
  4887. }
  4888. // We need to unlock FINAL_BLOCKER when finished.
  4889. void
  4890. Write_after_input_sections_task::locks(Task_locker* tl)
  4891. {
  4892. tl->add(this, this->final_blocker_);
  4893. }
  4894. // Run the task.
  4895. void
  4896. Write_after_input_sections_task::run(Workqueue*)
  4897. {
  4898. this->layout_->write_sections_after_input_sections(this->of_);
  4899. }
  4900. // Build IDs can be computed as a "flat" sha1 or md5 of a string of bytes,
  4901. // or as a "tree" where each chunk of the string is hashed and then those
  4902. // hashes are put into a (much smaller) string which is hashed with sha1.
  4903. // We compute a checksum over the entire file because that is simplest.
  4904. void
  4905. Build_id_task_runner::run(Workqueue* workqueue, const Task*)
  4906. {
  4907. Task_token* post_hash_tasks_blocker = new Task_token(true);
  4908. const Layout* layout = this->layout_;
  4909. Output_file* of = this->of_;
  4910. const size_t filesize = (layout->output_file_size() <= 0 ? 0
  4911. : static_cast<size_t>(layout->output_file_size()));
  4912. unsigned char* array_of_hashes = NULL;
  4913. size_t size_of_hashes = 0;
  4914. if (strcmp(this->options_->build_id(), "tree") == 0
  4915. && this->options_->build_id_chunk_size_for_treehash() > 0
  4916. && filesize > 0
  4917. && (filesize >= this->options_->build_id_min_file_size_for_treehash()))
  4918. {
  4919. static const size_t MD5_OUTPUT_SIZE_IN_BYTES = 16;
  4920. const size_t chunk_size =
  4921. this->options_->build_id_chunk_size_for_treehash();
  4922. const size_t num_hashes = ((filesize - 1) / chunk_size) + 1;
  4923. post_hash_tasks_blocker->add_blockers(num_hashes);
  4924. size_of_hashes = num_hashes * MD5_OUTPUT_SIZE_IN_BYTES;
  4925. array_of_hashes = new unsigned char[size_of_hashes];
  4926. unsigned char *dst = array_of_hashes;
  4927. for (size_t i = 0, src_offset = 0; i < num_hashes;
  4928. i++, dst += MD5_OUTPUT_SIZE_IN_BYTES, src_offset += chunk_size)
  4929. {
  4930. size_t size = std::min(chunk_size, filesize - src_offset);
  4931. workqueue->queue(new Hash_task(of,
  4932. src_offset,
  4933. size,
  4934. dst,
  4935. post_hash_tasks_blocker));
  4936. }
  4937. }
  4938. // Queue the final task to write the build id and close the output file.
  4939. workqueue->queue(new Task_function(new Close_task_runner(this->options_,
  4940. layout,
  4941. of,
  4942. array_of_hashes,
  4943. size_of_hashes),
  4944. post_hash_tasks_blocker,
  4945. "Task_function Close_task_runner"));
  4946. }
  4947. // Close_task_runner methods.
  4948. // Finish up the build ID computation, if necessary, and write a binary file,
  4949. // if necessary. Then close the output file.
  4950. void
  4951. Close_task_runner::run(Workqueue*, const Task*)
  4952. {
  4953. // At this point the multi-threaded part of the build ID computation,
  4954. // if any, is done. See Build_id_task_runner.
  4955. this->layout_->write_build_id(this->of_, this->array_of_hashes_,
  4956. this->size_of_hashes_);
  4957. // If we've been asked to create a binary file, we do so here.
  4958. if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
  4959. this->layout_->write_binary(this->of_);
  4960. this->of_->close();
  4961. }
  4962. // Instantiate the templates we need. We could use the configure
  4963. // script to restrict this to only the ones for implemented targets.
  4964. #ifdef HAVE_TARGET_32_LITTLE
  4965. template
  4966. Output_section*
  4967. Layout::init_fixed_output_section<32, false>(
  4968. const char* name,
  4969. elfcpp::Shdr<32, false>& shdr);
  4970. #endif
  4971. #ifdef HAVE_TARGET_32_BIG
  4972. template
  4973. Output_section*
  4974. Layout::init_fixed_output_section<32, true>(
  4975. const char* name,
  4976. elfcpp::Shdr<32, true>& shdr);
  4977. #endif
  4978. #ifdef HAVE_TARGET_64_LITTLE
  4979. template
  4980. Output_section*
  4981. Layout::init_fixed_output_section<64, false>(
  4982. const char* name,
  4983. elfcpp::Shdr<64, false>& shdr);
  4984. #endif
  4985. #ifdef HAVE_TARGET_64_BIG
  4986. template
  4987. Output_section*
  4988. Layout::init_fixed_output_section<64, true>(
  4989. const char* name,
  4990. elfcpp::Shdr<64, true>& shdr);
  4991. #endif
  4992. #ifdef HAVE_TARGET_32_LITTLE
  4993. template
  4994. Output_section*
  4995. Layout::layout<32, false>(Sized_relobj_file<32, false>* object,
  4996. unsigned int shndx,
  4997. const char* name,
  4998. const elfcpp::Shdr<32, false>& shdr,
  4999. unsigned int, unsigned int, off_t*);
  5000. #endif
  5001. #ifdef HAVE_TARGET_32_BIG
  5002. template
  5003. Output_section*
  5004. Layout::layout<32, true>(Sized_relobj_file<32, true>* object,
  5005. unsigned int shndx,
  5006. const char* name,
  5007. const elfcpp::Shdr<32, true>& shdr,
  5008. unsigned int, unsigned int, off_t*);
  5009. #endif
  5010. #ifdef HAVE_TARGET_64_LITTLE
  5011. template
  5012. Output_section*
  5013. Layout::layout<64, false>(Sized_relobj_file<64, false>* object,
  5014. unsigned int shndx,
  5015. const char* name,
  5016. const elfcpp::Shdr<64, false>& shdr,
  5017. unsigned int, unsigned int, off_t*);
  5018. #endif
  5019. #ifdef HAVE_TARGET_64_BIG
  5020. template
  5021. Output_section*
  5022. Layout::layout<64, true>(Sized_relobj_file<64, true>* object,
  5023. unsigned int shndx,
  5024. const char* name,
  5025. const elfcpp::Shdr<64, true>& shdr,
  5026. unsigned int, unsigned int, off_t*);
  5027. #endif
  5028. #ifdef HAVE_TARGET_32_LITTLE
  5029. template
  5030. Output_section*
  5031. Layout::layout_reloc<32, false>(Sized_relobj_file<32, false>* object,
  5032. unsigned int reloc_shndx,
  5033. const elfcpp::Shdr<32, false>& shdr,
  5034. Output_section* data_section,
  5035. Relocatable_relocs* rr);
  5036. #endif
  5037. #ifdef HAVE_TARGET_32_BIG
  5038. template
  5039. Output_section*
  5040. Layout::layout_reloc<32, true>(Sized_relobj_file<32, true>* object,
  5041. unsigned int reloc_shndx,
  5042. const elfcpp::Shdr<32, true>& shdr,
  5043. Output_section* data_section,
  5044. Relocatable_relocs* rr);
  5045. #endif
  5046. #ifdef HAVE_TARGET_64_LITTLE
  5047. template
  5048. Output_section*
  5049. Layout::layout_reloc<64, false>(Sized_relobj_file<64, false>* object,
  5050. unsigned int reloc_shndx,
  5051. const elfcpp::Shdr<64, false>& shdr,
  5052. Output_section* data_section,
  5053. Relocatable_relocs* rr);
  5054. #endif
  5055. #ifdef HAVE_TARGET_64_BIG
  5056. template
  5057. Output_section*
  5058. Layout::layout_reloc<64, true>(Sized_relobj_file<64, true>* object,
  5059. unsigned int reloc_shndx,
  5060. const elfcpp::Shdr<64, true>& shdr,
  5061. Output_section* data_section,
  5062. Relocatable_relocs* rr);
  5063. #endif
  5064. #ifdef HAVE_TARGET_32_LITTLE
  5065. template
  5066. void
  5067. Layout::layout_group<32, false>(Symbol_table* symtab,
  5068. Sized_relobj_file<32, false>* object,
  5069. unsigned int,
  5070. const char* group_section_name,
  5071. const char* signature,
  5072. const elfcpp::Shdr<32, false>& shdr,
  5073. elfcpp::Elf_Word flags,
  5074. std::vector<unsigned int>* shndxes);
  5075. #endif
  5076. #ifdef HAVE_TARGET_32_BIG
  5077. template
  5078. void
  5079. Layout::layout_group<32, true>(Symbol_table* symtab,
  5080. Sized_relobj_file<32, true>* object,
  5081. unsigned int,
  5082. const char* group_section_name,
  5083. const char* signature,
  5084. const elfcpp::Shdr<32, true>& shdr,
  5085. elfcpp::Elf_Word flags,
  5086. std::vector<unsigned int>* shndxes);
  5087. #endif
  5088. #ifdef HAVE_TARGET_64_LITTLE
  5089. template
  5090. void
  5091. Layout::layout_group<64, false>(Symbol_table* symtab,
  5092. Sized_relobj_file<64, false>* object,
  5093. unsigned int,
  5094. const char* group_section_name,
  5095. const char* signature,
  5096. const elfcpp::Shdr<64, false>& shdr,
  5097. elfcpp::Elf_Word flags,
  5098. std::vector<unsigned int>* shndxes);
  5099. #endif
  5100. #ifdef HAVE_TARGET_64_BIG
  5101. template
  5102. void
  5103. Layout::layout_group<64, true>(Symbol_table* symtab,
  5104. Sized_relobj_file<64, true>* object,
  5105. unsigned int,
  5106. const char* group_section_name,
  5107. const char* signature,
  5108. const elfcpp::Shdr<64, true>& shdr,
  5109. elfcpp::Elf_Word flags,
  5110. std::vector<unsigned int>* shndxes);
  5111. #endif
  5112. #ifdef HAVE_TARGET_32_LITTLE
  5113. template
  5114. Output_section*
  5115. Layout::layout_eh_frame<32, false>(Sized_relobj_file<32, false>* object,
  5116. const unsigned char* symbols,
  5117. off_t symbols_size,
  5118. const unsigned char* symbol_names,
  5119. off_t symbol_names_size,
  5120. unsigned int shndx,
  5121. const elfcpp::Shdr<32, false>& shdr,
  5122. unsigned int reloc_shndx,
  5123. unsigned int reloc_type,
  5124. off_t* off);
  5125. #endif
  5126. #ifdef HAVE_TARGET_32_BIG
  5127. template
  5128. Output_section*
  5129. Layout::layout_eh_frame<32, true>(Sized_relobj_file<32, true>* object,
  5130. const unsigned char* symbols,
  5131. off_t symbols_size,
  5132. const unsigned char* symbol_names,
  5133. off_t symbol_names_size,
  5134. unsigned int shndx,
  5135. const elfcpp::Shdr<32, true>& shdr,
  5136. unsigned int reloc_shndx,
  5137. unsigned int reloc_type,
  5138. off_t* off);
  5139. #endif
  5140. #ifdef HAVE_TARGET_64_LITTLE
  5141. template
  5142. Output_section*
  5143. Layout::layout_eh_frame<64, false>(Sized_relobj_file<64, false>* object,
  5144. const unsigned char* symbols,
  5145. off_t symbols_size,
  5146. const unsigned char* symbol_names,
  5147. off_t symbol_names_size,
  5148. unsigned int shndx,
  5149. const elfcpp::Shdr<64, false>& shdr,
  5150. unsigned int reloc_shndx,
  5151. unsigned int reloc_type,
  5152. off_t* off);
  5153. #endif
  5154. #ifdef HAVE_TARGET_64_BIG
  5155. template
  5156. Output_section*
  5157. Layout::layout_eh_frame<64, true>(Sized_relobj_file<64, true>* object,
  5158. const unsigned char* symbols,
  5159. off_t symbols_size,
  5160. const unsigned char* symbol_names,
  5161. off_t symbol_names_size,
  5162. unsigned int shndx,
  5163. const elfcpp::Shdr<64, true>& shdr,
  5164. unsigned int reloc_shndx,
  5165. unsigned int reloc_type,
  5166. off_t* off);
  5167. #endif
  5168. #ifdef HAVE_TARGET_32_LITTLE
  5169. template
  5170. void
  5171. Layout::add_to_gdb_index(bool is_type_unit,
  5172. Sized_relobj<32, false>* object,
  5173. const unsigned char* symbols,
  5174. off_t symbols_size,
  5175. unsigned int shndx,
  5176. unsigned int reloc_shndx,
  5177. unsigned int reloc_type);
  5178. #endif
  5179. #ifdef HAVE_TARGET_32_BIG
  5180. template
  5181. void
  5182. Layout::add_to_gdb_index(bool is_type_unit,
  5183. Sized_relobj<32, true>* object,
  5184. const unsigned char* symbols,
  5185. off_t symbols_size,
  5186. unsigned int shndx,
  5187. unsigned int reloc_shndx,
  5188. unsigned int reloc_type);
  5189. #endif
  5190. #ifdef HAVE_TARGET_64_LITTLE
  5191. template
  5192. void
  5193. Layout::add_to_gdb_index(bool is_type_unit,
  5194. Sized_relobj<64, false>* object,
  5195. const unsigned char* symbols,
  5196. off_t symbols_size,
  5197. unsigned int shndx,
  5198. unsigned int reloc_shndx,
  5199. unsigned int reloc_type);
  5200. #endif
  5201. #ifdef HAVE_TARGET_64_BIG
  5202. template
  5203. void
  5204. Layout::add_to_gdb_index(bool is_type_unit,
  5205. Sized_relobj<64, true>* object,
  5206. const unsigned char* symbols,
  5207. off_t symbols_size,
  5208. unsigned int shndx,
  5209. unsigned int reloc_shndx,
  5210. unsigned int reloc_type);
  5211. #endif
  5212. } // End namespace gold.