output.h 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846
  1. // output.h -- manage the output file for gold -*- C++ -*-
  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. #ifndef GOLD_OUTPUT_H
  18. #define GOLD_OUTPUT_H
  19. #include <list>
  20. #include <vector>
  21. #include "elfcpp.h"
  22. #include "mapfile.h"
  23. #include "layout.h"
  24. #include "reloc-types.h"
  25. namespace gold
  26. {
  27. class General_options;
  28. class Object;
  29. class Symbol;
  30. class Output_file;
  31. class Output_merge_base;
  32. class Output_section;
  33. class Relocatable_relocs;
  34. class Target;
  35. template<int size, bool big_endian>
  36. class Sized_target;
  37. template<int size, bool big_endian>
  38. class Sized_relobj;
  39. template<int size, bool big_endian>
  40. class Sized_relobj_file;
  41. // An abtract class for data which has to go into the output file.
  42. class Output_data
  43. {
  44. public:
  45. explicit Output_data()
  46. : address_(0), data_size_(0), offset_(-1),
  47. is_address_valid_(false), is_data_size_valid_(false),
  48. is_offset_valid_(false), is_data_size_fixed_(false),
  49. has_dynamic_reloc_(false)
  50. { }
  51. virtual
  52. ~Output_data();
  53. // Return the address. For allocated sections, this is only valid
  54. // after Layout::finalize is finished.
  55. uint64_t
  56. address() const
  57. {
  58. gold_assert(this->is_address_valid_);
  59. return this->address_;
  60. }
  61. // Return the size of the data. For allocated sections, this must
  62. // be valid after Layout::finalize calls set_address, but need not
  63. // be valid before then.
  64. off_t
  65. data_size() const
  66. {
  67. gold_assert(this->is_data_size_valid_);
  68. return this->data_size_;
  69. }
  70. // Get the current data size.
  71. off_t
  72. current_data_size() const
  73. { return this->current_data_size_for_child(); }
  74. // Return true if data size is fixed.
  75. bool
  76. is_data_size_fixed() const
  77. { return this->is_data_size_fixed_; }
  78. // Return the file offset. This is only valid after
  79. // Layout::finalize is finished. For some non-allocated sections,
  80. // it may not be valid until near the end of the link.
  81. off_t
  82. offset() const
  83. {
  84. gold_assert(this->is_offset_valid_);
  85. return this->offset_;
  86. }
  87. // Reset the address, file offset and data size. This essentially
  88. // disables the sanity testing about duplicate and unknown settings.
  89. void
  90. reset_address_and_file_offset()
  91. {
  92. this->is_address_valid_ = false;
  93. this->is_offset_valid_ = false;
  94. if (!this->is_data_size_fixed_)
  95. this->is_data_size_valid_ = false;
  96. this->do_reset_address_and_file_offset();
  97. }
  98. // As above, but just for data size.
  99. void
  100. reset_data_size()
  101. {
  102. if (!this->is_data_size_fixed_)
  103. this->is_data_size_valid_ = false;
  104. }
  105. // Return true if address and file offset already have reset values. In
  106. // other words, calling reset_address_and_file_offset will not change them.
  107. bool
  108. address_and_file_offset_have_reset_values() const
  109. { return this->do_address_and_file_offset_have_reset_values(); }
  110. // Return the required alignment.
  111. uint64_t
  112. addralign() const
  113. { return this->do_addralign(); }
  114. // Return whether this has a load address.
  115. bool
  116. has_load_address() const
  117. { return this->do_has_load_address(); }
  118. // Return the load address.
  119. uint64_t
  120. load_address() const
  121. { return this->do_load_address(); }
  122. // Return whether this is an Output_section.
  123. bool
  124. is_section() const
  125. { return this->do_is_section(); }
  126. // Return whether this is an Output_section of the specified type.
  127. bool
  128. is_section_type(elfcpp::Elf_Word stt) const
  129. { return this->do_is_section_type(stt); }
  130. // Return whether this is an Output_section with the specified flag
  131. // set.
  132. bool
  133. is_section_flag_set(elfcpp::Elf_Xword shf) const
  134. { return this->do_is_section_flag_set(shf); }
  135. // Return the output section that this goes in, if there is one.
  136. Output_section*
  137. output_section()
  138. { return this->do_output_section(); }
  139. const Output_section*
  140. output_section() const
  141. { return this->do_output_section(); }
  142. // Return the output section index, if there is an output section.
  143. unsigned int
  144. out_shndx() const
  145. { return this->do_out_shndx(); }
  146. // Set the output section index, if this is an output section.
  147. void
  148. set_out_shndx(unsigned int shndx)
  149. { this->do_set_out_shndx(shndx); }
  150. // Set the address and file offset of this data, and finalize the
  151. // size of the data. This is called during Layout::finalize for
  152. // allocated sections.
  153. void
  154. set_address_and_file_offset(uint64_t addr, off_t off)
  155. {
  156. this->set_address(addr);
  157. this->set_file_offset(off);
  158. this->finalize_data_size();
  159. }
  160. // Set the address.
  161. void
  162. set_address(uint64_t addr)
  163. {
  164. gold_assert(!this->is_address_valid_);
  165. this->address_ = addr;
  166. this->is_address_valid_ = true;
  167. }
  168. // Set the file offset.
  169. void
  170. set_file_offset(off_t off)
  171. {
  172. gold_assert(!this->is_offset_valid_);
  173. this->offset_ = off;
  174. this->is_offset_valid_ = true;
  175. }
  176. // Update the data size without finalizing it.
  177. void
  178. pre_finalize_data_size()
  179. {
  180. if (!this->is_data_size_valid_)
  181. {
  182. // Tell the child class to update the data size.
  183. this->update_data_size();
  184. }
  185. }
  186. // Finalize the data size.
  187. void
  188. finalize_data_size()
  189. {
  190. if (!this->is_data_size_valid_)
  191. {
  192. // Tell the child class to set the data size.
  193. this->set_final_data_size();
  194. gold_assert(this->is_data_size_valid_);
  195. }
  196. }
  197. // Set the TLS offset. Called only for SHT_TLS sections.
  198. void
  199. set_tls_offset(uint64_t tls_base)
  200. { this->do_set_tls_offset(tls_base); }
  201. // Return the TLS offset, relative to the base of the TLS segment.
  202. // Valid only for SHT_TLS sections.
  203. uint64_t
  204. tls_offset() const
  205. { return this->do_tls_offset(); }
  206. // Write the data to the output file. This is called after
  207. // Layout::finalize is complete.
  208. void
  209. write(Output_file* file)
  210. { this->do_write(file); }
  211. // This is called by Layout::finalize to note that the sizes of
  212. // allocated sections must now be fixed.
  213. static void
  214. layout_complete()
  215. { Output_data::allocated_sizes_are_fixed = true; }
  216. // Used to check that layout has been done.
  217. static bool
  218. is_layout_complete()
  219. { return Output_data::allocated_sizes_are_fixed; }
  220. // Note that a dynamic reloc has been applied to this data.
  221. void
  222. add_dynamic_reloc()
  223. { this->has_dynamic_reloc_ = true; }
  224. // Return whether a dynamic reloc has been applied.
  225. bool
  226. has_dynamic_reloc() const
  227. { return this->has_dynamic_reloc_; }
  228. // Whether the address is valid.
  229. bool
  230. is_address_valid() const
  231. { return this->is_address_valid_; }
  232. // Whether the file offset is valid.
  233. bool
  234. is_offset_valid() const
  235. { return this->is_offset_valid_; }
  236. // Whether the data size is valid.
  237. bool
  238. is_data_size_valid() const
  239. { return this->is_data_size_valid_; }
  240. // Print information to the map file.
  241. void
  242. print_to_mapfile(Mapfile* mapfile) const
  243. { return this->do_print_to_mapfile(mapfile); }
  244. protected:
  245. // Functions that child classes may or in some cases must implement.
  246. // Write the data to the output file.
  247. virtual void
  248. do_write(Output_file*) = 0;
  249. // Return the required alignment.
  250. virtual uint64_t
  251. do_addralign() const = 0;
  252. // Return whether this has a load address.
  253. virtual bool
  254. do_has_load_address() const
  255. { return false; }
  256. // Return the load address.
  257. virtual uint64_t
  258. do_load_address() const
  259. { gold_unreachable(); }
  260. // Return whether this is an Output_section.
  261. virtual bool
  262. do_is_section() const
  263. { return false; }
  264. // Return whether this is an Output_section of the specified type.
  265. // This only needs to be implement by Output_section.
  266. virtual bool
  267. do_is_section_type(elfcpp::Elf_Word) const
  268. { return false; }
  269. // Return whether this is an Output_section with the specific flag
  270. // set. This only needs to be implemented by Output_section.
  271. virtual bool
  272. do_is_section_flag_set(elfcpp::Elf_Xword) const
  273. { return false; }
  274. // Return the output section, if there is one.
  275. virtual Output_section*
  276. do_output_section()
  277. { return NULL; }
  278. virtual const Output_section*
  279. do_output_section() const
  280. { return NULL; }
  281. // Return the output section index, if there is an output section.
  282. virtual unsigned int
  283. do_out_shndx() const
  284. { gold_unreachable(); }
  285. // Set the output section index, if this is an output section.
  286. virtual void
  287. do_set_out_shndx(unsigned int)
  288. { gold_unreachable(); }
  289. // This is a hook for derived classes to set the preliminary data size.
  290. // This is called by pre_finalize_data_size, normally called during
  291. // Layout::finalize, before the section address is set, and is used
  292. // during an incremental update, when we need to know the size of a
  293. // section before allocating space in the output file. For classes
  294. // where the current data size is up to date, this default version of
  295. // the method can be inherited.
  296. virtual void
  297. update_data_size()
  298. { }
  299. // This is a hook for derived classes to set the data size. This is
  300. // called by finalize_data_size, normally called during
  301. // Layout::finalize, when the section address is set.
  302. virtual void
  303. set_final_data_size()
  304. { gold_unreachable(); }
  305. // A hook for resetting the address and file offset.
  306. virtual void
  307. do_reset_address_and_file_offset()
  308. { }
  309. // Return true if address and file offset already have reset values. In
  310. // other words, calling reset_address_and_file_offset will not change them.
  311. // A child class overriding do_reset_address_and_file_offset may need to
  312. // also override this.
  313. virtual bool
  314. do_address_and_file_offset_have_reset_values() const
  315. { return !this->is_address_valid_ && !this->is_offset_valid_; }
  316. // Set the TLS offset. Called only for SHT_TLS sections.
  317. virtual void
  318. do_set_tls_offset(uint64_t)
  319. { gold_unreachable(); }
  320. // Return the TLS offset, relative to the base of the TLS segment.
  321. // Valid only for SHT_TLS sections.
  322. virtual uint64_t
  323. do_tls_offset() const
  324. { gold_unreachable(); }
  325. // Print to the map file. This only needs to be implemented by
  326. // classes which may appear in a PT_LOAD segment.
  327. virtual void
  328. do_print_to_mapfile(Mapfile*) const
  329. { gold_unreachable(); }
  330. // Functions that child classes may call.
  331. // Reset the address. The Output_section class needs this when an
  332. // SHF_ALLOC input section is added to an output section which was
  333. // formerly not SHF_ALLOC.
  334. void
  335. mark_address_invalid()
  336. { this->is_address_valid_ = false; }
  337. // Set the size of the data.
  338. void
  339. set_data_size(off_t data_size)
  340. {
  341. gold_assert(!this->is_data_size_valid_
  342. && !this->is_data_size_fixed_);
  343. this->data_size_ = data_size;
  344. this->is_data_size_valid_ = true;
  345. }
  346. // Fix the data size. Once it is fixed, it cannot be changed
  347. // and the data size remains always valid.
  348. void
  349. fix_data_size()
  350. {
  351. gold_assert(this->is_data_size_valid_);
  352. this->is_data_size_fixed_ = true;
  353. }
  354. // Get the current data size--this is for the convenience of
  355. // sections which build up their size over time.
  356. off_t
  357. current_data_size_for_child() const
  358. { return this->data_size_; }
  359. // Set the current data size--this is for the convenience of
  360. // sections which build up their size over time.
  361. void
  362. set_current_data_size_for_child(off_t data_size)
  363. {
  364. gold_assert(!this->is_data_size_valid_);
  365. this->data_size_ = data_size;
  366. }
  367. // Return default alignment for the target size.
  368. static uint64_t
  369. default_alignment();
  370. // Return default alignment for a specified size--32 or 64.
  371. static uint64_t
  372. default_alignment_for_size(int size);
  373. private:
  374. Output_data(const Output_data&);
  375. Output_data& operator=(const Output_data&);
  376. // This is used for verification, to make sure that we don't try to
  377. // change any sizes of allocated sections after we set the section
  378. // addresses.
  379. static bool allocated_sizes_are_fixed;
  380. // Memory address in output file.
  381. uint64_t address_;
  382. // Size of data in output file.
  383. off_t data_size_;
  384. // File offset of contents in output file.
  385. off_t offset_;
  386. // Whether address_ is valid.
  387. bool is_address_valid_ : 1;
  388. // Whether data_size_ is valid.
  389. bool is_data_size_valid_ : 1;
  390. // Whether offset_ is valid.
  391. bool is_offset_valid_ : 1;
  392. // Whether data size is fixed.
  393. bool is_data_size_fixed_ : 1;
  394. // Whether any dynamic relocs have been applied to this section.
  395. bool has_dynamic_reloc_ : 1;
  396. };
  397. // Output the section headers.
  398. class Output_section_headers : public Output_data
  399. {
  400. public:
  401. Output_section_headers(const Layout*,
  402. const Layout::Segment_list*,
  403. const Layout::Section_list*,
  404. const Layout::Section_list*,
  405. const Stringpool*,
  406. const Output_section*);
  407. protected:
  408. // Write the data to the file.
  409. void
  410. do_write(Output_file*);
  411. // Return the required alignment.
  412. uint64_t
  413. do_addralign() const
  414. { return Output_data::default_alignment(); }
  415. // Write to a map file.
  416. void
  417. do_print_to_mapfile(Mapfile* mapfile) const
  418. { mapfile->print_output_data(this, _("** section headers")); }
  419. // Update the data size.
  420. void
  421. update_data_size()
  422. { this->set_data_size(this->do_size()); }
  423. // Set final data size.
  424. void
  425. set_final_data_size()
  426. { this->set_data_size(this->do_size()); }
  427. private:
  428. // Write the data to the file with the right size and endianness.
  429. template<int size, bool big_endian>
  430. void
  431. do_sized_write(Output_file*);
  432. // Compute data size.
  433. off_t
  434. do_size() const;
  435. const Layout* layout_;
  436. const Layout::Segment_list* segment_list_;
  437. const Layout::Section_list* section_list_;
  438. const Layout::Section_list* unattached_section_list_;
  439. const Stringpool* secnamepool_;
  440. const Output_section* shstrtab_section_;
  441. };
  442. // Output the segment headers.
  443. class Output_segment_headers : public Output_data
  444. {
  445. public:
  446. Output_segment_headers(const Layout::Segment_list& segment_list);
  447. protected:
  448. // Write the data to the file.
  449. void
  450. do_write(Output_file*);
  451. // Return the required alignment.
  452. uint64_t
  453. do_addralign() const
  454. { return Output_data::default_alignment(); }
  455. // Write to a map file.
  456. void
  457. do_print_to_mapfile(Mapfile* mapfile) const
  458. { mapfile->print_output_data(this, _("** segment headers")); }
  459. // Set final data size.
  460. void
  461. set_final_data_size()
  462. { this->set_data_size(this->do_size()); }
  463. private:
  464. // Write the data to the file with the right size and endianness.
  465. template<int size, bool big_endian>
  466. void
  467. do_sized_write(Output_file*);
  468. // Compute the current size.
  469. off_t
  470. do_size() const;
  471. const Layout::Segment_list& segment_list_;
  472. };
  473. // Output the ELF file header.
  474. class Output_file_header : public Output_data
  475. {
  476. public:
  477. Output_file_header(Target*,
  478. const Symbol_table*,
  479. const Output_segment_headers*);
  480. // Add information about the section headers. We lay out the ELF
  481. // file header before we create the section headers.
  482. void set_section_info(const Output_section_headers*,
  483. const Output_section* shstrtab);
  484. protected:
  485. // Write the data to the file.
  486. void
  487. do_write(Output_file*);
  488. // Return the required alignment.
  489. uint64_t
  490. do_addralign() const
  491. { return Output_data::default_alignment(); }
  492. // Write to a map file.
  493. void
  494. do_print_to_mapfile(Mapfile* mapfile) const
  495. { mapfile->print_output_data(this, _("** file header")); }
  496. // Set final data size.
  497. void
  498. set_final_data_size(void)
  499. { this->set_data_size(this->do_size()); }
  500. private:
  501. // Write the data to the file with the right size and endianness.
  502. template<int size, bool big_endian>
  503. void
  504. do_sized_write(Output_file*);
  505. // Return the value to use for the entry address.
  506. template<int size>
  507. typename elfcpp::Elf_types<size>::Elf_Addr
  508. entry();
  509. // Compute the current data size.
  510. off_t
  511. do_size() const;
  512. Target* target_;
  513. const Symbol_table* symtab_;
  514. const Output_segment_headers* segment_header_;
  515. const Output_section_headers* section_header_;
  516. const Output_section* shstrtab_;
  517. };
  518. // Output sections are mainly comprised of input sections. However,
  519. // there are cases where we have data to write out which is not in an
  520. // input section. Output_section_data is used in such cases. This is
  521. // an abstract base class.
  522. class Output_section_data : public Output_data
  523. {
  524. public:
  525. Output_section_data(off_t data_size, uint64_t addralign,
  526. bool is_data_size_fixed)
  527. : Output_data(), output_section_(NULL), addralign_(addralign)
  528. {
  529. this->set_data_size(data_size);
  530. if (is_data_size_fixed)
  531. this->fix_data_size();
  532. }
  533. Output_section_data(uint64_t addralign)
  534. : Output_data(), output_section_(NULL), addralign_(addralign)
  535. { }
  536. // Return the output section.
  537. Output_section*
  538. output_section()
  539. { return this->output_section_; }
  540. const Output_section*
  541. output_section() const
  542. { return this->output_section_; }
  543. // Record the output section.
  544. void
  545. set_output_section(Output_section* os);
  546. // Add an input section, for SHF_MERGE sections. This returns true
  547. // if the section was handled.
  548. bool
  549. add_input_section(Relobj* object, unsigned int shndx)
  550. { return this->do_add_input_section(object, shndx); }
  551. // Given an input OBJECT, an input section index SHNDX within that
  552. // object, and an OFFSET relative to the start of that input
  553. // section, return whether or not the corresponding offset within
  554. // the output section is known. If this function returns true, it
  555. // sets *POUTPUT to the output offset. The value -1 indicates that
  556. // this input offset is being discarded.
  557. bool
  558. output_offset(const Relobj* object, unsigned int shndx,
  559. section_offset_type offset,
  560. section_offset_type* poutput) const
  561. { return this->do_output_offset(object, shndx, offset, poutput); }
  562. // Write the contents to a buffer. This is used for sections which
  563. // require postprocessing, such as compression.
  564. void
  565. write_to_buffer(unsigned char* buffer)
  566. { this->do_write_to_buffer(buffer); }
  567. // Print merge stats to stderr. This should only be called for
  568. // SHF_MERGE sections.
  569. void
  570. print_merge_stats(const char* section_name)
  571. { this->do_print_merge_stats(section_name); }
  572. protected:
  573. // The child class must implement do_write.
  574. // The child class may implement specific adjustments to the output
  575. // section.
  576. virtual void
  577. do_adjust_output_section(Output_section*)
  578. { }
  579. // May be implemented by child class. Return true if the section
  580. // was handled.
  581. virtual bool
  582. do_add_input_section(Relobj*, unsigned int)
  583. { gold_unreachable(); }
  584. // The child class may implement output_offset.
  585. virtual bool
  586. do_output_offset(const Relobj*, unsigned int, section_offset_type,
  587. section_offset_type*) const
  588. { return false; }
  589. // The child class may implement write_to_buffer. Most child
  590. // classes can not appear in a compressed section, and they do not
  591. // implement this.
  592. virtual void
  593. do_write_to_buffer(unsigned char*)
  594. { gold_unreachable(); }
  595. // Print merge statistics.
  596. virtual void
  597. do_print_merge_stats(const char*)
  598. { gold_unreachable(); }
  599. // Return the required alignment.
  600. uint64_t
  601. do_addralign() const
  602. { return this->addralign_; }
  603. // Return the output section.
  604. Output_section*
  605. do_output_section()
  606. { return this->output_section_; }
  607. const Output_section*
  608. do_output_section() const
  609. { return this->output_section_; }
  610. // Return the section index of the output section.
  611. unsigned int
  612. do_out_shndx() const;
  613. // Set the alignment.
  614. void
  615. set_addralign(uint64_t addralign);
  616. private:
  617. // The output section for this section.
  618. Output_section* output_section_;
  619. // The required alignment.
  620. uint64_t addralign_;
  621. };
  622. // Some Output_section_data classes build up their data step by step,
  623. // rather than all at once. This class provides an interface for
  624. // them.
  625. class Output_section_data_build : public Output_section_data
  626. {
  627. public:
  628. Output_section_data_build(uint64_t addralign)
  629. : Output_section_data(addralign)
  630. { }
  631. Output_section_data_build(off_t data_size, uint64_t addralign)
  632. : Output_section_data(data_size, addralign, false)
  633. { }
  634. // Set the current data size.
  635. void
  636. set_current_data_size(off_t data_size)
  637. { this->set_current_data_size_for_child(data_size); }
  638. protected:
  639. // Set the final data size.
  640. virtual void
  641. set_final_data_size()
  642. { this->set_data_size(this->current_data_size_for_child()); }
  643. };
  644. // A simple case of Output_data in which we have constant data to
  645. // output.
  646. class Output_data_const : public Output_section_data
  647. {
  648. public:
  649. Output_data_const(const std::string& data, uint64_t addralign)
  650. : Output_section_data(data.size(), addralign, true), data_(data)
  651. { }
  652. Output_data_const(const char* p, off_t len, uint64_t addralign)
  653. : Output_section_data(len, addralign, true), data_(p, len)
  654. { }
  655. Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
  656. : Output_section_data(len, addralign, true),
  657. data_(reinterpret_cast<const char*>(p), len)
  658. { }
  659. protected:
  660. // Write the data to the output file.
  661. void
  662. do_write(Output_file*);
  663. // Write the data to a buffer.
  664. void
  665. do_write_to_buffer(unsigned char* buffer)
  666. { memcpy(buffer, this->data_.data(), this->data_.size()); }
  667. // Write to a map file.
  668. void
  669. do_print_to_mapfile(Mapfile* mapfile) const
  670. { mapfile->print_output_data(this, _("** fill")); }
  671. private:
  672. std::string data_;
  673. };
  674. // Another version of Output_data with constant data, in which the
  675. // buffer is allocated by the caller.
  676. class Output_data_const_buffer : public Output_section_data
  677. {
  678. public:
  679. Output_data_const_buffer(const unsigned char* p, off_t len,
  680. uint64_t addralign, const char* map_name)
  681. : Output_section_data(len, addralign, true),
  682. p_(p), map_name_(map_name)
  683. { }
  684. protected:
  685. // Write the data the output file.
  686. void
  687. do_write(Output_file*);
  688. // Write the data to a buffer.
  689. void
  690. do_write_to_buffer(unsigned char* buffer)
  691. { memcpy(buffer, this->p_, this->data_size()); }
  692. // Write to a map file.
  693. void
  694. do_print_to_mapfile(Mapfile* mapfile) const
  695. { mapfile->print_output_data(this, _(this->map_name_)); }
  696. private:
  697. // The data to output.
  698. const unsigned char* p_;
  699. // Name to use in a map file. Maps are a rarely used feature, but
  700. // the space usage is minor as aren't very many of these objects.
  701. const char* map_name_;
  702. };
  703. // A place holder for a fixed amount of data written out via some
  704. // other mechanism.
  705. class Output_data_fixed_space : public Output_section_data
  706. {
  707. public:
  708. Output_data_fixed_space(off_t data_size, uint64_t addralign,
  709. const char* map_name)
  710. : Output_section_data(data_size, addralign, true),
  711. map_name_(map_name)
  712. { }
  713. protected:
  714. // Write out the data--the actual data must be written out
  715. // elsewhere.
  716. void
  717. do_write(Output_file*)
  718. { }
  719. // Write to a map file.
  720. void
  721. do_print_to_mapfile(Mapfile* mapfile) const
  722. { mapfile->print_output_data(this, _(this->map_name_)); }
  723. private:
  724. // Name to use in a map file. Maps are a rarely used feature, but
  725. // the space usage is minor as aren't very many of these objects.
  726. const char* map_name_;
  727. };
  728. // A place holder for variable sized data written out via some other
  729. // mechanism.
  730. class Output_data_space : public Output_section_data_build
  731. {
  732. public:
  733. explicit Output_data_space(uint64_t addralign, const char* map_name)
  734. : Output_section_data_build(addralign),
  735. map_name_(map_name)
  736. { }
  737. explicit Output_data_space(off_t data_size, uint64_t addralign,
  738. const char* map_name)
  739. : Output_section_data_build(data_size, addralign),
  740. map_name_(map_name)
  741. { }
  742. // Set the alignment.
  743. void
  744. set_space_alignment(uint64_t align)
  745. { this->set_addralign(align); }
  746. protected:
  747. // Write out the data--the actual data must be written out
  748. // elsewhere.
  749. void
  750. do_write(Output_file*)
  751. { }
  752. // Write to a map file.
  753. void
  754. do_print_to_mapfile(Mapfile* mapfile) const
  755. { mapfile->print_output_data(this, _(this->map_name_)); }
  756. private:
  757. // Name to use in a map file. Maps are a rarely used feature, but
  758. // the space usage is minor as aren't very many of these objects.
  759. const char* map_name_;
  760. };
  761. // Fill fixed space with zeroes. This is just like
  762. // Output_data_fixed_space, except that the map name is known.
  763. class Output_data_zero_fill : public Output_section_data
  764. {
  765. public:
  766. Output_data_zero_fill(off_t data_size, uint64_t addralign)
  767. : Output_section_data(data_size, addralign, true)
  768. { }
  769. protected:
  770. // There is no data to write out.
  771. void
  772. do_write(Output_file*)
  773. { }
  774. // Write to a map file.
  775. void
  776. do_print_to_mapfile(Mapfile* mapfile) const
  777. { mapfile->print_output_data(this, "** zero fill"); }
  778. };
  779. // A string table which goes into an output section.
  780. class Output_data_strtab : public Output_section_data
  781. {
  782. public:
  783. Output_data_strtab(Stringpool* strtab)
  784. : Output_section_data(1), strtab_(strtab)
  785. { }
  786. protected:
  787. // This is called to update the section size prior to assigning
  788. // the address and file offset.
  789. void
  790. update_data_size()
  791. { this->set_final_data_size(); }
  792. // This is called to set the address and file offset. Here we make
  793. // sure that the Stringpool is finalized.
  794. void
  795. set_final_data_size();
  796. // Write out the data.
  797. void
  798. do_write(Output_file*);
  799. // Write the data to a buffer.
  800. void
  801. do_write_to_buffer(unsigned char* buffer)
  802. { this->strtab_->write_to_buffer(buffer, this->data_size()); }
  803. // Write to a map file.
  804. void
  805. do_print_to_mapfile(Mapfile* mapfile) const
  806. { mapfile->print_output_data(this, _("** string table")); }
  807. private:
  808. Stringpool* strtab_;
  809. };
  810. // This POD class is used to represent a single reloc in the output
  811. // file. This could be a private class within Output_data_reloc, but
  812. // the templatization is complex enough that I broke it out into a
  813. // separate class. The class is templatized on either elfcpp::SHT_REL
  814. // or elfcpp::SHT_RELA, and also on whether this is a dynamic
  815. // relocation or an ordinary relocation.
  816. // A relocation can be against a global symbol, a local symbol, a
  817. // local section symbol, an output section, or the undefined symbol at
  818. // index 0. We represent the latter by using a NULL global symbol.
  819. template<int sh_type, bool dynamic, int size, bool big_endian>
  820. class Output_reloc;
  821. template<bool dynamic, int size, bool big_endian>
  822. class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
  823. {
  824. public:
  825. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  826. typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
  827. static const Address invalid_address = static_cast<Address>(0) - 1;
  828. // An uninitialized entry. We need this because we want to put
  829. // instances of this class into an STL container.
  830. Output_reloc()
  831. : local_sym_index_(INVALID_CODE)
  832. { }
  833. // We have a bunch of different constructors. They come in pairs
  834. // depending on how the address of the relocation is specified. It
  835. // can either be an offset in an Output_data or an offset in an
  836. // input section.
  837. // A reloc against a global symbol.
  838. Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
  839. Address address, bool is_relative, bool is_symbolless,
  840. bool use_plt_offset);
  841. Output_reloc(Symbol* gsym, unsigned int type,
  842. Sized_relobj<size, big_endian>* relobj,
  843. unsigned int shndx, Address address, bool is_relative,
  844. bool is_symbolless, bool use_plt_offset);
  845. // A reloc against a local symbol or local section symbol.
  846. Output_reloc(Sized_relobj<size, big_endian>* relobj,
  847. unsigned int local_sym_index, unsigned int type,
  848. Output_data* od, Address address, bool is_relative,
  849. bool is_symbolless, bool is_section_symbol,
  850. bool use_plt_offset);
  851. Output_reloc(Sized_relobj<size, big_endian>* relobj,
  852. unsigned int local_sym_index, unsigned int type,
  853. unsigned int shndx, Address address, bool is_relative,
  854. bool is_symbolless, bool is_section_symbol,
  855. bool use_plt_offset);
  856. // A reloc against the STT_SECTION symbol of an output section.
  857. Output_reloc(Output_section* os, unsigned int type, Output_data* od,
  858. Address address, bool is_relative);
  859. Output_reloc(Output_section* os, unsigned int type,
  860. Sized_relobj<size, big_endian>* relobj, unsigned int shndx,
  861. Address address, bool is_relative);
  862. // An absolute or relative relocation with no symbol.
  863. Output_reloc(unsigned int type, Output_data* od, Address address,
  864. bool is_relative);
  865. Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
  866. unsigned int shndx, Address address, bool is_relative);
  867. // A target specific relocation. The target will be called to get
  868. // the symbol index, passing ARG. The type and offset will be set
  869. // as for other relocation types.
  870. Output_reloc(unsigned int type, void* arg, Output_data* od,
  871. Address address);
  872. Output_reloc(unsigned int type, void* arg,
  873. Sized_relobj<size, big_endian>* relobj,
  874. unsigned int shndx, Address address);
  875. // Return the reloc type.
  876. unsigned int
  877. type() const
  878. { return this->type_; }
  879. // Return whether this is a RELATIVE relocation.
  880. bool
  881. is_relative() const
  882. { return this->is_relative_; }
  883. // Return whether this is a relocation which should not use
  884. // a symbol, but which obtains its addend from a symbol.
  885. bool
  886. is_symbolless() const
  887. { return this->is_symbolless_; }
  888. // Return whether this is against a local section symbol.
  889. bool
  890. is_local_section_symbol() const
  891. {
  892. return (this->local_sym_index_ != GSYM_CODE
  893. && this->local_sym_index_ != SECTION_CODE
  894. && this->local_sym_index_ != INVALID_CODE
  895. && this->local_sym_index_ != TARGET_CODE
  896. && this->is_section_symbol_);
  897. }
  898. // Return whether this is a target specific relocation.
  899. bool
  900. is_target_specific() const
  901. { return this->local_sym_index_ == TARGET_CODE; }
  902. // Return the argument to pass to the target for a target specific
  903. // relocation.
  904. void*
  905. target_arg() const
  906. {
  907. gold_assert(this->local_sym_index_ == TARGET_CODE);
  908. return this->u1_.arg;
  909. }
  910. // For a local section symbol, return the offset of the input
  911. // section within the output section. ADDEND is the addend being
  912. // applied to the input section.
  913. Address
  914. local_section_offset(Addend addend) const;
  915. // Get the value of the symbol referred to by a Rel relocation when
  916. // we are adding the given ADDEND.
  917. Address
  918. symbol_value(Addend addend) const;
  919. // If this relocation is against an input section, return the
  920. // relocatable object containing the input section.
  921. Sized_relobj<size, big_endian>*
  922. get_relobj() const
  923. {
  924. if (this->shndx_ == INVALID_CODE)
  925. return NULL;
  926. return this->u2_.relobj;
  927. }
  928. // Write the reloc entry to an output view.
  929. void
  930. write(unsigned char* pov) const;
  931. // Write the offset and info fields to Write_rel.
  932. template<typename Write_rel>
  933. void write_rel(Write_rel*) const;
  934. // This is used when sorting dynamic relocs. Return -1 to sort this
  935. // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
  936. int
  937. compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
  938. const;
  939. // Return whether this reloc should be sorted before the argument
  940. // when sorting dynamic relocs.
  941. bool
  942. sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
  943. r2) const
  944. { return this->compare(r2) < 0; }
  945. private:
  946. // Record that we need a dynamic symbol index.
  947. void
  948. set_needs_dynsym_index();
  949. // Return the symbol index.
  950. unsigned int
  951. get_symbol_index() const;
  952. // Return the output address.
  953. Address
  954. get_address() const;
  955. // Codes for local_sym_index_.
  956. enum
  957. {
  958. // Global symbol.
  959. GSYM_CODE = -1U,
  960. // Output section.
  961. SECTION_CODE = -2U,
  962. // Target specific.
  963. TARGET_CODE = -3U,
  964. // Invalid uninitialized entry.
  965. INVALID_CODE = -4U
  966. };
  967. union
  968. {
  969. // For a local symbol or local section symbol
  970. // (this->local_sym_index_ >= 0), the object. We will never
  971. // generate a relocation against a local symbol in a dynamic
  972. // object; that doesn't make sense. And our callers will always
  973. // be templatized, so we use Sized_relobj here.
  974. Sized_relobj<size, big_endian>* relobj;
  975. // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
  976. // symbol. If this is NULL, it indicates a relocation against the
  977. // undefined 0 symbol.
  978. Symbol* gsym;
  979. // For a relocation against an output section
  980. // (this->local_sym_index_ == SECTION_CODE), the output section.
  981. Output_section* os;
  982. // For a target specific relocation, an argument to pass to the
  983. // target.
  984. void* arg;
  985. } u1_;
  986. union
  987. {
  988. // If this->shndx_ is not INVALID CODE, the object which holds the
  989. // input section being used to specify the reloc address.
  990. Sized_relobj<size, big_endian>* relobj;
  991. // If this->shndx_ is INVALID_CODE, the output data being used to
  992. // specify the reloc address. This may be NULL if the reloc
  993. // address is absolute.
  994. Output_data* od;
  995. } u2_;
  996. // The address offset within the input section or the Output_data.
  997. Address address_;
  998. // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
  999. // relocation against an output section, or TARGET_CODE for a target
  1000. // specific relocation, or INVALID_CODE for an uninitialized value.
  1001. // Otherwise, for a local symbol (this->is_section_symbol_ is
  1002. // false), the local symbol index. For a local section symbol
  1003. // (this->is_section_symbol_ is true), the section index in the
  1004. // input file.
  1005. unsigned int local_sym_index_;
  1006. // The reloc type--a processor specific code.
  1007. unsigned int type_ : 28;
  1008. // True if the relocation is a RELATIVE relocation.
  1009. bool is_relative_ : 1;
  1010. // True if the relocation is one which should not use
  1011. // a symbol, but which obtains its addend from a symbol.
  1012. bool is_symbolless_ : 1;
  1013. // True if the relocation is against a section symbol.
  1014. bool is_section_symbol_ : 1;
  1015. // True if the addend should be the PLT offset.
  1016. // (Used only for RELA, but stored here for space.)
  1017. bool use_plt_offset_ : 1;
  1018. // If the reloc address is an input section in an object, the
  1019. // section index. This is INVALID_CODE if the reloc address is
  1020. // specified in some other way.
  1021. unsigned int shndx_;
  1022. };
  1023. // The SHT_RELA version of Output_reloc<>. This is just derived from
  1024. // the SHT_REL version of Output_reloc, but it adds an addend.
  1025. template<bool dynamic, int size, bool big_endian>
  1026. class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
  1027. {
  1028. public:
  1029. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1030. typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
  1031. // An uninitialized entry.
  1032. Output_reloc()
  1033. : rel_()
  1034. { }
  1035. // A reloc against a global symbol.
  1036. Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
  1037. Address address, Addend addend, bool is_relative,
  1038. bool is_symbolless, bool use_plt_offset)
  1039. : rel_(gsym, type, od, address, is_relative, is_symbolless,
  1040. use_plt_offset),
  1041. addend_(addend)
  1042. { }
  1043. Output_reloc(Symbol* gsym, unsigned int type,
  1044. Sized_relobj<size, big_endian>* relobj,
  1045. unsigned int shndx, Address address, Addend addend,
  1046. bool is_relative, bool is_symbolless, bool use_plt_offset)
  1047. : rel_(gsym, type, relobj, shndx, address, is_relative,
  1048. is_symbolless, use_plt_offset), addend_(addend)
  1049. { }
  1050. // A reloc against a local symbol.
  1051. Output_reloc(Sized_relobj<size, big_endian>* relobj,
  1052. unsigned int local_sym_index, unsigned int type,
  1053. Output_data* od, Address address,
  1054. Addend addend, bool is_relative,
  1055. bool is_symbolless, bool is_section_symbol,
  1056. bool use_plt_offset)
  1057. : rel_(relobj, local_sym_index, type, od, address, is_relative,
  1058. is_symbolless, is_section_symbol, use_plt_offset),
  1059. addend_(addend)
  1060. { }
  1061. Output_reloc(Sized_relobj<size, big_endian>* relobj,
  1062. unsigned int local_sym_index, unsigned int type,
  1063. unsigned int shndx, Address address,
  1064. Addend addend, bool is_relative,
  1065. bool is_symbolless, bool is_section_symbol,
  1066. bool use_plt_offset)
  1067. : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
  1068. is_symbolless, is_section_symbol, use_plt_offset),
  1069. addend_(addend)
  1070. { }
  1071. // A reloc against the STT_SECTION symbol of an output section.
  1072. Output_reloc(Output_section* os, unsigned int type, Output_data* od,
  1073. Address address, Addend addend, bool is_relative)
  1074. : rel_(os, type, od, address, is_relative), addend_(addend)
  1075. { }
  1076. Output_reloc(Output_section* os, unsigned int type,
  1077. Sized_relobj<size, big_endian>* relobj,
  1078. unsigned int shndx, Address address, Addend addend,
  1079. bool is_relative)
  1080. : rel_(os, type, relobj, shndx, address, is_relative), addend_(addend)
  1081. { }
  1082. // An absolute or relative relocation with no symbol.
  1083. Output_reloc(unsigned int type, Output_data* od, Address address,
  1084. Addend addend, bool is_relative)
  1085. : rel_(type, od, address, is_relative), addend_(addend)
  1086. { }
  1087. Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
  1088. unsigned int shndx, Address address, Addend addend,
  1089. bool is_relative)
  1090. : rel_(type, relobj, shndx, address, is_relative), addend_(addend)
  1091. { }
  1092. // A target specific relocation. The target will be called to get
  1093. // the symbol index and the addend, passing ARG. The type and
  1094. // offset will be set as for other relocation types.
  1095. Output_reloc(unsigned int type, void* arg, Output_data* od,
  1096. Address address, Addend addend)
  1097. : rel_(type, arg, od, address), addend_(addend)
  1098. { }
  1099. Output_reloc(unsigned int type, void* arg,
  1100. Sized_relobj<size, big_endian>* relobj,
  1101. unsigned int shndx, Address address, Addend addend)
  1102. : rel_(type, arg, relobj, shndx, address), addend_(addend)
  1103. { }
  1104. // Return whether this is a RELATIVE relocation.
  1105. bool
  1106. is_relative() const
  1107. { return this->rel_.is_relative(); }
  1108. // Return whether this is a relocation which should not use
  1109. // a symbol, but which obtains its addend from a symbol.
  1110. bool
  1111. is_symbolless() const
  1112. { return this->rel_.is_symbolless(); }
  1113. // If this relocation is against an input section, return the
  1114. // relocatable object containing the input section.
  1115. Sized_relobj<size, big_endian>*
  1116. get_relobj() const
  1117. { return this->rel_.get_relobj(); }
  1118. // Write the reloc entry to an output view.
  1119. void
  1120. write(unsigned char* pov) const;
  1121. // Return whether this reloc should be sorted before the argument
  1122. // when sorting dynamic relocs.
  1123. bool
  1124. sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
  1125. r2) const
  1126. {
  1127. int i = this->rel_.compare(r2.rel_);
  1128. if (i < 0)
  1129. return true;
  1130. else if (i > 0)
  1131. return false;
  1132. else
  1133. return this->addend_ < r2.addend_;
  1134. }
  1135. private:
  1136. // The basic reloc.
  1137. Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
  1138. // The addend.
  1139. Addend addend_;
  1140. };
  1141. // Output_data_reloc_generic is a non-template base class for
  1142. // Output_data_reloc_base. This gives the generic code a way to hold
  1143. // a pointer to a reloc section.
  1144. class Output_data_reloc_generic : public Output_section_data_build
  1145. {
  1146. public:
  1147. Output_data_reloc_generic(int size, bool sort_relocs)
  1148. : Output_section_data_build(Output_data::default_alignment_for_size(size)),
  1149. relative_reloc_count_(0), sort_relocs_(sort_relocs)
  1150. { }
  1151. // Return the number of relative relocs in this section.
  1152. size_t
  1153. relative_reloc_count() const
  1154. { return this->relative_reloc_count_; }
  1155. // Whether we should sort the relocs.
  1156. bool
  1157. sort_relocs() const
  1158. { return this->sort_relocs_; }
  1159. // Add a reloc of type TYPE against the global symbol GSYM. The
  1160. // relocation applies to the data at offset ADDRESS within OD.
  1161. virtual void
  1162. add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
  1163. uint64_t address, uint64_t addend) = 0;
  1164. // Add a reloc of type TYPE against the global symbol GSYM. The
  1165. // relocation applies to data at offset ADDRESS within section SHNDX
  1166. // of object file RELOBJ. OD is the associated output section.
  1167. virtual void
  1168. add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
  1169. Relobj* relobj, unsigned int shndx, uint64_t address,
  1170. uint64_t addend) = 0;
  1171. // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
  1172. // in RELOBJ. The relocation applies to the data at offset ADDRESS
  1173. // within OD.
  1174. virtual void
  1175. add_local_generic(Relobj* relobj, unsigned int local_sym_index,
  1176. unsigned int type, Output_data* od, uint64_t address,
  1177. uint64_t addend) = 0;
  1178. // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
  1179. // in RELOBJ. The relocation applies to the data at offset ADDRESS
  1180. // within section SHNDX of RELOBJ. OD is the associated output
  1181. // section.
  1182. virtual void
  1183. add_local_generic(Relobj* relobj, unsigned int local_sym_index,
  1184. unsigned int type, Output_data* od, unsigned int shndx,
  1185. uint64_t address, uint64_t addend) = 0;
  1186. // Add a reloc of type TYPE against the STT_SECTION symbol of the
  1187. // output section OS. The relocation applies to the data at offset
  1188. // ADDRESS within OD.
  1189. virtual void
  1190. add_output_section_generic(Output_section *os, unsigned int type,
  1191. Output_data* od, uint64_t address,
  1192. uint64_t addend) = 0;
  1193. // Add a reloc of type TYPE against the STT_SECTION symbol of the
  1194. // output section OS. The relocation applies to the data at offset
  1195. // ADDRESS within section SHNDX of RELOBJ. OD is the associated
  1196. // output section.
  1197. virtual void
  1198. add_output_section_generic(Output_section* os, unsigned int type,
  1199. Output_data* od, Relobj* relobj,
  1200. unsigned int shndx, uint64_t address,
  1201. uint64_t addend) = 0;
  1202. protected:
  1203. // Note that we've added another relative reloc.
  1204. void
  1205. bump_relative_reloc_count()
  1206. { ++this->relative_reloc_count_; }
  1207. private:
  1208. // The number of relative relocs added to this section. This is to
  1209. // support DT_RELCOUNT.
  1210. size_t relative_reloc_count_;
  1211. // Whether to sort the relocations when writing them out, to make
  1212. // the dynamic linker more efficient.
  1213. bool sort_relocs_;
  1214. };
  1215. // Output_data_reloc is used to manage a section containing relocs.
  1216. // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
  1217. // indicates whether this is a dynamic relocation or a normal
  1218. // relocation. Output_data_reloc_base is a base class.
  1219. // Output_data_reloc is the real class, which we specialize based on
  1220. // the reloc type.
  1221. template<int sh_type, bool dynamic, int size, bool big_endian>
  1222. class Output_data_reloc_base : public Output_data_reloc_generic
  1223. {
  1224. public:
  1225. typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
  1226. typedef typename Output_reloc_type::Address Address;
  1227. static const int reloc_size =
  1228. Reloc_types<sh_type, size, big_endian>::reloc_size;
  1229. // Construct the section.
  1230. Output_data_reloc_base(bool sort_relocs)
  1231. : Output_data_reloc_generic(size, sort_relocs)
  1232. { }
  1233. protected:
  1234. // Write out the data.
  1235. void
  1236. do_write(Output_file*);
  1237. // Set the entry size and the link.
  1238. void
  1239. do_adjust_output_section(Output_section* os);
  1240. // Write to a map file.
  1241. void
  1242. do_print_to_mapfile(Mapfile* mapfile) const
  1243. {
  1244. mapfile->print_output_data(this,
  1245. (dynamic
  1246. ? _("** dynamic relocs")
  1247. : _("** relocs")));
  1248. }
  1249. // Add a relocation entry.
  1250. void
  1251. add(Output_data* od, const Output_reloc_type& reloc)
  1252. {
  1253. this->relocs_.push_back(reloc);
  1254. this->set_current_data_size(this->relocs_.size() * reloc_size);
  1255. if (dynamic)
  1256. od->add_dynamic_reloc();
  1257. if (reloc.is_relative())
  1258. this->bump_relative_reloc_count();
  1259. Sized_relobj<size, big_endian>* relobj = reloc.get_relobj();
  1260. if (relobj != NULL)
  1261. relobj->add_dyn_reloc(this->relocs_.size() - 1);
  1262. }
  1263. private:
  1264. typedef std::vector<Output_reloc_type> Relocs;
  1265. // The class used to sort the relocations.
  1266. struct Sort_relocs_comparison
  1267. {
  1268. bool
  1269. operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
  1270. { return r1.sort_before(r2); }
  1271. };
  1272. // The relocations in this section.
  1273. Relocs relocs_;
  1274. };
  1275. // The class which callers actually create.
  1276. template<int sh_type, bool dynamic, int size, bool big_endian>
  1277. class Output_data_reloc;
  1278. // The SHT_REL version of Output_data_reloc.
  1279. template<bool dynamic, int size, bool big_endian>
  1280. class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
  1281. : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
  1282. {
  1283. private:
  1284. typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
  1285. big_endian> Base;
  1286. public:
  1287. typedef typename Base::Output_reloc_type Output_reloc_type;
  1288. typedef typename Output_reloc_type::Address Address;
  1289. Output_data_reloc(bool sr)
  1290. : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
  1291. { }
  1292. // Add a reloc against a global symbol.
  1293. void
  1294. add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
  1295. {
  1296. this->add(od, Output_reloc_type(gsym, type, od, address,
  1297. false, false, false));
  1298. }
  1299. void
  1300. add_global(Symbol* gsym, unsigned int type, Output_data* od,
  1301. Sized_relobj<size, big_endian>* relobj,
  1302. unsigned int shndx, Address address)
  1303. {
  1304. this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
  1305. false, false, false));
  1306. }
  1307. void
  1308. add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
  1309. uint64_t address, uint64_t addend)
  1310. {
  1311. gold_assert(addend == 0);
  1312. this->add(od, Output_reloc_type(gsym, type, od,
  1313. convert_types<Address, uint64_t>(address),
  1314. false, false, false));
  1315. }
  1316. void
  1317. add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
  1318. Relobj* relobj, unsigned int shndx, uint64_t address,
  1319. uint64_t addend)
  1320. {
  1321. gold_assert(addend == 0);
  1322. Sized_relobj<size, big_endian>* sized_relobj =
  1323. static_cast<Sized_relobj<size, big_endian>*>(relobj);
  1324. this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
  1325. convert_types<Address, uint64_t>(address),
  1326. false, false, false));
  1327. }
  1328. // Add a RELATIVE reloc against a global symbol. The final relocation
  1329. // will not reference the symbol.
  1330. void
  1331. add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
  1332. Address address)
  1333. {
  1334. this->add(od, Output_reloc_type(gsym, type, od, address, true, true,
  1335. false));
  1336. }
  1337. void
  1338. add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
  1339. Sized_relobj<size, big_endian>* relobj,
  1340. unsigned int shndx, Address address)
  1341. {
  1342. this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
  1343. true, true, false));
  1344. }
  1345. // Add a global relocation which does not use a symbol for the relocation,
  1346. // but which gets its addend from a symbol.
  1347. void
  1348. add_symbolless_global_addend(Symbol* gsym, unsigned int type,
  1349. Output_data* od, Address address)
  1350. {
  1351. this->add(od, Output_reloc_type(gsym, type, od, address, false, true,
  1352. false));
  1353. }
  1354. void
  1355. add_symbolless_global_addend(Symbol* gsym, unsigned int type,
  1356. Output_data* od,
  1357. Sized_relobj<size, big_endian>* relobj,
  1358. unsigned int shndx, Address address)
  1359. {
  1360. this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
  1361. false, true, false));
  1362. }
  1363. // Add a reloc against a local symbol.
  1364. void
  1365. add_local(Sized_relobj<size, big_endian>* relobj,
  1366. unsigned int local_sym_index, unsigned int type,
  1367. Output_data* od, Address address)
  1368. {
  1369. this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
  1370. address, false, false, false, false));
  1371. }
  1372. void
  1373. add_local(Sized_relobj<size, big_endian>* relobj,
  1374. unsigned int local_sym_index, unsigned int type,
  1375. Output_data* od, unsigned int shndx, Address address)
  1376. {
  1377. this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
  1378. address, false, false, false, false));
  1379. }
  1380. void
  1381. add_local_generic(Relobj* relobj, unsigned int local_sym_index,
  1382. unsigned int type, Output_data* od, uint64_t address,
  1383. uint64_t addend)
  1384. {
  1385. gold_assert(addend == 0);
  1386. Sized_relobj<size, big_endian>* sized_relobj =
  1387. static_cast<Sized_relobj<size, big_endian> *>(relobj);
  1388. this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
  1389. convert_types<Address, uint64_t>(address),
  1390. false, false, false, false));
  1391. }
  1392. void
  1393. add_local_generic(Relobj* relobj, unsigned int local_sym_index,
  1394. unsigned int type, Output_data* od, unsigned int shndx,
  1395. uint64_t address, uint64_t addend)
  1396. {
  1397. gold_assert(addend == 0);
  1398. Sized_relobj<size, big_endian>* sized_relobj =
  1399. static_cast<Sized_relobj<size, big_endian>*>(relobj);
  1400. this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
  1401. convert_types<Address, uint64_t>(address),
  1402. false, false, false, false));
  1403. }
  1404. // Add a RELATIVE reloc against a local symbol.
  1405. void
  1406. add_local_relative(Sized_relobj<size, big_endian>* relobj,
  1407. unsigned int local_sym_index, unsigned int type,
  1408. Output_data* od, Address address)
  1409. {
  1410. this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
  1411. address, true, true, false, false));
  1412. }
  1413. void
  1414. add_local_relative(Sized_relobj<size, big_endian>* relobj,
  1415. unsigned int local_sym_index, unsigned int type,
  1416. Output_data* od, unsigned int shndx, Address address)
  1417. {
  1418. this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
  1419. address, true, true, false, false));
  1420. }
  1421. void
  1422. add_local_relative(Sized_relobj<size, big_endian>* relobj,
  1423. unsigned int local_sym_index, unsigned int type,
  1424. Output_data* od, unsigned int shndx, Address address,
  1425. bool use_plt_offset)
  1426. {
  1427. this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
  1428. address, true, true, false,
  1429. use_plt_offset));
  1430. }
  1431. // Add a local relocation which does not use a symbol for the relocation,
  1432. // but which gets its addend from a symbol.
  1433. void
  1434. add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
  1435. unsigned int local_sym_index, unsigned int type,
  1436. Output_data* od, Address address)
  1437. {
  1438. this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
  1439. address, false, true, false, false));
  1440. }
  1441. void
  1442. add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
  1443. unsigned int local_sym_index, unsigned int type,
  1444. Output_data* od, unsigned int shndx,
  1445. Address address)
  1446. {
  1447. this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
  1448. address, false, true, false, false));
  1449. }
  1450. // Add a reloc against a local section symbol. This will be
  1451. // converted into a reloc against the STT_SECTION symbol of the
  1452. // output section.
  1453. void
  1454. add_local_section(Sized_relobj<size, big_endian>* relobj,
  1455. unsigned int input_shndx, unsigned int type,
  1456. Output_data* od, Address address)
  1457. {
  1458. this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
  1459. address, false, false, true, false));
  1460. }
  1461. void
  1462. add_local_section(Sized_relobj<size, big_endian>* relobj,
  1463. unsigned int input_shndx, unsigned int type,
  1464. Output_data* od, unsigned int shndx, Address address)
  1465. {
  1466. this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
  1467. address, false, false, true, false));
  1468. }
  1469. // A reloc against the STT_SECTION symbol of an output section.
  1470. // OS is the Output_section that the relocation refers to; OD is
  1471. // the Output_data object being relocated.
  1472. void
  1473. add_output_section(Output_section* os, unsigned int type,
  1474. Output_data* od, Address address)
  1475. { this->add(od, Output_reloc_type(os, type, od, address, false)); }
  1476. void
  1477. add_output_section(Output_section* os, unsigned int type, Output_data* od,
  1478. Sized_relobj<size, big_endian>* relobj,
  1479. unsigned int shndx, Address address)
  1480. { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, false)); }
  1481. void
  1482. add_output_section_generic(Output_section* os, unsigned int type,
  1483. Output_data* od, uint64_t address,
  1484. uint64_t addend)
  1485. {
  1486. gold_assert(addend == 0);
  1487. this->add(od, Output_reloc_type(os, type, od,
  1488. convert_types<Address, uint64_t>(address),
  1489. false));
  1490. }
  1491. void
  1492. add_output_section_generic(Output_section* os, unsigned int type,
  1493. Output_data* od, Relobj* relobj,
  1494. unsigned int shndx, uint64_t address,
  1495. uint64_t addend)
  1496. {
  1497. gold_assert(addend == 0);
  1498. Sized_relobj<size, big_endian>* sized_relobj =
  1499. static_cast<Sized_relobj<size, big_endian>*>(relobj);
  1500. this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
  1501. convert_types<Address, uint64_t>(address),
  1502. false));
  1503. }
  1504. // As above, but the reloc TYPE is relative
  1505. void
  1506. add_output_section_relative(Output_section* os, unsigned int type,
  1507. Output_data* od, Address address)
  1508. { this->add(od, Output_reloc_type(os, type, od, address, true)); }
  1509. void
  1510. add_output_section_relative(Output_section* os, unsigned int type,
  1511. Output_data* od,
  1512. Sized_relobj<size, big_endian>* relobj,
  1513. unsigned int shndx, Address address)
  1514. { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, true)); }
  1515. // Add an absolute relocation.
  1516. void
  1517. add_absolute(unsigned int type, Output_data* od, Address address)
  1518. { this->add(od, Output_reloc_type(type, od, address, false)); }
  1519. void
  1520. add_absolute(unsigned int type, Output_data* od,
  1521. Sized_relobj<size, big_endian>* relobj,
  1522. unsigned int shndx, Address address)
  1523. { this->add(od, Output_reloc_type(type, relobj, shndx, address, false)); }
  1524. // Add a relative relocation
  1525. void
  1526. add_relative(unsigned int type, Output_data* od, Address address)
  1527. { this->add(od, Output_reloc_type(type, od, address, true)); }
  1528. void
  1529. add_relative(unsigned int type, Output_data* od,
  1530. Sized_relobj<size, big_endian>* relobj,
  1531. unsigned int shndx, Address address)
  1532. { this->add(od, Output_reloc_type(type, relobj, shndx, address, true)); }
  1533. // Add a target specific relocation. A target which calls this must
  1534. // define the reloc_symbol_index and reloc_addend virtual functions.
  1535. void
  1536. add_target_specific(unsigned int type, void* arg, Output_data* od,
  1537. Address address)
  1538. { this->add(od, Output_reloc_type(type, arg, od, address)); }
  1539. void
  1540. add_target_specific(unsigned int type, void* arg, Output_data* od,
  1541. Sized_relobj<size, big_endian>* relobj,
  1542. unsigned int shndx, Address address)
  1543. { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
  1544. };
  1545. // The SHT_RELA version of Output_data_reloc.
  1546. template<bool dynamic, int size, bool big_endian>
  1547. class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
  1548. : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
  1549. {
  1550. private:
  1551. typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
  1552. big_endian> Base;
  1553. public:
  1554. typedef typename Base::Output_reloc_type Output_reloc_type;
  1555. typedef typename Output_reloc_type::Address Address;
  1556. typedef typename Output_reloc_type::Addend Addend;
  1557. Output_data_reloc(bool sr)
  1558. : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
  1559. { }
  1560. // Add a reloc against a global symbol.
  1561. void
  1562. add_global(Symbol* gsym, unsigned int type, Output_data* od,
  1563. Address address, Addend addend)
  1564. {
  1565. this->add(od, Output_reloc_type(gsym, type, od, address, addend,
  1566. false, false, false));
  1567. }
  1568. void
  1569. add_global(Symbol* gsym, unsigned int type, Output_data* od,
  1570. Sized_relobj<size, big_endian>* relobj,
  1571. unsigned int shndx, Address address,
  1572. Addend addend)
  1573. {
  1574. this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
  1575. addend, false, false, false));
  1576. }
  1577. void
  1578. add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
  1579. uint64_t address, uint64_t addend)
  1580. {
  1581. this->add(od, Output_reloc_type(gsym, type, od,
  1582. convert_types<Address, uint64_t>(address),
  1583. convert_types<Addend, uint64_t>(addend),
  1584. false, false, false));
  1585. }
  1586. void
  1587. add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
  1588. Relobj* relobj, unsigned int shndx, uint64_t address,
  1589. uint64_t addend)
  1590. {
  1591. Sized_relobj<size, big_endian>* sized_relobj =
  1592. static_cast<Sized_relobj<size, big_endian>*>(relobj);
  1593. this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
  1594. convert_types<Address, uint64_t>(address),
  1595. convert_types<Addend, uint64_t>(addend),
  1596. false, false, false));
  1597. }
  1598. // Add a RELATIVE reloc against a global symbol. The final output
  1599. // relocation will not reference the symbol, but we must keep the symbol
  1600. // information long enough to set the addend of the relocation correctly
  1601. // when it is written.
  1602. void
  1603. add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
  1604. Address address, Addend addend, bool use_plt_offset)
  1605. {
  1606. this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
  1607. true, use_plt_offset));
  1608. }
  1609. void
  1610. add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
  1611. Sized_relobj<size, big_endian>* relobj,
  1612. unsigned int shndx, Address address, Addend addend,
  1613. bool use_plt_offset)
  1614. {
  1615. this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
  1616. addend, true, true, use_plt_offset));
  1617. }
  1618. // Add a global relocation which does not use a symbol for the relocation,
  1619. // but which gets its addend from a symbol.
  1620. void
  1621. add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
  1622. Address address, Addend addend)
  1623. {
  1624. this->add(od, Output_reloc_type(gsym, type, od, address, addend,
  1625. false, true, false));
  1626. }
  1627. void
  1628. add_symbolless_global_addend(Symbol* gsym, unsigned int type,
  1629. Output_data* od,
  1630. Sized_relobj<size, big_endian>* relobj,
  1631. unsigned int shndx, Address address,
  1632. Addend addend)
  1633. {
  1634. this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
  1635. addend, false, true, false));
  1636. }
  1637. // Add a reloc against a local symbol.
  1638. void
  1639. add_local(Sized_relobj<size, big_endian>* relobj,
  1640. unsigned int local_sym_index, unsigned int type,
  1641. Output_data* od, Address address, Addend addend)
  1642. {
  1643. this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
  1644. addend, false, false, false, false));
  1645. }
  1646. void
  1647. add_local(Sized_relobj<size, big_endian>* relobj,
  1648. unsigned int local_sym_index, unsigned int type,
  1649. Output_data* od, unsigned int shndx, Address address,
  1650. Addend addend)
  1651. {
  1652. this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
  1653. address, addend, false, false, false,
  1654. false));
  1655. }
  1656. void
  1657. add_local_generic(Relobj* relobj, unsigned int local_sym_index,
  1658. unsigned int type, Output_data* od, uint64_t address,
  1659. uint64_t addend)
  1660. {
  1661. Sized_relobj<size, big_endian>* sized_relobj =
  1662. static_cast<Sized_relobj<size, big_endian> *>(relobj);
  1663. this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
  1664. convert_types<Address, uint64_t>(address),
  1665. convert_types<Addend, uint64_t>(addend),
  1666. false, false, false, false));
  1667. }
  1668. void
  1669. add_local_generic(Relobj* relobj, unsigned int local_sym_index,
  1670. unsigned int type, Output_data* od, unsigned int shndx,
  1671. uint64_t address, uint64_t addend)
  1672. {
  1673. Sized_relobj<size, big_endian>* sized_relobj =
  1674. static_cast<Sized_relobj<size, big_endian>*>(relobj);
  1675. this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
  1676. convert_types<Address, uint64_t>(address),
  1677. convert_types<Addend, uint64_t>(addend),
  1678. false, false, false, false));
  1679. }
  1680. // Add a RELATIVE reloc against a local symbol.
  1681. void
  1682. add_local_relative(Sized_relobj<size, big_endian>* relobj,
  1683. unsigned int local_sym_index, unsigned int type,
  1684. Output_data* od, Address address, Addend addend,
  1685. bool use_plt_offset)
  1686. {
  1687. this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
  1688. addend, true, true, false,
  1689. use_plt_offset));
  1690. }
  1691. void
  1692. add_local_relative(Sized_relobj<size, big_endian>* relobj,
  1693. unsigned int local_sym_index, unsigned int type,
  1694. Output_data* od, unsigned int shndx, Address address,
  1695. Addend addend, bool use_plt_offset)
  1696. {
  1697. this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
  1698. address, addend, true, true, false,
  1699. use_plt_offset));
  1700. }
  1701. // Add a local relocation which does not use a symbol for the relocation,
  1702. // but which gets it's addend from a symbol.
  1703. void
  1704. add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
  1705. unsigned int local_sym_index, unsigned int type,
  1706. Output_data* od, Address address, Addend addend)
  1707. {
  1708. this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
  1709. addend, false, true, false, false));
  1710. }
  1711. void
  1712. add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
  1713. unsigned int local_sym_index, unsigned int type,
  1714. Output_data* od, unsigned int shndx,
  1715. Address address, Addend addend)
  1716. {
  1717. this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
  1718. address, addend, false, true, false,
  1719. false));
  1720. }
  1721. // Add a reloc against a local section symbol. This will be
  1722. // converted into a reloc against the STT_SECTION symbol of the
  1723. // output section.
  1724. void
  1725. add_local_section(Sized_relobj<size, big_endian>* relobj,
  1726. unsigned int input_shndx, unsigned int type,
  1727. Output_data* od, Address address, Addend addend)
  1728. {
  1729. this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
  1730. addend, false, false, true, false));
  1731. }
  1732. void
  1733. add_local_section(Sized_relobj<size, big_endian>* relobj,
  1734. unsigned int input_shndx, unsigned int type,
  1735. Output_data* od, unsigned int shndx, Address address,
  1736. Addend addend)
  1737. {
  1738. this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
  1739. address, addend, false, false, true,
  1740. false));
  1741. }
  1742. // A reloc against the STT_SECTION symbol of an output section.
  1743. void
  1744. add_output_section(Output_section* os, unsigned int type, Output_data* od,
  1745. Address address, Addend addend)
  1746. { this->add(od, Output_reloc_type(os, type, od, address, addend, false)); }
  1747. void
  1748. add_output_section(Output_section* os, unsigned int type, Output_data* od,
  1749. Sized_relobj<size, big_endian>* relobj,
  1750. unsigned int shndx, Address address, Addend addend)
  1751. {
  1752. this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
  1753. addend, false));
  1754. }
  1755. void
  1756. add_output_section_generic(Output_section* os, unsigned int type,
  1757. Output_data* od, uint64_t address,
  1758. uint64_t addend)
  1759. {
  1760. this->add(od, Output_reloc_type(os, type, od,
  1761. convert_types<Address, uint64_t>(address),
  1762. convert_types<Addend, uint64_t>(addend),
  1763. false));
  1764. }
  1765. void
  1766. add_output_section_generic(Output_section* os, unsigned int type,
  1767. Output_data* od, Relobj* relobj,
  1768. unsigned int shndx, uint64_t address,
  1769. uint64_t addend)
  1770. {
  1771. Sized_relobj<size, big_endian>* sized_relobj =
  1772. static_cast<Sized_relobj<size, big_endian>*>(relobj);
  1773. this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
  1774. convert_types<Address, uint64_t>(address),
  1775. convert_types<Addend, uint64_t>(addend),
  1776. false));
  1777. }
  1778. // As above, but the reloc TYPE is relative
  1779. void
  1780. add_output_section_relative(Output_section* os, unsigned int type,
  1781. Output_data* od, Address address, Addend addend)
  1782. { this->add(od, Output_reloc_type(os, type, od, address, addend, true)); }
  1783. void
  1784. add_output_section_relative(Output_section* os, unsigned int type,
  1785. Output_data* od,
  1786. Sized_relobj<size, big_endian>* relobj,
  1787. unsigned int shndx, Address address,
  1788. Addend addend)
  1789. {
  1790. this->add(od, Output_reloc_type(os, type, relobj, shndx,
  1791. address, addend, true));
  1792. }
  1793. // Add an absolute relocation.
  1794. void
  1795. add_absolute(unsigned int type, Output_data* od, Address address,
  1796. Addend addend)
  1797. { this->add(od, Output_reloc_type(type, od, address, addend, false)); }
  1798. void
  1799. add_absolute(unsigned int type, Output_data* od,
  1800. Sized_relobj<size, big_endian>* relobj,
  1801. unsigned int shndx, Address address, Addend addend)
  1802. {
  1803. this->add(od, Output_reloc_type(type, relobj, shndx, address, addend,
  1804. false));
  1805. }
  1806. // Add a relative relocation
  1807. void
  1808. add_relative(unsigned int type, Output_data* od, Address address,
  1809. Addend addend)
  1810. { this->add(od, Output_reloc_type(type, od, address, addend, true)); }
  1811. void
  1812. add_relative(unsigned int type, Output_data* od,
  1813. Sized_relobj<size, big_endian>* relobj,
  1814. unsigned int shndx, Address address, Addend addend)
  1815. {
  1816. this->add(od, Output_reloc_type(type, relobj, shndx, address, addend,
  1817. true));
  1818. }
  1819. // Add a target specific relocation. A target which calls this must
  1820. // define the reloc_symbol_index and reloc_addend virtual functions.
  1821. void
  1822. add_target_specific(unsigned int type, void* arg, Output_data* od,
  1823. Address address, Addend addend)
  1824. { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
  1825. void
  1826. add_target_specific(unsigned int type, void* arg, Output_data* od,
  1827. Sized_relobj<size, big_endian>* relobj,
  1828. unsigned int shndx, Address address, Addend addend)
  1829. {
  1830. this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
  1831. addend));
  1832. }
  1833. };
  1834. // Output_relocatable_relocs represents a relocation section in a
  1835. // relocatable link. The actual data is written out in the target
  1836. // hook relocate_relocs. This just saves space for it.
  1837. template<int sh_type, int size, bool big_endian>
  1838. class Output_relocatable_relocs : public Output_section_data
  1839. {
  1840. public:
  1841. Output_relocatable_relocs(Relocatable_relocs* rr)
  1842. : Output_section_data(Output_data::default_alignment_for_size(size)),
  1843. rr_(rr)
  1844. { }
  1845. void
  1846. set_final_data_size();
  1847. // Write out the data. There is nothing to do here.
  1848. void
  1849. do_write(Output_file*)
  1850. { }
  1851. // Write to a map file.
  1852. void
  1853. do_print_to_mapfile(Mapfile* mapfile) const
  1854. { mapfile->print_output_data(this, _("** relocs")); }
  1855. private:
  1856. // The relocs associated with this input section.
  1857. Relocatable_relocs* rr_;
  1858. };
  1859. // Handle a GROUP section.
  1860. template<int size, bool big_endian>
  1861. class Output_data_group : public Output_section_data
  1862. {
  1863. public:
  1864. // The constructor clears *INPUT_SHNDXES.
  1865. Output_data_group(Sized_relobj_file<size, big_endian>* relobj,
  1866. section_size_type entry_count,
  1867. elfcpp::Elf_Word flags,
  1868. std::vector<unsigned int>* input_shndxes);
  1869. void
  1870. do_write(Output_file*);
  1871. // Write to a map file.
  1872. void
  1873. do_print_to_mapfile(Mapfile* mapfile) const
  1874. { mapfile->print_output_data(this, _("** group")); }
  1875. // Set final data size.
  1876. void
  1877. set_final_data_size()
  1878. { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
  1879. private:
  1880. // The input object.
  1881. Sized_relobj_file<size, big_endian>* relobj_;
  1882. // The group flag word.
  1883. elfcpp::Elf_Word flags_;
  1884. // The section indexes of the input sections in this group.
  1885. std::vector<unsigned int> input_shndxes_;
  1886. };
  1887. // Output_data_got is used to manage a GOT. Each entry in the GOT is
  1888. // for one symbol--either a global symbol or a local symbol in an
  1889. // object. The target specific code adds entries to the GOT as
  1890. // needed. The GOT_SIZE template parameter is the size in bits of a
  1891. // GOT entry, typically 32 or 64.
  1892. class Output_data_got_base : public Output_section_data_build
  1893. {
  1894. public:
  1895. Output_data_got_base(uint64_t align)
  1896. : Output_section_data_build(align)
  1897. { }
  1898. Output_data_got_base(off_t data_size, uint64_t align)
  1899. : Output_section_data_build(data_size, align)
  1900. { }
  1901. // Reserve the slot at index I in the GOT.
  1902. void
  1903. reserve_slot(unsigned int i)
  1904. { this->do_reserve_slot(i); }
  1905. protected:
  1906. // Reserve the slot at index I in the GOT.
  1907. virtual void
  1908. do_reserve_slot(unsigned int i) = 0;
  1909. };
  1910. template<int got_size, bool big_endian>
  1911. class Output_data_got : public Output_data_got_base
  1912. {
  1913. public:
  1914. typedef typename elfcpp::Elf_types<got_size>::Elf_Addr Valtype;
  1915. Output_data_got()
  1916. : Output_data_got_base(Output_data::default_alignment_for_size(got_size)),
  1917. entries_(), free_list_()
  1918. { }
  1919. Output_data_got(off_t data_size)
  1920. : Output_data_got_base(data_size,
  1921. Output_data::default_alignment_for_size(got_size)),
  1922. entries_(), free_list_()
  1923. {
  1924. // For an incremental update, we have an existing GOT section.
  1925. // Initialize the list of entries and the free list.
  1926. this->entries_.resize(data_size / (got_size / 8));
  1927. this->free_list_.init(data_size, false);
  1928. }
  1929. // Add an entry for a global symbol to the GOT. Return true if this
  1930. // is a new GOT entry, false if the symbol was already in the GOT.
  1931. bool
  1932. add_global(Symbol* gsym, unsigned int got_type);
  1933. // Like add_global, but use the PLT offset of the global symbol if
  1934. // it has one.
  1935. bool
  1936. add_global_plt(Symbol* gsym, unsigned int got_type);
  1937. // Like add_global, but for a TLS symbol where the value will be
  1938. // offset using Target::tls_offset_for_global.
  1939. bool
  1940. add_global_tls(Symbol* gsym, unsigned int got_type)
  1941. { return add_global_plt(gsym, got_type); }
  1942. // Add an entry for a global symbol to the GOT, and add a dynamic
  1943. // relocation of type R_TYPE for the GOT entry.
  1944. void
  1945. add_global_with_rel(Symbol* gsym, unsigned int got_type,
  1946. Output_data_reloc_generic* rel_dyn, unsigned int r_type);
  1947. // Add a pair of entries for a global symbol to the GOT, and add
  1948. // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
  1949. void
  1950. add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
  1951. Output_data_reloc_generic* rel_dyn,
  1952. unsigned int r_type_1, unsigned int r_type_2);
  1953. // Add an entry for a local symbol to the GOT. This returns true if
  1954. // this is a new GOT entry, false if the symbol already has a GOT
  1955. // entry.
  1956. bool
  1957. add_local(Relobj* object, unsigned int sym_index, unsigned int got_type);
  1958. // Like add_local, but use the PLT offset of the local symbol if it
  1959. // has one.
  1960. bool
  1961. add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type);
  1962. // Like add_local, but for a TLS symbol where the value will be
  1963. // offset using Target::tls_offset_for_local.
  1964. bool
  1965. add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
  1966. { return add_local_plt(object, sym_index, got_type); }
  1967. // Add an entry for a local symbol to the GOT, and add a dynamic
  1968. // relocation of type R_TYPE for the GOT entry.
  1969. void
  1970. add_local_with_rel(Relobj* object, unsigned int sym_index,
  1971. unsigned int got_type, Output_data_reloc_generic* rel_dyn,
  1972. unsigned int r_type);
  1973. // Add a pair of entries for a local symbol to the GOT, and add
  1974. // a dynamic relocation of type R_TYPE using the section symbol of
  1975. // the output section to which input section SHNDX maps, on the first.
  1976. // The first got entry will have a value of zero, the second the
  1977. // value of the local symbol.
  1978. void
  1979. add_local_pair_with_rel(Relobj* object, unsigned int sym_index,
  1980. unsigned int shndx, unsigned int got_type,
  1981. Output_data_reloc_generic* rel_dyn,
  1982. unsigned int r_type);
  1983. // Add a pair of entries for a local symbol to the GOT, and add
  1984. // a dynamic relocation of type R_TYPE using STN_UNDEF on the first.
  1985. // The first got entry will have a value of zero, the second the
  1986. // value of the local symbol offset by Target::tls_offset_for_local.
  1987. void
  1988. add_local_tls_pair(Relobj* object, unsigned int sym_index,
  1989. unsigned int got_type,
  1990. Output_data_reloc_generic* rel_dyn,
  1991. unsigned int r_type);
  1992. // Add a constant to the GOT. This returns the offset of the new
  1993. // entry from the start of the GOT.
  1994. unsigned int
  1995. add_constant(Valtype constant)
  1996. { return this->add_got_entry(Got_entry(constant)); }
  1997. // Add a pair of constants to the GOT. This returns the offset of
  1998. // the new entry from the start of the GOT.
  1999. unsigned int
  2000. add_constant_pair(Valtype c1, Valtype c2)
  2001. { return this->add_got_entry_pair(Got_entry(c1), Got_entry(c2)); }
  2002. // Replace GOT entry I with a new constant.
  2003. void
  2004. replace_constant(unsigned int i, Valtype constant)
  2005. {
  2006. this->replace_got_entry(i, Got_entry(constant));
  2007. }
  2008. // Reserve a slot in the GOT for a local symbol.
  2009. void
  2010. reserve_local(unsigned int i, Relobj* object, unsigned int sym_index,
  2011. unsigned int got_type);
  2012. // Reserve a slot in the GOT for a global symbol.
  2013. void
  2014. reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type);
  2015. protected:
  2016. // Write out the GOT table.
  2017. void
  2018. do_write(Output_file*);
  2019. // Write to a map file.
  2020. void
  2021. do_print_to_mapfile(Mapfile* mapfile) const
  2022. { mapfile->print_output_data(this, _("** GOT")); }
  2023. // Reserve the slot at index I in the GOT.
  2024. virtual void
  2025. do_reserve_slot(unsigned int i)
  2026. { this->free_list_.remove(i * got_size / 8, (i + 1) * got_size / 8); }
  2027. // Return the number of words in the GOT.
  2028. unsigned int
  2029. num_entries () const
  2030. { return this->entries_.size(); }
  2031. // Return the offset into the GOT of GOT entry I.
  2032. unsigned int
  2033. got_offset(unsigned int i) const
  2034. { return i * (got_size / 8); }
  2035. private:
  2036. // This POD class holds a single GOT entry.
  2037. class Got_entry
  2038. {
  2039. public:
  2040. // Create a zero entry.
  2041. Got_entry()
  2042. : local_sym_index_(RESERVED_CODE), use_plt_or_tls_offset_(false)
  2043. { this->u_.constant = 0; }
  2044. // Create a global symbol entry.
  2045. Got_entry(Symbol* gsym, bool use_plt_or_tls_offset)
  2046. : local_sym_index_(GSYM_CODE),
  2047. use_plt_or_tls_offset_(use_plt_or_tls_offset)
  2048. { this->u_.gsym = gsym; }
  2049. // Create a local symbol entry.
  2050. Got_entry(Relobj* object, unsigned int local_sym_index,
  2051. bool use_plt_or_tls_offset)
  2052. : local_sym_index_(local_sym_index),
  2053. use_plt_or_tls_offset_(use_plt_or_tls_offset)
  2054. {
  2055. gold_assert(local_sym_index != GSYM_CODE
  2056. && local_sym_index != CONSTANT_CODE
  2057. && local_sym_index != RESERVED_CODE
  2058. && local_sym_index == this->local_sym_index_);
  2059. this->u_.object = object;
  2060. }
  2061. // Create a constant entry. The constant is a host value--it will
  2062. // be swapped, if necessary, when it is written out.
  2063. explicit Got_entry(Valtype constant)
  2064. : local_sym_index_(CONSTANT_CODE), use_plt_or_tls_offset_(false)
  2065. { this->u_.constant = constant; }
  2066. // Write the GOT entry to an output view.
  2067. void
  2068. write(unsigned int got_indx, unsigned char* pov) const;
  2069. private:
  2070. enum
  2071. {
  2072. GSYM_CODE = 0x7fffffff,
  2073. CONSTANT_CODE = 0x7ffffffe,
  2074. RESERVED_CODE = 0x7ffffffd
  2075. };
  2076. union
  2077. {
  2078. // For a local symbol, the object.
  2079. Relobj* object;
  2080. // For a global symbol, the symbol.
  2081. Symbol* gsym;
  2082. // For a constant, the constant.
  2083. Valtype constant;
  2084. } u_;
  2085. // For a local symbol, the local symbol index. This is GSYM_CODE
  2086. // for a global symbol, or CONSTANT_CODE for a constant.
  2087. unsigned int local_sym_index_ : 31;
  2088. // Whether to use the PLT offset of the symbol if it has one.
  2089. // For TLS symbols, whether to offset the symbol value.
  2090. bool use_plt_or_tls_offset_ : 1;
  2091. };
  2092. typedef std::vector<Got_entry> Got_entries;
  2093. // Create a new GOT entry and return its offset.
  2094. unsigned int
  2095. add_got_entry(Got_entry got_entry);
  2096. // Create a pair of new GOT entries and return the offset of the first.
  2097. unsigned int
  2098. add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
  2099. // Replace GOT entry I with a new value.
  2100. void
  2101. replace_got_entry(unsigned int i, Got_entry got_entry);
  2102. // Return the offset into the GOT of the last entry added.
  2103. unsigned int
  2104. last_got_offset() const
  2105. { return this->got_offset(this->num_entries() - 1); }
  2106. // Set the size of the section.
  2107. void
  2108. set_got_size()
  2109. { this->set_current_data_size(this->got_offset(this->num_entries())); }
  2110. // The list of GOT entries.
  2111. Got_entries entries_;
  2112. // List of available regions within the section, for incremental
  2113. // update links.
  2114. Free_list free_list_;
  2115. };
  2116. // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
  2117. // section.
  2118. class Output_data_dynamic : public Output_section_data
  2119. {
  2120. public:
  2121. Output_data_dynamic(Stringpool* pool)
  2122. : Output_section_data(Output_data::default_alignment()),
  2123. entries_(), pool_(pool)
  2124. { }
  2125. // Add a new dynamic entry with a fixed numeric value.
  2126. void
  2127. add_constant(elfcpp::DT tag, unsigned int val)
  2128. { this->add_entry(Dynamic_entry(tag, val)); }
  2129. // Add a new dynamic entry with the address of output data.
  2130. void
  2131. add_section_address(elfcpp::DT tag, const Output_data* od)
  2132. { this->add_entry(Dynamic_entry(tag, od, false)); }
  2133. // Add a new dynamic entry with the address of output data
  2134. // plus a constant offset.
  2135. void
  2136. add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
  2137. unsigned int offset)
  2138. { this->add_entry(Dynamic_entry(tag, od, offset)); }
  2139. // Add a new dynamic entry with the size of output data.
  2140. void
  2141. add_section_size(elfcpp::DT tag, const Output_data* od)
  2142. { this->add_entry(Dynamic_entry(tag, od, true)); }
  2143. // Add a new dynamic entry with the total size of two output datas.
  2144. void
  2145. add_section_size(elfcpp::DT tag, const Output_data* od,
  2146. const Output_data* od2)
  2147. { this->add_entry(Dynamic_entry(tag, od, od2)); }
  2148. // Add a new dynamic entry with the address of a symbol.
  2149. void
  2150. add_symbol(elfcpp::DT tag, const Symbol* sym)
  2151. { this->add_entry(Dynamic_entry(tag, sym)); }
  2152. // Add a new dynamic entry with a string.
  2153. void
  2154. add_string(elfcpp::DT tag, const char* str)
  2155. { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
  2156. void
  2157. add_string(elfcpp::DT tag, const std::string& str)
  2158. { this->add_string(tag, str.c_str()); }
  2159. // Add a new dynamic entry with custom value.
  2160. void
  2161. add_custom(elfcpp::DT tag)
  2162. { this->add_entry(Dynamic_entry(tag)); }
  2163. protected:
  2164. // Adjust the output section to set the entry size.
  2165. void
  2166. do_adjust_output_section(Output_section*);
  2167. // Set the final data size.
  2168. void
  2169. set_final_data_size();
  2170. // Write out the dynamic entries.
  2171. void
  2172. do_write(Output_file*);
  2173. // Write to a map file.
  2174. void
  2175. do_print_to_mapfile(Mapfile* mapfile) const
  2176. { mapfile->print_output_data(this, _("** dynamic")); }
  2177. private:
  2178. // This POD class holds a single dynamic entry.
  2179. class Dynamic_entry
  2180. {
  2181. public:
  2182. // Create an entry with a fixed numeric value.
  2183. Dynamic_entry(elfcpp::DT tag, unsigned int val)
  2184. : tag_(tag), offset_(DYNAMIC_NUMBER)
  2185. { this->u_.val = val; }
  2186. // Create an entry with the size or address of a section.
  2187. Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
  2188. : tag_(tag),
  2189. offset_(section_size
  2190. ? DYNAMIC_SECTION_SIZE
  2191. : DYNAMIC_SECTION_ADDRESS)
  2192. {
  2193. this->u_.od = od;
  2194. this->od2 = NULL;
  2195. }
  2196. // Create an entry with the size of two sections.
  2197. Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
  2198. : tag_(tag),
  2199. offset_(DYNAMIC_SECTION_SIZE)
  2200. {
  2201. this->u_.od = od;
  2202. this->od2 = od2;
  2203. }
  2204. // Create an entry with the address of a section plus a constant offset.
  2205. Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
  2206. : tag_(tag),
  2207. offset_(offset)
  2208. { this->u_.od = od; }
  2209. // Create an entry with the address of a symbol.
  2210. Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
  2211. : tag_(tag), offset_(DYNAMIC_SYMBOL)
  2212. { this->u_.sym = sym; }
  2213. // Create an entry with a string.
  2214. Dynamic_entry(elfcpp::DT tag, const char* str)
  2215. : tag_(tag), offset_(DYNAMIC_STRING)
  2216. { this->u_.str = str; }
  2217. // Create an entry with a custom value.
  2218. Dynamic_entry(elfcpp::DT tag)
  2219. : tag_(tag), offset_(DYNAMIC_CUSTOM)
  2220. { }
  2221. // Return the tag of this entry.
  2222. elfcpp::DT
  2223. tag() const
  2224. { return this->tag_; }
  2225. // Write the dynamic entry to an output view.
  2226. template<int size, bool big_endian>
  2227. void
  2228. write(unsigned char* pov, const Stringpool*) const;
  2229. private:
  2230. // Classification is encoded in the OFFSET field.
  2231. enum Classification
  2232. {
  2233. // Section address.
  2234. DYNAMIC_SECTION_ADDRESS = 0,
  2235. // Number.
  2236. DYNAMIC_NUMBER = -1U,
  2237. // Section size.
  2238. DYNAMIC_SECTION_SIZE = -2U,
  2239. // Symbol adress.
  2240. DYNAMIC_SYMBOL = -3U,
  2241. // String.
  2242. DYNAMIC_STRING = -4U,
  2243. // Custom value.
  2244. DYNAMIC_CUSTOM = -5U
  2245. // Any other value indicates a section address plus OFFSET.
  2246. };
  2247. union
  2248. {
  2249. // For DYNAMIC_NUMBER.
  2250. unsigned int val;
  2251. // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
  2252. const Output_data* od;
  2253. // For DYNAMIC_SYMBOL.
  2254. const Symbol* sym;
  2255. // For DYNAMIC_STRING.
  2256. const char* str;
  2257. } u_;
  2258. // For DYNAMIC_SYMBOL with two sections.
  2259. const Output_data* od2;
  2260. // The dynamic tag.
  2261. elfcpp::DT tag_;
  2262. // The type of entry (Classification) or offset within a section.
  2263. unsigned int offset_;
  2264. };
  2265. // Add an entry to the list.
  2266. void
  2267. add_entry(const Dynamic_entry& entry)
  2268. { this->entries_.push_back(entry); }
  2269. // Sized version of write function.
  2270. template<int size, bool big_endian>
  2271. void
  2272. sized_write(Output_file* of);
  2273. // The type of the list of entries.
  2274. typedef std::vector<Dynamic_entry> Dynamic_entries;
  2275. // The entries.
  2276. Dynamic_entries entries_;
  2277. // The pool used for strings.
  2278. Stringpool* pool_;
  2279. };
  2280. // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
  2281. // which may be required if the object file has more than
  2282. // SHN_LORESERVE sections.
  2283. class Output_symtab_xindex : public Output_section_data
  2284. {
  2285. public:
  2286. Output_symtab_xindex(size_t symcount)
  2287. : Output_section_data(symcount * 4, 4, true),
  2288. entries_()
  2289. { }
  2290. // Add an entry: symbol number SYMNDX has section SHNDX.
  2291. void
  2292. add(unsigned int symndx, unsigned int shndx)
  2293. { this->entries_.push_back(std::make_pair(symndx, shndx)); }
  2294. protected:
  2295. void
  2296. do_write(Output_file*);
  2297. // Write to a map file.
  2298. void
  2299. do_print_to_mapfile(Mapfile* mapfile) const
  2300. { mapfile->print_output_data(this, _("** symtab xindex")); }
  2301. private:
  2302. template<bool big_endian>
  2303. void
  2304. endian_do_write(unsigned char*);
  2305. // It is likely that most symbols will not require entries. Rather
  2306. // than keep a vector for all symbols, we keep pairs of symbol index
  2307. // and section index.
  2308. typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
  2309. // The entries we need.
  2310. Xindex_entries entries_;
  2311. };
  2312. // A relaxed input section.
  2313. class Output_relaxed_input_section : public Output_section_data_build
  2314. {
  2315. public:
  2316. // We would like to call relobj->section_addralign(shndx) to get the
  2317. // alignment but we do not want the constructor to fail. So callers
  2318. // are repsonsible for ensuring that.
  2319. Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
  2320. uint64_t addralign)
  2321. : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
  2322. { }
  2323. // Return the Relobj of this relaxed input section.
  2324. Relobj*
  2325. relobj() const
  2326. { return this->relobj_; }
  2327. // Return the section index of this relaxed input section.
  2328. unsigned int
  2329. shndx() const
  2330. { return this->shndx_; }
  2331. protected:
  2332. void
  2333. set_relobj(Relobj* relobj)
  2334. { this->relobj_ = relobj; }
  2335. void
  2336. set_shndx(unsigned int shndx)
  2337. { this->shndx_ = shndx; }
  2338. private:
  2339. Relobj* relobj_;
  2340. unsigned int shndx_;
  2341. };
  2342. // This class describes properties of merge data sections. It is used
  2343. // as a key type for maps.
  2344. class Merge_section_properties
  2345. {
  2346. public:
  2347. Merge_section_properties(bool is_string, uint64_t entsize,
  2348. uint64_t addralign)
  2349. : is_string_(is_string), entsize_(entsize), addralign_(addralign)
  2350. { }
  2351. // Whether this equals to another Merge_section_properties MSP.
  2352. bool
  2353. eq(const Merge_section_properties& msp) const
  2354. {
  2355. return ((this->is_string_ == msp.is_string_)
  2356. && (this->entsize_ == msp.entsize_)
  2357. && (this->addralign_ == msp.addralign_));
  2358. }
  2359. // Compute a hash value for this using 64-bit FNV-1a hash.
  2360. size_t
  2361. hash_value() const
  2362. {
  2363. uint64_t h = 14695981039346656037ULL; // FNV offset basis.
  2364. uint64_t prime = 1099511628211ULL;
  2365. h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
  2366. h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
  2367. h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
  2368. return h;
  2369. }
  2370. // Functors for associative containers.
  2371. struct equal_to
  2372. {
  2373. bool
  2374. operator()(const Merge_section_properties& msp1,
  2375. const Merge_section_properties& msp2) const
  2376. { return msp1.eq(msp2); }
  2377. };
  2378. struct hash
  2379. {
  2380. size_t
  2381. operator()(const Merge_section_properties& msp) const
  2382. { return msp.hash_value(); }
  2383. };
  2384. private:
  2385. // Whether this merge data section is for strings.
  2386. bool is_string_;
  2387. // Entsize of this merge data section.
  2388. uint64_t entsize_;
  2389. // Address alignment.
  2390. uint64_t addralign_;
  2391. };
  2392. // This class is used to speed up look up of special input sections in an
  2393. // Output_section.
  2394. class Output_section_lookup_maps
  2395. {
  2396. public:
  2397. Output_section_lookup_maps()
  2398. : is_valid_(true), merge_sections_by_properties_(),
  2399. relaxed_input_sections_by_id_()
  2400. { }
  2401. // Whether the maps are valid.
  2402. bool
  2403. is_valid() const
  2404. { return this->is_valid_; }
  2405. // Invalidate the maps.
  2406. void
  2407. invalidate()
  2408. { this->is_valid_ = false; }
  2409. // Clear the maps.
  2410. void
  2411. clear()
  2412. {
  2413. this->merge_sections_by_properties_.clear();
  2414. this->relaxed_input_sections_by_id_.clear();
  2415. // A cleared map is valid.
  2416. this->is_valid_ = true;
  2417. }
  2418. // Find a merge section by merge section properties. Return NULL if none
  2419. // is found.
  2420. Output_merge_base*
  2421. find_merge_section(const Merge_section_properties& msp) const
  2422. {
  2423. gold_assert(this->is_valid_);
  2424. Merge_sections_by_properties::const_iterator p =
  2425. this->merge_sections_by_properties_.find(msp);
  2426. return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
  2427. }
  2428. // Add a merge section pointed by POMB with properties MSP.
  2429. void
  2430. add_merge_section(const Merge_section_properties& msp,
  2431. Output_merge_base* pomb)
  2432. {
  2433. std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
  2434. std::pair<Merge_sections_by_properties::iterator, bool> result =
  2435. this->merge_sections_by_properties_.insert(value);
  2436. gold_assert(result.second);
  2437. }
  2438. // Find a relaxed input section of OBJECT with index SHNDX.
  2439. Output_relaxed_input_section*
  2440. find_relaxed_input_section(const Relobj* object, unsigned int shndx) const
  2441. {
  2442. gold_assert(this->is_valid_);
  2443. Relaxed_input_sections_by_id::const_iterator p =
  2444. this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
  2445. return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
  2446. }
  2447. // Add a relaxed input section pointed by POMB and whose original input
  2448. // section is in OBJECT with index SHNDX.
  2449. void
  2450. add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
  2451. Output_relaxed_input_section* poris)
  2452. {
  2453. Const_section_id csid(relobj, shndx);
  2454. std::pair<Const_section_id, Output_relaxed_input_section*>
  2455. value(csid, poris);
  2456. std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
  2457. this->relaxed_input_sections_by_id_.insert(value);
  2458. gold_assert(result.second);
  2459. }
  2460. private:
  2461. typedef Unordered_map<Merge_section_properties, Output_merge_base*,
  2462. Merge_section_properties::hash,
  2463. Merge_section_properties::equal_to>
  2464. Merge_sections_by_properties;
  2465. typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
  2466. Const_section_id_hash>
  2467. Relaxed_input_sections_by_id;
  2468. // Whether this is valid
  2469. bool is_valid_;
  2470. // Merge sections by merge section properties.
  2471. Merge_sections_by_properties merge_sections_by_properties_;
  2472. // Relaxed sections by section IDs.
  2473. Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
  2474. };
  2475. // This abstract base class defines the interface for the
  2476. // types of methods used to fill free space left in an output
  2477. // section during an incremental link. These methods are used
  2478. // to insert dummy compilation units into debug info so that
  2479. // debug info consumers can scan the debug info serially.
  2480. class Output_fill
  2481. {
  2482. public:
  2483. Output_fill()
  2484. : is_big_endian_(parameters->target().is_big_endian())
  2485. { }
  2486. virtual
  2487. ~Output_fill()
  2488. { }
  2489. // Return the smallest size chunk of free space that can be
  2490. // filled with a dummy compilation unit.
  2491. size_t
  2492. minimum_hole_size() const
  2493. { return this->do_minimum_hole_size(); }
  2494. // Write a fill pattern of length LEN at offset OFF in the file.
  2495. void
  2496. write(Output_file* of, off_t off, size_t len) const
  2497. { this->do_write(of, off, len); }
  2498. protected:
  2499. virtual size_t
  2500. do_minimum_hole_size() const = 0;
  2501. virtual void
  2502. do_write(Output_file* of, off_t off, size_t len) const = 0;
  2503. bool
  2504. is_big_endian() const
  2505. { return this->is_big_endian_; }
  2506. private:
  2507. bool is_big_endian_;
  2508. };
  2509. // Fill method that introduces a dummy compilation unit in
  2510. // a .debug_info or .debug_types section.
  2511. class Output_fill_debug_info : public Output_fill
  2512. {
  2513. public:
  2514. Output_fill_debug_info(bool is_debug_types)
  2515. : is_debug_types_(is_debug_types)
  2516. { }
  2517. protected:
  2518. virtual size_t
  2519. do_minimum_hole_size() const;
  2520. virtual void
  2521. do_write(Output_file* of, off_t off, size_t len) const;
  2522. private:
  2523. // Version of the header.
  2524. static const int version = 4;
  2525. // True if this is a .debug_types section.
  2526. bool is_debug_types_;
  2527. };
  2528. // Fill method that introduces a dummy compilation unit in
  2529. // a .debug_line section.
  2530. class Output_fill_debug_line : public Output_fill
  2531. {
  2532. public:
  2533. Output_fill_debug_line()
  2534. { }
  2535. protected:
  2536. virtual size_t
  2537. do_minimum_hole_size() const;
  2538. virtual void
  2539. do_write(Output_file* of, off_t off, size_t len) const;
  2540. private:
  2541. // Version of the header. We write a DWARF-3 header because it's smaller
  2542. // and many tools have not yet been updated to understand the DWARF-4 header.
  2543. static const int version = 3;
  2544. // Length of the portion of the header that follows the header_length
  2545. // field. This includes the following fields:
  2546. // minimum_instruction_length, default_is_stmt, line_base, line_range,
  2547. // opcode_base, standard_opcode_lengths[], include_directories, filenames.
  2548. // The standard_opcode_lengths array is 12 bytes long, and the
  2549. // include_directories and filenames fields each contain only a single
  2550. // null byte.
  2551. static const size_t header_length = 19;
  2552. };
  2553. // An output section. We don't expect to have too many output
  2554. // sections, so we don't bother to do a template on the size.
  2555. class Output_section : public Output_data
  2556. {
  2557. public:
  2558. // Create an output section, giving the name, type, and flags.
  2559. Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
  2560. virtual ~Output_section();
  2561. // Add a new input section SHNDX, named NAME, with header SHDR, from
  2562. // object OBJECT. RELOC_SHNDX is the index of a relocation section
  2563. // which applies to this section, or 0 if none, or -1 if more than
  2564. // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
  2565. // in a linker script; in that case we need to keep track of input
  2566. // sections associated with an output section. Return the offset
  2567. // within the output section.
  2568. template<int size, bool big_endian>
  2569. off_t
  2570. add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
  2571. unsigned int shndx, const char* name,
  2572. const elfcpp::Shdr<size, big_endian>& shdr,
  2573. unsigned int reloc_shndx, bool have_sections_script);
  2574. // Add generated data POSD to this output section.
  2575. void
  2576. add_output_section_data(Output_section_data* posd);
  2577. // Add a relaxed input section PORIS called NAME to this output section
  2578. // with LAYOUT.
  2579. void
  2580. add_relaxed_input_section(Layout* layout,
  2581. Output_relaxed_input_section* poris,
  2582. const std::string& name);
  2583. // Return the section name.
  2584. const char*
  2585. name() const
  2586. { return this->name_; }
  2587. // Return the section type.
  2588. elfcpp::Elf_Word
  2589. type() const
  2590. { return this->type_; }
  2591. // Return the section flags.
  2592. elfcpp::Elf_Xword
  2593. flags() const
  2594. { return this->flags_; }
  2595. typedef std::map<Section_id, unsigned int> Section_layout_order;
  2596. void
  2597. update_section_layout(const Section_layout_order* order_map);
  2598. // Update the output section flags based on input section flags.
  2599. void
  2600. update_flags_for_input_section(elfcpp::Elf_Xword flags);
  2601. // Set the output section flags.
  2602. void
  2603. set_flags(elfcpp::Elf_Xword flags)
  2604. { this->flags_ = flags; }
  2605. // Return the entsize field.
  2606. uint64_t
  2607. entsize() const
  2608. { return this->entsize_; }
  2609. // Set the entsize field.
  2610. void
  2611. set_entsize(uint64_t v);
  2612. // Set the load address.
  2613. void
  2614. set_load_address(uint64_t load_address)
  2615. {
  2616. this->load_address_ = load_address;
  2617. this->has_load_address_ = true;
  2618. }
  2619. // Set the link field to the output section index of a section.
  2620. void
  2621. set_link_section(const Output_data* od)
  2622. {
  2623. gold_assert(this->link_ == 0
  2624. && !this->should_link_to_symtab_
  2625. && !this->should_link_to_dynsym_);
  2626. this->link_section_ = od;
  2627. }
  2628. // Set the link field to a constant.
  2629. void
  2630. set_link(unsigned int v)
  2631. {
  2632. gold_assert(this->link_section_ == NULL
  2633. && !this->should_link_to_symtab_
  2634. && !this->should_link_to_dynsym_);
  2635. this->link_ = v;
  2636. }
  2637. // Record that this section should link to the normal symbol table.
  2638. void
  2639. set_should_link_to_symtab()
  2640. {
  2641. gold_assert(this->link_section_ == NULL
  2642. && this->link_ == 0
  2643. && !this->should_link_to_dynsym_);
  2644. this->should_link_to_symtab_ = true;
  2645. }
  2646. // Record that this section should link to the dynamic symbol table.
  2647. void
  2648. set_should_link_to_dynsym()
  2649. {
  2650. gold_assert(this->link_section_ == NULL
  2651. && this->link_ == 0
  2652. && !this->should_link_to_symtab_);
  2653. this->should_link_to_dynsym_ = true;
  2654. }
  2655. // Return the info field.
  2656. unsigned int
  2657. info() const
  2658. {
  2659. gold_assert(this->info_section_ == NULL
  2660. && this->info_symndx_ == NULL);
  2661. return this->info_;
  2662. }
  2663. // Set the info field to the output section index of a section.
  2664. void
  2665. set_info_section(const Output_section* os)
  2666. {
  2667. gold_assert((this->info_section_ == NULL
  2668. || (this->info_section_ == os
  2669. && this->info_uses_section_index_))
  2670. && this->info_symndx_ == NULL
  2671. && this->info_ == 0);
  2672. this->info_section_ = os;
  2673. this->info_uses_section_index_= true;
  2674. }
  2675. // Set the info field to the symbol table index of a symbol.
  2676. void
  2677. set_info_symndx(const Symbol* sym)
  2678. {
  2679. gold_assert(this->info_section_ == NULL
  2680. && (this->info_symndx_ == NULL
  2681. || this->info_symndx_ == sym)
  2682. && this->info_ == 0);
  2683. this->info_symndx_ = sym;
  2684. }
  2685. // Set the info field to the symbol table index of a section symbol.
  2686. void
  2687. set_info_section_symndx(const Output_section* os)
  2688. {
  2689. gold_assert((this->info_section_ == NULL
  2690. || (this->info_section_ == os
  2691. && !this->info_uses_section_index_))
  2692. && this->info_symndx_ == NULL
  2693. && this->info_ == 0);
  2694. this->info_section_ = os;
  2695. this->info_uses_section_index_ = false;
  2696. }
  2697. // Set the info field to a constant.
  2698. void
  2699. set_info(unsigned int v)
  2700. {
  2701. gold_assert(this->info_section_ == NULL
  2702. && this->info_symndx_ == NULL
  2703. && (this->info_ == 0
  2704. || this->info_ == v));
  2705. this->info_ = v;
  2706. }
  2707. // Set the addralign field.
  2708. void
  2709. set_addralign(uint64_t v)
  2710. { this->addralign_ = v; }
  2711. void
  2712. checkpoint_set_addralign(uint64_t val)
  2713. {
  2714. if (this->checkpoint_ != NULL)
  2715. this->checkpoint_->set_addralign(val);
  2716. }
  2717. // Whether the output section index has been set.
  2718. bool
  2719. has_out_shndx() const
  2720. { return this->out_shndx_ != -1U; }
  2721. // Indicate that we need a symtab index.
  2722. void
  2723. set_needs_symtab_index()
  2724. { this->needs_symtab_index_ = true; }
  2725. // Return whether we need a symtab index.
  2726. bool
  2727. needs_symtab_index() const
  2728. { return this->needs_symtab_index_; }
  2729. // Get the symtab index.
  2730. unsigned int
  2731. symtab_index() const
  2732. {
  2733. gold_assert(this->symtab_index_ != 0);
  2734. return this->symtab_index_;
  2735. }
  2736. // Set the symtab index.
  2737. void
  2738. set_symtab_index(unsigned int index)
  2739. {
  2740. gold_assert(index != 0);
  2741. this->symtab_index_ = index;
  2742. }
  2743. // Indicate that we need a dynsym index.
  2744. void
  2745. set_needs_dynsym_index()
  2746. { this->needs_dynsym_index_ = true; }
  2747. // Return whether we need a dynsym index.
  2748. bool
  2749. needs_dynsym_index() const
  2750. { return this->needs_dynsym_index_; }
  2751. // Get the dynsym index.
  2752. unsigned int
  2753. dynsym_index() const
  2754. {
  2755. gold_assert(this->dynsym_index_ != 0);
  2756. return this->dynsym_index_;
  2757. }
  2758. // Set the dynsym index.
  2759. void
  2760. set_dynsym_index(unsigned int index)
  2761. {
  2762. gold_assert(index != 0);
  2763. this->dynsym_index_ = index;
  2764. }
  2765. // Sort the attached input sections.
  2766. void
  2767. sort_attached_input_sections();
  2768. // Return whether the input sections sections attachd to this output
  2769. // section may require sorting. This is used to handle constructor
  2770. // priorities compatibly with GNU ld.
  2771. bool
  2772. may_sort_attached_input_sections() const
  2773. { return this->may_sort_attached_input_sections_; }
  2774. // Record that the input sections attached to this output section
  2775. // may require sorting.
  2776. void
  2777. set_may_sort_attached_input_sections()
  2778. { this->may_sort_attached_input_sections_ = true; }
  2779. // Returns true if input sections must be sorted according to the
  2780. // order in which their name appear in the --section-ordering-file.
  2781. bool
  2782. input_section_order_specified()
  2783. { return this->input_section_order_specified_; }
  2784. // Record that input sections must be sorted as some of their names
  2785. // match the patterns specified through --section-ordering-file.
  2786. void
  2787. set_input_section_order_specified()
  2788. { this->input_section_order_specified_ = true; }
  2789. // Return whether the input sections attached to this output section
  2790. // require sorting. This is used to handle constructor priorities
  2791. // compatibly with GNU ld.
  2792. bool
  2793. must_sort_attached_input_sections() const
  2794. { return this->must_sort_attached_input_sections_; }
  2795. // Record that the input sections attached to this output section
  2796. // require sorting.
  2797. void
  2798. set_must_sort_attached_input_sections()
  2799. { this->must_sort_attached_input_sections_ = true; }
  2800. // Get the order in which this section appears in the PT_LOAD output
  2801. // segment.
  2802. Output_section_order
  2803. order() const
  2804. { return this->order_; }
  2805. // Set the order for this section.
  2806. void
  2807. set_order(Output_section_order order)
  2808. { this->order_ = order; }
  2809. // Return whether this section holds relro data--data which has
  2810. // dynamic relocations but which may be marked read-only after the
  2811. // dynamic relocations have been completed.
  2812. bool
  2813. is_relro() const
  2814. { return this->is_relro_; }
  2815. // Record that this section holds relro data.
  2816. void
  2817. set_is_relro()
  2818. { this->is_relro_ = true; }
  2819. // Record that this section does not hold relro data.
  2820. void
  2821. clear_is_relro()
  2822. { this->is_relro_ = false; }
  2823. // True if this is a small section: a section which holds small
  2824. // variables.
  2825. bool
  2826. is_small_section() const
  2827. { return this->is_small_section_; }
  2828. // Record that this is a small section.
  2829. void
  2830. set_is_small_section()
  2831. { this->is_small_section_ = true; }
  2832. // True if this is a large section: a section which holds large
  2833. // variables.
  2834. bool
  2835. is_large_section() const
  2836. { return this->is_large_section_; }
  2837. // Record that this is a large section.
  2838. void
  2839. set_is_large_section()
  2840. { this->is_large_section_ = true; }
  2841. // True if this is a large data (not BSS) section.
  2842. bool
  2843. is_large_data_section()
  2844. { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
  2845. // Return whether this section should be written after all the input
  2846. // sections are complete.
  2847. bool
  2848. after_input_sections() const
  2849. { return this->after_input_sections_; }
  2850. // Record that this section should be written after all the input
  2851. // sections are complete.
  2852. void
  2853. set_after_input_sections()
  2854. { this->after_input_sections_ = true; }
  2855. // Return whether this section requires postprocessing after all
  2856. // relocations have been applied.
  2857. bool
  2858. requires_postprocessing() const
  2859. { return this->requires_postprocessing_; }
  2860. bool
  2861. is_unique_segment() const
  2862. { return this->is_unique_segment_; }
  2863. void
  2864. set_is_unique_segment()
  2865. { this->is_unique_segment_ = true; }
  2866. uint64_t extra_segment_flags() const
  2867. { return this->extra_segment_flags_; }
  2868. void
  2869. set_extra_segment_flags(uint64_t flags)
  2870. { this->extra_segment_flags_ = flags; }
  2871. uint64_t segment_alignment() const
  2872. { return this->segment_alignment_; }
  2873. void
  2874. set_segment_alignment(uint64_t align)
  2875. { this->segment_alignment_ = align; }
  2876. // If a section requires postprocessing, return the buffer to use.
  2877. unsigned char*
  2878. postprocessing_buffer() const
  2879. {
  2880. gold_assert(this->postprocessing_buffer_ != NULL);
  2881. return this->postprocessing_buffer_;
  2882. }
  2883. // If a section requires postprocessing, create the buffer to use.
  2884. void
  2885. create_postprocessing_buffer();
  2886. // If a section requires postprocessing, this is the size of the
  2887. // buffer to which relocations should be applied.
  2888. off_t
  2889. postprocessing_buffer_size() const
  2890. { return this->current_data_size_for_child(); }
  2891. // Modify the section name. This is only permitted for an
  2892. // unallocated section, and only before the size has been finalized.
  2893. // Otherwise the name will not get into Layout::namepool_.
  2894. void
  2895. set_name(const char* newname)
  2896. {
  2897. gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
  2898. gold_assert(!this->is_data_size_valid());
  2899. this->name_ = newname;
  2900. }
  2901. // Return whether the offset OFFSET in the input section SHNDX in
  2902. // object OBJECT is being included in the link.
  2903. bool
  2904. is_input_address_mapped(const Relobj* object, unsigned int shndx,
  2905. off_t offset) const;
  2906. // Return the offset within the output section of OFFSET relative to
  2907. // the start of input section SHNDX in object OBJECT.
  2908. section_offset_type
  2909. output_offset(const Relobj* object, unsigned int shndx,
  2910. section_offset_type offset) const;
  2911. // Return the output virtual address of OFFSET relative to the start
  2912. // of input section SHNDX in object OBJECT.
  2913. uint64_t
  2914. output_address(const Relobj* object, unsigned int shndx,
  2915. off_t offset) const;
  2916. // Look for the merged section for input section SHNDX in object
  2917. // OBJECT. If found, return true, and set *ADDR to the address of
  2918. // the start of the merged section. This is not necessary the
  2919. // output offset corresponding to input offset 0 in the section,
  2920. // since the section may be mapped arbitrarily.
  2921. bool
  2922. find_starting_output_address(const Relobj* object, unsigned int shndx,
  2923. uint64_t* addr) const;
  2924. // Record that this output section was found in the SECTIONS clause
  2925. // of a linker script.
  2926. void
  2927. set_found_in_sections_clause()
  2928. { this->found_in_sections_clause_ = true; }
  2929. // Return whether this output section was found in the SECTIONS
  2930. // clause of a linker script.
  2931. bool
  2932. found_in_sections_clause() const
  2933. { return this->found_in_sections_clause_; }
  2934. // Write the section header into *OPHDR.
  2935. template<int size, bool big_endian>
  2936. void
  2937. write_header(const Layout*, const Stringpool*,
  2938. elfcpp::Shdr_write<size, big_endian>*) const;
  2939. // The next few calls are for linker script support.
  2940. // In some cases we need to keep a list of the input sections
  2941. // associated with this output section. We only need the list if we
  2942. // might have to change the offsets of the input section within the
  2943. // output section after we add the input section. The ordinary
  2944. // input sections will be written out when we process the object
  2945. // file, and as such we don't need to track them here. We do need
  2946. // to track Output_section_data objects here. We store instances of
  2947. // this structure in a std::vector, so it must be a POD. There can
  2948. // be many instances of this structure, so we use a union to save
  2949. // some space.
  2950. class Input_section
  2951. {
  2952. public:
  2953. Input_section()
  2954. : shndx_(0), p2align_(0)
  2955. {
  2956. this->u1_.data_size = 0;
  2957. this->u2_.object = NULL;
  2958. }
  2959. // For an ordinary input section.
  2960. Input_section(Relobj* object, unsigned int shndx, off_t data_size,
  2961. uint64_t addralign)
  2962. : shndx_(shndx),
  2963. p2align_(ffsll(static_cast<long long>(addralign))),
  2964. section_order_index_(0)
  2965. {
  2966. gold_assert(shndx != OUTPUT_SECTION_CODE
  2967. && shndx != MERGE_DATA_SECTION_CODE
  2968. && shndx != MERGE_STRING_SECTION_CODE
  2969. && shndx != RELAXED_INPUT_SECTION_CODE);
  2970. this->u1_.data_size = data_size;
  2971. this->u2_.object = object;
  2972. }
  2973. // For a non-merge output section.
  2974. Input_section(Output_section_data* posd)
  2975. : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
  2976. section_order_index_(0)
  2977. {
  2978. this->u1_.data_size = 0;
  2979. this->u2_.posd = posd;
  2980. }
  2981. // For a merge section.
  2982. Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
  2983. : shndx_(is_string
  2984. ? MERGE_STRING_SECTION_CODE
  2985. : MERGE_DATA_SECTION_CODE),
  2986. p2align_(0),
  2987. section_order_index_(0)
  2988. {
  2989. this->u1_.entsize = entsize;
  2990. this->u2_.posd = posd;
  2991. }
  2992. // For a relaxed input section.
  2993. Input_section(Output_relaxed_input_section* psection)
  2994. : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
  2995. section_order_index_(0)
  2996. {
  2997. this->u1_.data_size = 0;
  2998. this->u2_.poris = psection;
  2999. }
  3000. unsigned int
  3001. section_order_index() const
  3002. {
  3003. return this->section_order_index_;
  3004. }
  3005. void
  3006. set_section_order_index(unsigned int number)
  3007. {
  3008. this->section_order_index_ = number;
  3009. }
  3010. // The required alignment.
  3011. uint64_t
  3012. addralign() const
  3013. {
  3014. if (this->p2align_ != 0)
  3015. return static_cast<uint64_t>(1) << (this->p2align_ - 1);
  3016. else if (!this->is_input_section())
  3017. return this->u2_.posd->addralign();
  3018. else
  3019. return 0;
  3020. }
  3021. // Set the required alignment, which must be either 0 or a power of 2.
  3022. // For input sections that are sub-classes of Output_section_data, a
  3023. // alignment of zero means asking the underlying object for alignment.
  3024. void
  3025. set_addralign(uint64_t addralign)
  3026. {
  3027. if (addralign == 0)
  3028. this->p2align_ = 0;
  3029. else
  3030. {
  3031. gold_assert((addralign & (addralign - 1)) == 0);
  3032. this->p2align_ = ffsll(static_cast<long long>(addralign));
  3033. }
  3034. }
  3035. // Return the current required size, without finalization.
  3036. off_t
  3037. current_data_size() const;
  3038. // Return the required size.
  3039. off_t
  3040. data_size() const;
  3041. // Whether this is an input section.
  3042. bool
  3043. is_input_section() const
  3044. {
  3045. return (this->shndx_ != OUTPUT_SECTION_CODE
  3046. && this->shndx_ != MERGE_DATA_SECTION_CODE
  3047. && this->shndx_ != MERGE_STRING_SECTION_CODE
  3048. && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
  3049. }
  3050. // Return whether this is a merge section which matches the
  3051. // parameters.
  3052. bool
  3053. is_merge_section(bool is_string, uint64_t entsize,
  3054. uint64_t addralign) const
  3055. {
  3056. return (this->shndx_ == (is_string
  3057. ? MERGE_STRING_SECTION_CODE
  3058. : MERGE_DATA_SECTION_CODE)
  3059. && this->u1_.entsize == entsize
  3060. && this->addralign() == addralign);
  3061. }
  3062. // Return whether this is a merge section for some input section.
  3063. bool
  3064. is_merge_section() const
  3065. {
  3066. return (this->shndx_ == MERGE_DATA_SECTION_CODE
  3067. || this->shndx_ == MERGE_STRING_SECTION_CODE);
  3068. }
  3069. // Return whether this is a relaxed input section.
  3070. bool
  3071. is_relaxed_input_section() const
  3072. { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
  3073. // Return whether this is a generic Output_section_data.
  3074. bool
  3075. is_output_section_data() const
  3076. {
  3077. return this->shndx_ == OUTPUT_SECTION_CODE;
  3078. }
  3079. // Return the object for an input section.
  3080. Relobj*
  3081. relobj() const;
  3082. // Return the input section index for an input section.
  3083. unsigned int
  3084. shndx() const;
  3085. // For non-input-sections, return the associated Output_section_data
  3086. // object.
  3087. Output_section_data*
  3088. output_section_data() const
  3089. {
  3090. gold_assert(!this->is_input_section());
  3091. return this->u2_.posd;
  3092. }
  3093. // For a merge section, return the Output_merge_base pointer.
  3094. Output_merge_base*
  3095. output_merge_base() const
  3096. {
  3097. gold_assert(this->is_merge_section());
  3098. return this->u2_.pomb;
  3099. }
  3100. // Return the Output_relaxed_input_section object.
  3101. Output_relaxed_input_section*
  3102. relaxed_input_section() const
  3103. {
  3104. gold_assert(this->is_relaxed_input_section());
  3105. return this->u2_.poris;
  3106. }
  3107. // Set the output section.
  3108. void
  3109. set_output_section(Output_section* os)
  3110. {
  3111. gold_assert(!this->is_input_section());
  3112. Output_section_data* posd =
  3113. this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
  3114. posd->set_output_section(os);
  3115. }
  3116. // Set the address and file offset. This is called during
  3117. // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
  3118. // the enclosing section.
  3119. void
  3120. set_address_and_file_offset(uint64_t address, off_t file_offset,
  3121. off_t section_file_offset);
  3122. // Reset the address and file offset.
  3123. void
  3124. reset_address_and_file_offset();
  3125. // Finalize the data size.
  3126. void
  3127. finalize_data_size();
  3128. // Add an input section, for SHF_MERGE sections.
  3129. bool
  3130. add_input_section(Relobj* object, unsigned int shndx)
  3131. {
  3132. gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
  3133. || this->shndx_ == MERGE_STRING_SECTION_CODE);
  3134. return this->u2_.posd->add_input_section(object, shndx);
  3135. }
  3136. // Given an input OBJECT, an input section index SHNDX within that
  3137. // object, and an OFFSET relative to the start of that input
  3138. // section, return whether or not the output offset is known. If
  3139. // this function returns true, it sets *POUTPUT to the offset in
  3140. // the output section, relative to the start of the input section
  3141. // in the output section. *POUTPUT may be different from OFFSET
  3142. // for a merged section.
  3143. bool
  3144. output_offset(const Relobj* object, unsigned int shndx,
  3145. section_offset_type offset,
  3146. section_offset_type* poutput) const;
  3147. // Write out the data. This does nothing for an input section.
  3148. void
  3149. write(Output_file*);
  3150. // Write the data to a buffer. This does nothing for an input
  3151. // section.
  3152. void
  3153. write_to_buffer(unsigned char*);
  3154. // Print to a map file.
  3155. void
  3156. print_to_mapfile(Mapfile*) const;
  3157. // Print statistics about merge sections to stderr.
  3158. void
  3159. print_merge_stats(const char* section_name)
  3160. {
  3161. if (this->shndx_ == MERGE_DATA_SECTION_CODE
  3162. || this->shndx_ == MERGE_STRING_SECTION_CODE)
  3163. this->u2_.posd->print_merge_stats(section_name);
  3164. }
  3165. private:
  3166. // Code values which appear in shndx_. If the value is not one of
  3167. // these codes, it is the input section index in the object file.
  3168. enum
  3169. {
  3170. // An Output_section_data.
  3171. OUTPUT_SECTION_CODE = -1U,
  3172. // An Output_section_data for an SHF_MERGE section with
  3173. // SHF_STRINGS not set.
  3174. MERGE_DATA_SECTION_CODE = -2U,
  3175. // An Output_section_data for an SHF_MERGE section with
  3176. // SHF_STRINGS set.
  3177. MERGE_STRING_SECTION_CODE = -3U,
  3178. // An Output_section_data for a relaxed input section.
  3179. RELAXED_INPUT_SECTION_CODE = -4U
  3180. };
  3181. // For an ordinary input section, this is the section index in the
  3182. // input file. For an Output_section_data, this is
  3183. // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
  3184. // MERGE_STRING_SECTION_CODE.
  3185. unsigned int shndx_;
  3186. // The required alignment, stored as a power of 2.
  3187. unsigned int p2align_;
  3188. union
  3189. {
  3190. // For an ordinary input section, the section size.
  3191. off_t data_size;
  3192. // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
  3193. // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
  3194. // entity size.
  3195. uint64_t entsize;
  3196. } u1_;
  3197. union
  3198. {
  3199. // For an ordinary input section, the object which holds the
  3200. // input section.
  3201. Relobj* object;
  3202. // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
  3203. // MERGE_STRING_SECTION_CODE, the data.
  3204. Output_section_data* posd;
  3205. Output_merge_base* pomb;
  3206. // For RELAXED_INPUT_SECTION_CODE, the data.
  3207. Output_relaxed_input_section* poris;
  3208. } u2_;
  3209. // The line number of the pattern it matches in the --section-ordering-file
  3210. // file. It is 0 if does not match any pattern.
  3211. unsigned int section_order_index_;
  3212. };
  3213. // Store the list of input sections for this Output_section into the
  3214. // list passed in. This removes the input sections, leaving only
  3215. // any Output_section_data elements. This returns the size of those
  3216. // Output_section_data elements. ADDRESS is the address of this
  3217. // output section. FILL is the fill value to use, in case there are
  3218. // any spaces between the remaining Output_section_data elements.
  3219. uint64_t
  3220. get_input_sections(uint64_t address, const std::string& fill,
  3221. std::list<Input_section>*);
  3222. // Add a script input section. A script input section can either be
  3223. // a plain input section or a sub-class of Output_section_data.
  3224. void
  3225. add_script_input_section(const Input_section& input_section);
  3226. // Set the current size of the output section.
  3227. void
  3228. set_current_data_size(off_t size)
  3229. { this->set_current_data_size_for_child(size); }
  3230. // End of linker script support.
  3231. // Save states before doing section layout.
  3232. // This is used for relaxation.
  3233. void
  3234. save_states();
  3235. // Restore states prior to section layout.
  3236. void
  3237. restore_states();
  3238. // Discard states.
  3239. void
  3240. discard_states();
  3241. // Convert existing input sections to relaxed input sections.
  3242. void
  3243. convert_input_sections_to_relaxed_sections(
  3244. const std::vector<Output_relaxed_input_section*>& sections);
  3245. // Find a relaxed input section to an input section in OBJECT
  3246. // with index SHNDX. Return NULL if none is found.
  3247. const Output_relaxed_input_section*
  3248. find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
  3249. // Whether section offsets need adjustment due to relaxation.
  3250. bool
  3251. section_offsets_need_adjustment() const
  3252. { return this->section_offsets_need_adjustment_; }
  3253. // Set section_offsets_need_adjustment to be true.
  3254. void
  3255. set_section_offsets_need_adjustment()
  3256. { this->section_offsets_need_adjustment_ = true; }
  3257. // Set section_offsets_need_adjustment to be false.
  3258. void
  3259. clear_section_offsets_need_adjustment()
  3260. { this->section_offsets_need_adjustment_ = false; }
  3261. // Adjust section offsets of input sections in this. This is
  3262. // requires if relaxation caused some input sections to change sizes.
  3263. void
  3264. adjust_section_offsets();
  3265. // Whether this is a NOLOAD section.
  3266. bool
  3267. is_noload() const
  3268. { return this->is_noload_; }
  3269. // Set NOLOAD flag.
  3270. void
  3271. set_is_noload()
  3272. { this->is_noload_ = true; }
  3273. // Print merge statistics to stderr.
  3274. void
  3275. print_merge_stats();
  3276. // Set a fixed layout for the section. Used for incremental update links.
  3277. void
  3278. set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
  3279. uint64_t sh_addralign);
  3280. // Return TRUE if the section has a fixed layout.
  3281. bool
  3282. has_fixed_layout() const
  3283. { return this->has_fixed_layout_; }
  3284. // Set flag to allow patch space for this section. Used for full
  3285. // incremental links.
  3286. void
  3287. set_is_patch_space_allowed()
  3288. { this->is_patch_space_allowed_ = true; }
  3289. // Set a fill method to use for free space left in the output section
  3290. // during incremental links.
  3291. void
  3292. set_free_space_fill(Output_fill* free_space_fill)
  3293. {
  3294. this->free_space_fill_ = free_space_fill;
  3295. this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
  3296. }
  3297. // Reserve space within the fixed layout for the section. Used for
  3298. // incremental update links.
  3299. void
  3300. reserve(uint64_t sh_offset, uint64_t sh_size);
  3301. // Allocate space from the free list for the section. Used for
  3302. // incremental update links.
  3303. off_t
  3304. allocate(off_t len, uint64_t addralign);
  3305. typedef std::vector<Input_section> Input_section_list;
  3306. // Allow access to the input sections.
  3307. const Input_section_list&
  3308. input_sections() const
  3309. { return this->input_sections_; }
  3310. Input_section_list&
  3311. input_sections()
  3312. { return this->input_sections_; }
  3313. protected:
  3314. // Return the output section--i.e., the object itself.
  3315. Output_section*
  3316. do_output_section()
  3317. { return this; }
  3318. const Output_section*
  3319. do_output_section() const
  3320. { return this; }
  3321. // Return the section index in the output file.
  3322. unsigned int
  3323. do_out_shndx() const
  3324. {
  3325. gold_assert(this->out_shndx_ != -1U);
  3326. return this->out_shndx_;
  3327. }
  3328. // Set the output section index.
  3329. void
  3330. do_set_out_shndx(unsigned int shndx)
  3331. {
  3332. gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
  3333. this->out_shndx_ = shndx;
  3334. }
  3335. // Update the data size of the Output_section. For a typical
  3336. // Output_section, there is nothing to do, but if there are any
  3337. // Output_section_data objects we need to do a trial layout
  3338. // here.
  3339. virtual void
  3340. update_data_size();
  3341. // Set the final data size of the Output_section. For a typical
  3342. // Output_section, there is nothing to do, but if there are any
  3343. // Output_section_data objects we need to set their final addresses
  3344. // here.
  3345. virtual void
  3346. set_final_data_size();
  3347. // Reset the address and file offset.
  3348. void
  3349. do_reset_address_and_file_offset();
  3350. // Return true if address and file offset already have reset values. In
  3351. // other words, calling reset_address_and_file_offset will not change them.
  3352. bool
  3353. do_address_and_file_offset_have_reset_values() const;
  3354. // Write the data to the file. For a typical Output_section, this
  3355. // does nothing: the data is written out by calling Object::Relocate
  3356. // on each input object. But if there are any Output_section_data
  3357. // objects we do need to write them out here.
  3358. virtual void
  3359. do_write(Output_file*);
  3360. // Return the address alignment--function required by parent class.
  3361. uint64_t
  3362. do_addralign() const
  3363. { return this->addralign_; }
  3364. // Return whether there is a load address.
  3365. bool
  3366. do_has_load_address() const
  3367. { return this->has_load_address_; }
  3368. // Return the load address.
  3369. uint64_t
  3370. do_load_address() const
  3371. {
  3372. gold_assert(this->has_load_address_);
  3373. return this->load_address_;
  3374. }
  3375. // Return whether this is an Output_section.
  3376. bool
  3377. do_is_section() const
  3378. { return true; }
  3379. // Return whether this is a section of the specified type.
  3380. bool
  3381. do_is_section_type(elfcpp::Elf_Word type) const
  3382. { return this->type_ == type; }
  3383. // Return whether the specified section flag is set.
  3384. bool
  3385. do_is_section_flag_set(elfcpp::Elf_Xword flag) const
  3386. { return (this->flags_ & flag) != 0; }
  3387. // Set the TLS offset. Called only for SHT_TLS sections.
  3388. void
  3389. do_set_tls_offset(uint64_t tls_base);
  3390. // Return the TLS offset, relative to the base of the TLS segment.
  3391. // Valid only for SHT_TLS sections.
  3392. uint64_t
  3393. do_tls_offset() const
  3394. { return this->tls_offset_; }
  3395. // This may be implemented by a child class.
  3396. virtual void
  3397. do_finalize_name(Layout*)
  3398. { }
  3399. // Print to the map file.
  3400. virtual void
  3401. do_print_to_mapfile(Mapfile*) const;
  3402. // Record that this section requires postprocessing after all
  3403. // relocations have been applied. This is called by a child class.
  3404. void
  3405. set_requires_postprocessing()
  3406. {
  3407. this->requires_postprocessing_ = true;
  3408. this->after_input_sections_ = true;
  3409. }
  3410. // Write all the data of an Output_section into the postprocessing
  3411. // buffer.
  3412. void
  3413. write_to_postprocessing_buffer();
  3414. // Whether this always keeps an input section list
  3415. bool
  3416. always_keeps_input_sections() const
  3417. { return this->always_keeps_input_sections_; }
  3418. // Always keep an input section list.
  3419. void
  3420. set_always_keeps_input_sections()
  3421. {
  3422. gold_assert(this->current_data_size_for_child() == 0);
  3423. this->always_keeps_input_sections_ = true;
  3424. }
  3425. private:
  3426. // We only save enough information to undo the effects of section layout.
  3427. class Checkpoint_output_section
  3428. {
  3429. public:
  3430. Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
  3431. const Input_section_list& input_sections,
  3432. off_t first_input_offset,
  3433. bool attached_input_sections_are_sorted)
  3434. : addralign_(addralign), flags_(flags),
  3435. input_sections_(input_sections),
  3436. input_sections_size_(input_sections_.size()),
  3437. input_sections_copy_(), first_input_offset_(first_input_offset),
  3438. attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
  3439. { }
  3440. virtual
  3441. ~Checkpoint_output_section()
  3442. { }
  3443. // Return the address alignment.
  3444. uint64_t
  3445. addralign() const
  3446. { return this->addralign_; }
  3447. void
  3448. set_addralign(uint64_t val)
  3449. { this->addralign_ = val; }
  3450. // Return the section flags.
  3451. elfcpp::Elf_Xword
  3452. flags() const
  3453. { return this->flags_; }
  3454. // Return a reference to the input section list copy.
  3455. Input_section_list*
  3456. input_sections()
  3457. { return &this->input_sections_copy_; }
  3458. // Return the size of input_sections at the time when checkpoint is
  3459. // taken.
  3460. size_t
  3461. input_sections_size() const
  3462. { return this->input_sections_size_; }
  3463. // Whether input sections are copied.
  3464. bool
  3465. input_sections_saved() const
  3466. { return this->input_sections_copy_.size() == this->input_sections_size_; }
  3467. off_t
  3468. first_input_offset() const
  3469. { return this->first_input_offset_; }
  3470. bool
  3471. attached_input_sections_are_sorted() const
  3472. { return this->attached_input_sections_are_sorted_; }
  3473. // Save input sections.
  3474. void
  3475. save_input_sections()
  3476. {
  3477. this->input_sections_copy_.reserve(this->input_sections_size_);
  3478. this->input_sections_copy_.clear();
  3479. Input_section_list::const_iterator p = this->input_sections_.begin();
  3480. gold_assert(this->input_sections_size_ >= this->input_sections_.size());
  3481. for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
  3482. this->input_sections_copy_.push_back(*p);
  3483. }
  3484. private:
  3485. // The section alignment.
  3486. uint64_t addralign_;
  3487. // The section flags.
  3488. elfcpp::Elf_Xword flags_;
  3489. // Reference to the input sections to be checkpointed.
  3490. const Input_section_list& input_sections_;
  3491. // Size of the checkpointed portion of input_sections_;
  3492. size_t input_sections_size_;
  3493. // Copy of input sections.
  3494. Input_section_list input_sections_copy_;
  3495. // The offset of the first entry in input_sections_.
  3496. off_t first_input_offset_;
  3497. // True if the input sections attached to this output section have
  3498. // already been sorted.
  3499. bool attached_input_sections_are_sorted_;
  3500. };
  3501. // This class is used to sort the input sections.
  3502. class Input_section_sort_entry;
  3503. // This is the sort comparison function for ctors and dtors.
  3504. struct Input_section_sort_compare
  3505. {
  3506. bool
  3507. operator()(const Input_section_sort_entry&,
  3508. const Input_section_sort_entry&) const;
  3509. };
  3510. // This is the sort comparison function for .init_array and .fini_array.
  3511. struct Input_section_sort_init_fini_compare
  3512. {
  3513. bool
  3514. operator()(const Input_section_sort_entry&,
  3515. const Input_section_sort_entry&) const;
  3516. };
  3517. // This is the sort comparison function when a section order is specified
  3518. // from an input file.
  3519. struct Input_section_sort_section_order_index_compare
  3520. {
  3521. bool
  3522. operator()(const Input_section_sort_entry&,
  3523. const Input_section_sort_entry&) const;
  3524. };
  3525. // This is the sort comparison function for .text to sort sections with
  3526. // prefixes .text.{unlikely,exit,startup,hot} before other sections.
  3527. struct Input_section_sort_section_prefix_special_ordering_compare
  3528. {
  3529. bool
  3530. operator()(const Input_section_sort_entry&,
  3531. const Input_section_sort_entry&) const;
  3532. };
  3533. // This is the sort comparison function for sorting sections by name.
  3534. struct Input_section_sort_section_name_compare
  3535. {
  3536. bool
  3537. operator()(const Input_section_sort_entry&,
  3538. const Input_section_sort_entry&) const;
  3539. };
  3540. // Fill data. This is used to fill in data between input sections.
  3541. // It is also used for data statements (BYTE, WORD, etc.) in linker
  3542. // scripts. When we have to keep track of the input sections, we
  3543. // can use an Output_data_const, but we don't want to have to keep
  3544. // track of input sections just to implement fills.
  3545. class Fill
  3546. {
  3547. public:
  3548. Fill(off_t section_offset, off_t length)
  3549. : section_offset_(section_offset),
  3550. length_(convert_to_section_size_type(length))
  3551. { }
  3552. // Return section offset.
  3553. off_t
  3554. section_offset() const
  3555. { return this->section_offset_; }
  3556. // Return fill length.
  3557. section_size_type
  3558. length() const
  3559. { return this->length_; }
  3560. private:
  3561. // The offset within the output section.
  3562. off_t section_offset_;
  3563. // The length of the space to fill.
  3564. section_size_type length_;
  3565. };
  3566. typedef std::vector<Fill> Fill_list;
  3567. // Map used during relaxation of existing sections. This map
  3568. // a section id an input section list index. We assume that
  3569. // Input_section_list is a vector.
  3570. typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
  3571. // Add a new output section by Input_section.
  3572. void
  3573. add_output_section_data(Input_section*);
  3574. // Add an SHF_MERGE input section. Returns true if the section was
  3575. // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
  3576. // stores information about the merged input sections.
  3577. bool
  3578. add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
  3579. uint64_t entsize, uint64_t addralign,
  3580. bool keeps_input_sections);
  3581. // Add an output SHF_MERGE section POSD to this output section.
  3582. // IS_STRING indicates whether it is a SHF_STRINGS section, and
  3583. // ENTSIZE is the entity size. This returns the entry added to
  3584. // input_sections_.
  3585. void
  3586. add_output_merge_section(Output_section_data* posd, bool is_string,
  3587. uint64_t entsize);
  3588. // Find the merge section into which an input section with index SHNDX in
  3589. // OBJECT has been added. Return NULL if none found.
  3590. const Output_section_data*
  3591. find_merge_section(const Relobj* object, unsigned int shndx) const;
  3592. // Build a relaxation map.
  3593. void
  3594. build_relaxation_map(
  3595. const Input_section_list& input_sections,
  3596. size_t limit,
  3597. Relaxation_map* map) const;
  3598. // Convert input sections in an input section list into relaxed sections.
  3599. void
  3600. convert_input_sections_in_list_to_relaxed_sections(
  3601. const std::vector<Output_relaxed_input_section*>& relaxed_sections,
  3602. const Relaxation_map& map,
  3603. Input_section_list* input_sections);
  3604. // Build the lookup maps for merge and relaxed input sections.
  3605. void
  3606. build_lookup_maps() const;
  3607. // Most of these fields are only valid after layout.
  3608. // The name of the section. This will point into a Stringpool.
  3609. const char* name_;
  3610. // The section address is in the parent class.
  3611. // The section alignment.
  3612. uint64_t addralign_;
  3613. // The section entry size.
  3614. uint64_t entsize_;
  3615. // The load address. This is only used when using a linker script
  3616. // with a SECTIONS clause. The has_load_address_ field indicates
  3617. // whether this field is valid.
  3618. uint64_t load_address_;
  3619. // The file offset is in the parent class.
  3620. // Set the section link field to the index of this section.
  3621. const Output_data* link_section_;
  3622. // If link_section_ is NULL, this is the link field.
  3623. unsigned int link_;
  3624. // Set the section info field to the index of this section.
  3625. const Output_section* info_section_;
  3626. // If info_section_ is NULL, set the info field to the symbol table
  3627. // index of this symbol.
  3628. const Symbol* info_symndx_;
  3629. // If info_section_ and info_symndx_ are NULL, this is the section
  3630. // info field.
  3631. unsigned int info_;
  3632. // The section type.
  3633. const elfcpp::Elf_Word type_;
  3634. // The section flags.
  3635. elfcpp::Elf_Xword flags_;
  3636. // The order of this section in the output segment.
  3637. Output_section_order order_;
  3638. // The section index.
  3639. unsigned int out_shndx_;
  3640. // If there is a STT_SECTION for this output section in the normal
  3641. // symbol table, this is the symbol index. This starts out as zero.
  3642. // It is initialized in Layout::finalize() to be the index, or -1U
  3643. // if there isn't one.
  3644. unsigned int symtab_index_;
  3645. // If there is a STT_SECTION for this output section in the dynamic
  3646. // symbol table, this is the symbol index. This starts out as zero.
  3647. // It is initialized in Layout::finalize() to be the index, or -1U
  3648. // if there isn't one.
  3649. unsigned int dynsym_index_;
  3650. // The input sections. This will be empty in cases where we don't
  3651. // need to keep track of them.
  3652. Input_section_list input_sections_;
  3653. // The offset of the first entry in input_sections_.
  3654. off_t first_input_offset_;
  3655. // The fill data. This is separate from input_sections_ because we
  3656. // often will need fill sections without needing to keep track of
  3657. // input sections.
  3658. Fill_list fills_;
  3659. // If the section requires postprocessing, this buffer holds the
  3660. // section contents during relocation.
  3661. unsigned char* postprocessing_buffer_;
  3662. // Whether this output section needs a STT_SECTION symbol in the
  3663. // normal symbol table. This will be true if there is a relocation
  3664. // which needs it.
  3665. bool needs_symtab_index_ : 1;
  3666. // Whether this output section needs a STT_SECTION symbol in the
  3667. // dynamic symbol table. This will be true if there is a dynamic
  3668. // relocation which needs it.
  3669. bool needs_dynsym_index_ : 1;
  3670. // Whether the link field of this output section should point to the
  3671. // normal symbol table.
  3672. bool should_link_to_symtab_ : 1;
  3673. // Whether the link field of this output section should point to the
  3674. // dynamic symbol table.
  3675. bool should_link_to_dynsym_ : 1;
  3676. // Whether this section should be written after all the input
  3677. // sections are complete.
  3678. bool after_input_sections_ : 1;
  3679. // Whether this section requires post processing after all
  3680. // relocations have been applied.
  3681. bool requires_postprocessing_ : 1;
  3682. // Whether an input section was mapped to this output section
  3683. // because of a SECTIONS clause in a linker script.
  3684. bool found_in_sections_clause_ : 1;
  3685. // Whether this section has an explicitly specified load address.
  3686. bool has_load_address_ : 1;
  3687. // True if the info_section_ field means the section index of the
  3688. // section, false if it means the symbol index of the corresponding
  3689. // section symbol.
  3690. bool info_uses_section_index_ : 1;
  3691. // True if input sections attached to this output section have to be
  3692. // sorted according to a specified order.
  3693. bool input_section_order_specified_ : 1;
  3694. // True if the input sections attached to this output section may
  3695. // need sorting.
  3696. bool may_sort_attached_input_sections_ : 1;
  3697. // True if the input sections attached to this output section must
  3698. // be sorted.
  3699. bool must_sort_attached_input_sections_ : 1;
  3700. // True if the input sections attached to this output section have
  3701. // already been sorted.
  3702. bool attached_input_sections_are_sorted_ : 1;
  3703. // True if this section holds relro data.
  3704. bool is_relro_ : 1;
  3705. // True if this is a small section.
  3706. bool is_small_section_ : 1;
  3707. // True if this is a large section.
  3708. bool is_large_section_ : 1;
  3709. // Whether code-fills are generated at write.
  3710. bool generate_code_fills_at_write_ : 1;
  3711. // Whether the entry size field should be zero.
  3712. bool is_entsize_zero_ : 1;
  3713. // Whether section offsets need adjustment due to relaxation.
  3714. bool section_offsets_need_adjustment_ : 1;
  3715. // Whether this is a NOLOAD section.
  3716. bool is_noload_ : 1;
  3717. // Whether this always keeps input section.
  3718. bool always_keeps_input_sections_ : 1;
  3719. // Whether this section has a fixed layout, for incremental update links.
  3720. bool has_fixed_layout_ : 1;
  3721. // True if we can add patch space to this section.
  3722. bool is_patch_space_allowed_ : 1;
  3723. // True if this output section goes into a unique segment.
  3724. bool is_unique_segment_ : 1;
  3725. // For SHT_TLS sections, the offset of this section relative to the base
  3726. // of the TLS segment.
  3727. uint64_t tls_offset_;
  3728. // Additional segment flags, specified via linker plugin, when mapping some
  3729. // input sections to unique segments.
  3730. uint64_t extra_segment_flags_;
  3731. // Segment alignment specified via linker plugin, when mapping some
  3732. // input sections to unique segments.
  3733. uint64_t segment_alignment_;
  3734. // Saved checkpoint.
  3735. Checkpoint_output_section* checkpoint_;
  3736. // Fast lookup maps for merged and relaxed input sections.
  3737. Output_section_lookup_maps* lookup_maps_;
  3738. // List of available regions within the section, for incremental
  3739. // update links.
  3740. Free_list free_list_;
  3741. // Method for filling chunks of free space.
  3742. Output_fill* free_space_fill_;
  3743. // Amount added as patch space for incremental linking.
  3744. off_t patch_space_;
  3745. };
  3746. // An output segment. PT_LOAD segments are built from collections of
  3747. // output sections. Other segments typically point within PT_LOAD
  3748. // segments, and are built directly as needed.
  3749. //
  3750. // NOTE: We want to use the copy constructor for this class. During
  3751. // relaxation, we may try built the segments multiple times. We do
  3752. // that by copying the original segment list before lay-out, doing
  3753. // a trial lay-out and roll-back to the saved copied if we need to
  3754. // to the lay-out again.
  3755. class Output_segment
  3756. {
  3757. public:
  3758. // Create an output segment, specifying the type and flags.
  3759. Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
  3760. // Return the virtual address.
  3761. uint64_t
  3762. vaddr() const
  3763. { return this->vaddr_; }
  3764. // Return the physical address.
  3765. uint64_t
  3766. paddr() const
  3767. { return this->paddr_; }
  3768. // Return the segment type.
  3769. elfcpp::Elf_Word
  3770. type() const
  3771. { return this->type_; }
  3772. // Return the segment flags.
  3773. elfcpp::Elf_Word
  3774. flags() const
  3775. { return this->flags_; }
  3776. // Return the memory size.
  3777. uint64_t
  3778. memsz() const
  3779. { return this->memsz_; }
  3780. // Return the file size.
  3781. off_t
  3782. filesz() const
  3783. { return this->filesz_; }
  3784. // Return the file offset.
  3785. off_t
  3786. offset() const
  3787. { return this->offset_; }
  3788. // Whether this is a segment created to hold large data sections.
  3789. bool
  3790. is_large_data_segment() const
  3791. { return this->is_large_data_segment_; }
  3792. // Record that this is a segment created to hold large data
  3793. // sections.
  3794. void
  3795. set_is_large_data_segment()
  3796. { this->is_large_data_segment_ = true; }
  3797. bool
  3798. is_unique_segment() const
  3799. { return this->is_unique_segment_; }
  3800. // Mark segment as unique, happens when linker plugins request that
  3801. // certain input sections be mapped to unique segments.
  3802. void
  3803. set_is_unique_segment()
  3804. { this->is_unique_segment_ = true; }
  3805. // Return the maximum alignment of the Output_data.
  3806. uint64_t
  3807. maximum_alignment();
  3808. // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
  3809. // the segment flags to use.
  3810. void
  3811. add_output_section_to_load(Layout* layout, Output_section* os,
  3812. elfcpp::Elf_Word seg_flags);
  3813. // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
  3814. // is the segment flags to use.
  3815. void
  3816. add_output_section_to_nonload(Output_section* os,
  3817. elfcpp::Elf_Word seg_flags);
  3818. // Remove an Output_section from this segment. It is an error if it
  3819. // is not present.
  3820. void
  3821. remove_output_section(Output_section* os);
  3822. // Add an Output_data (which need not be an Output_section) to the
  3823. // start of this segment.
  3824. void
  3825. add_initial_output_data(Output_data*);
  3826. // Return true if this segment has any sections which hold actual
  3827. // data, rather than being a BSS section.
  3828. bool
  3829. has_any_data_sections() const;
  3830. // Whether this segment has a dynamic relocs.
  3831. bool
  3832. has_dynamic_reloc() const;
  3833. // Return the first section.
  3834. Output_section*
  3835. first_section() const;
  3836. // Return the address of the first section.
  3837. uint64_t
  3838. first_section_load_address() const
  3839. {
  3840. const Output_section* os = this->first_section();
  3841. return os->has_load_address() ? os->load_address() : os->address();
  3842. }
  3843. // Return whether the addresses have been set already.
  3844. bool
  3845. are_addresses_set() const
  3846. { return this->are_addresses_set_; }
  3847. // Set the addresses.
  3848. void
  3849. set_addresses(uint64_t vaddr, uint64_t paddr)
  3850. {
  3851. this->vaddr_ = vaddr;
  3852. this->paddr_ = paddr;
  3853. this->are_addresses_set_ = true;
  3854. }
  3855. // Update the flags for the flags of an output section added to this
  3856. // segment.
  3857. void
  3858. update_flags_for_output_section(elfcpp::Elf_Xword flags)
  3859. {
  3860. // The ELF ABI specifies that a PT_TLS segment should always have
  3861. // PF_R as the flags.
  3862. if (this->type() != elfcpp::PT_TLS)
  3863. this->flags_ |= flags;
  3864. }
  3865. // Set the segment flags. This is only used if we have a PHDRS
  3866. // clause which explicitly specifies the flags.
  3867. void
  3868. set_flags(elfcpp::Elf_Word flags)
  3869. { this->flags_ = flags; }
  3870. // Set the address of the segment to ADDR and the offset to *POFF
  3871. // and set the addresses and offsets of all contained output
  3872. // sections accordingly. Set the section indexes of all contained
  3873. // output sections starting with *PSHNDX. If RESET is true, first
  3874. // reset the addresses of the contained sections. Return the
  3875. // address of the immediately following segment. Update *POFF and
  3876. // *PSHNDX. This should only be called for a PT_LOAD segment.
  3877. uint64_t
  3878. set_section_addresses(const Target*, Layout*, bool reset, uint64_t addr,
  3879. unsigned int* increase_relro, bool* has_relro,
  3880. off_t* poff, unsigned int* pshndx);
  3881. // Set the minimum alignment of this segment. This may be adjusted
  3882. // upward based on the section alignments.
  3883. void
  3884. set_minimum_p_align(uint64_t align)
  3885. {
  3886. if (align > this->min_p_align_)
  3887. this->min_p_align_ = align;
  3888. }
  3889. // Set the offset of this segment based on the section. This should
  3890. // only be called for a non-PT_LOAD segment.
  3891. void
  3892. set_offset(unsigned int increase);
  3893. // Set the TLS offsets of the sections contained in the PT_TLS segment.
  3894. void
  3895. set_tls_offsets();
  3896. // Return the number of output sections.
  3897. unsigned int
  3898. output_section_count() const;
  3899. // Return the section attached to the list segment with the lowest
  3900. // load address. This is used when handling a PHDRS clause in a
  3901. // linker script.
  3902. Output_section*
  3903. section_with_lowest_load_address() const;
  3904. // Write the segment header into *OPHDR.
  3905. template<int size, bool big_endian>
  3906. void
  3907. write_header(elfcpp::Phdr_write<size, big_endian>*);
  3908. // Write the section headers of associated sections into V.
  3909. template<int size, bool big_endian>
  3910. unsigned char*
  3911. write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
  3912. unsigned int* pshndx) const;
  3913. // Print the output sections in the map file.
  3914. void
  3915. print_sections_to_mapfile(Mapfile*) const;
  3916. private:
  3917. typedef std::vector<Output_data*> Output_data_list;
  3918. // Find the maximum alignment in an Output_data_list.
  3919. static uint64_t
  3920. maximum_alignment_list(const Output_data_list*);
  3921. // Return whether the first data section is a relro section.
  3922. bool
  3923. is_first_section_relro() const;
  3924. // Set the section addresses in an Output_data_list.
  3925. uint64_t
  3926. set_section_list_addresses(Layout*, bool reset, Output_data_list*,
  3927. uint64_t addr, off_t* poff, unsigned int* pshndx,
  3928. bool* in_tls);
  3929. // Return the number of Output_sections in an Output_data_list.
  3930. unsigned int
  3931. output_section_count_list(const Output_data_list*) const;
  3932. // Return whether an Output_data_list has a dynamic reloc.
  3933. bool
  3934. has_dynamic_reloc_list(const Output_data_list*) const;
  3935. // Find the section with the lowest load address in an
  3936. // Output_data_list.
  3937. void
  3938. lowest_load_address_in_list(const Output_data_list* pdl,
  3939. Output_section** found,
  3940. uint64_t* found_lma) const;
  3941. // Find the first and last entries by address.
  3942. void
  3943. find_first_and_last_list(const Output_data_list* pdl,
  3944. const Output_data** pfirst,
  3945. const Output_data** plast) const;
  3946. // Write the section headers in the list into V.
  3947. template<int size, bool big_endian>
  3948. unsigned char*
  3949. write_section_headers_list(const Layout*, const Stringpool*,
  3950. const Output_data_list*, unsigned char* v,
  3951. unsigned int* pshdx) const;
  3952. // Print a section list to the mapfile.
  3953. void
  3954. print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
  3955. // NOTE: We want to use the copy constructor. Currently, shallow copy
  3956. // works for us so we do not need to write our own copy constructor.
  3957. // The list of output data attached to this segment.
  3958. Output_data_list output_lists_[ORDER_MAX];
  3959. // The segment virtual address.
  3960. uint64_t vaddr_;
  3961. // The segment physical address.
  3962. uint64_t paddr_;
  3963. // The size of the segment in memory.
  3964. uint64_t memsz_;
  3965. // The maximum section alignment. The is_max_align_known_ field
  3966. // indicates whether this has been finalized.
  3967. uint64_t max_align_;
  3968. // The required minimum value for the p_align field. This is used
  3969. // for PT_LOAD segments. Note that this does not mean that
  3970. // addresses should be aligned to this value; it means the p_paddr
  3971. // and p_vaddr fields must be congruent modulo this value. For
  3972. // non-PT_LOAD segments, the dynamic linker works more efficiently
  3973. // if the p_align field has the more conventional value, although it
  3974. // can align as needed.
  3975. uint64_t min_p_align_;
  3976. // The offset of the segment data within the file.
  3977. off_t offset_;
  3978. // The size of the segment data in the file.
  3979. off_t filesz_;
  3980. // The segment type;
  3981. elfcpp::Elf_Word type_;
  3982. // The segment flags.
  3983. elfcpp::Elf_Word flags_;
  3984. // Whether we have finalized max_align_.
  3985. bool is_max_align_known_ : 1;
  3986. // Whether vaddr and paddr were set by a linker script.
  3987. bool are_addresses_set_ : 1;
  3988. // Whether this segment holds large data sections.
  3989. bool is_large_data_segment_ : 1;
  3990. // Whether this was marked as a unique segment via a linker plugin.
  3991. bool is_unique_segment_ : 1;
  3992. };
  3993. // This class represents the output file.
  3994. class Output_file
  3995. {
  3996. public:
  3997. Output_file(const char* name);
  3998. // Indicate that this is a temporary file which should not be
  3999. // output.
  4000. void
  4001. set_is_temporary()
  4002. { this->is_temporary_ = true; }
  4003. // Try to open an existing file. Returns false if the file doesn't
  4004. // exist, has a size of 0 or can't be mmaped. This method is
  4005. // thread-unsafe. If BASE_NAME is not NULL, use the contents of
  4006. // that file as the base for incremental linking.
  4007. bool
  4008. open_base_file(const char* base_name, bool writable);
  4009. // Open the output file. FILE_SIZE is the final size of the file.
  4010. // If the file already exists, it is deleted/truncated. This method
  4011. // is thread-unsafe.
  4012. void
  4013. open(off_t file_size);
  4014. // Resize the output file. This method is thread-unsafe.
  4015. void
  4016. resize(off_t file_size);
  4017. // Close the output file (flushing all buffered data) and make sure
  4018. // there are no errors. This method is thread-unsafe.
  4019. void
  4020. close();
  4021. // Return the size of this file.
  4022. off_t
  4023. filesize()
  4024. { return this->file_size_; }
  4025. // Return the name of this file.
  4026. const char*
  4027. filename()
  4028. { return this->name_; }
  4029. // We currently always use mmap which makes the view handling quite
  4030. // simple. In the future we may support other approaches.
  4031. // Write data to the output file.
  4032. void
  4033. write(off_t offset, const void* data, size_t len)
  4034. { memcpy(this->base_ + offset, data, len); }
  4035. // Get a buffer to use to write to the file, given the offset into
  4036. // the file and the size.
  4037. unsigned char*
  4038. get_output_view(off_t start, size_t size)
  4039. {
  4040. gold_assert(start >= 0
  4041. && start + static_cast<off_t>(size) <= this->file_size_);
  4042. return this->base_ + start;
  4043. }
  4044. // VIEW must have been returned by get_output_view. Write the
  4045. // buffer to the file, passing in the offset and the size.
  4046. void
  4047. write_output_view(off_t, size_t, unsigned char*)
  4048. { }
  4049. // Get a read/write buffer. This is used when we want to write part
  4050. // of the file, read it in, and write it again.
  4051. unsigned char*
  4052. get_input_output_view(off_t start, size_t size)
  4053. { return this->get_output_view(start, size); }
  4054. // Write a read/write buffer back to the file.
  4055. void
  4056. write_input_output_view(off_t, size_t, unsigned char*)
  4057. { }
  4058. // Get a read buffer. This is used when we just want to read part
  4059. // of the file back it in.
  4060. const unsigned char*
  4061. get_input_view(off_t start, size_t size)
  4062. { return this->get_output_view(start, size); }
  4063. // Release a read bfufer.
  4064. void
  4065. free_input_view(off_t, size_t, const unsigned char*)
  4066. { }
  4067. private:
  4068. // Map the file into memory or, if that fails, allocate anonymous
  4069. // memory.
  4070. void
  4071. map();
  4072. // Allocate anonymous memory for the file.
  4073. bool
  4074. map_anonymous();
  4075. // Map the file into memory.
  4076. bool
  4077. map_no_anonymous(bool);
  4078. // Unmap the file from memory (and flush to disk buffers).
  4079. void
  4080. unmap();
  4081. // File name.
  4082. const char* name_;
  4083. // File descriptor.
  4084. int o_;
  4085. // File size.
  4086. off_t file_size_;
  4087. // Base of file mapped into memory.
  4088. unsigned char* base_;
  4089. // True iff base_ points to a memory buffer rather than an output file.
  4090. bool map_is_anonymous_;
  4091. // True if base_ was allocated using new rather than mmap.
  4092. bool map_is_allocated_;
  4093. // True if this is a temporary file which should not be output.
  4094. bool is_temporary_;
  4095. };
  4096. } // End namespace gold.
  4097. #endif // !defined(GOLD_OUTPUT_H)