output.cc 154 KB

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