script-sections.cc 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596
  1. // script-sections.cc -- linker script SECTIONS for gold
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cstring>
  19. #include <algorithm>
  20. #include <list>
  21. #include <map>
  22. #include <string>
  23. #include <vector>
  24. #include <fnmatch.h>
  25. #include "parameters.h"
  26. #include "object.h"
  27. #include "layout.h"
  28. #include "output.h"
  29. #include "script-c.h"
  30. #include "script.h"
  31. #include "script-sections.h"
  32. // Support for the SECTIONS clause in linker scripts.
  33. namespace gold
  34. {
  35. // A region of memory.
  36. class Memory_region
  37. {
  38. public:
  39. Memory_region(const char* name, size_t namelen, unsigned int attributes,
  40. Expression* start, Expression* length)
  41. : name_(name, namelen),
  42. attributes_(attributes),
  43. start_(start),
  44. length_(length),
  45. current_offset_(0),
  46. vma_sections_(),
  47. lma_sections_(),
  48. last_section_(NULL)
  49. { }
  50. // Return the name of this region.
  51. const std::string&
  52. name() const
  53. { return this->name_; }
  54. // Return the start address of this region.
  55. Expression*
  56. start_address() const
  57. { return this->start_; }
  58. // Return the length of this region.
  59. Expression*
  60. length() const
  61. { return this->length_; }
  62. // Print the region (when debugging).
  63. void
  64. print(FILE*) const;
  65. // Return true if <name,namelen> matches this region.
  66. bool
  67. name_match(const char* name, size_t namelen)
  68. {
  69. return (this->name_.length() == namelen
  70. && strncmp(this->name_.c_str(), name, namelen) == 0);
  71. }
  72. Expression*
  73. get_current_address() const
  74. {
  75. return
  76. script_exp_binary_add(this->start_,
  77. script_exp_integer(this->current_offset_));
  78. }
  79. void
  80. set_address(uint64_t addr, const Symbol_table* symtab, const Layout* layout)
  81. {
  82. uint64_t start = this->start_->eval(symtab, layout, false);
  83. uint64_t len = this->length_->eval(symtab, layout, false);
  84. if (addr < start || addr >= start + len)
  85. gold_error(_("address 0x%llx is not within region %s"),
  86. static_cast<unsigned long long>(addr),
  87. this->name_.c_str());
  88. else if (addr < start + this->current_offset_)
  89. gold_error(_("address 0x%llx moves dot backwards in region %s"),
  90. static_cast<unsigned long long>(addr),
  91. this->name_.c_str());
  92. this->current_offset_ = addr - start;
  93. }
  94. void
  95. increment_offset(std::string section_name, uint64_t amount,
  96. const Symbol_table* symtab, const Layout* layout)
  97. {
  98. this->current_offset_ += amount;
  99. if (this->current_offset_
  100. > this->length_->eval(symtab, layout, false))
  101. gold_error(_("section %s overflows end of region %s"),
  102. section_name.c_str(), this->name_.c_str());
  103. }
  104. // Returns true iff there is room left in this region
  105. // for AMOUNT more bytes of data.
  106. bool
  107. has_room_for(const Symbol_table* symtab, const Layout* layout,
  108. uint64_t amount) const
  109. {
  110. return (this->current_offset_ + amount
  111. < this->length_->eval(symtab, layout, false));
  112. }
  113. // Return true if the provided section flags
  114. // are compatible with this region's attributes.
  115. bool
  116. attributes_compatible(elfcpp::Elf_Xword flags, elfcpp::Elf_Xword type) const;
  117. void
  118. add_section(Output_section_definition* sec, bool vma)
  119. {
  120. if (vma)
  121. this->vma_sections_.push_back(sec);
  122. else
  123. this->lma_sections_.push_back(sec);
  124. }
  125. typedef std::vector<Output_section_definition*> Section_list;
  126. // Return the start of the list of sections
  127. // whose VMAs are taken from this region.
  128. Section_list::const_iterator
  129. get_vma_section_list_start() const
  130. { return this->vma_sections_.begin(); }
  131. // Return the start of the list of sections
  132. // whose LMAs are taken from this region.
  133. Section_list::const_iterator
  134. get_lma_section_list_start() const
  135. { return this->lma_sections_.begin(); }
  136. // Return the end of the list of sections
  137. // whose VMAs are taken from this region.
  138. Section_list::const_iterator
  139. get_vma_section_list_end() const
  140. { return this->vma_sections_.end(); }
  141. // Return the end of the list of sections
  142. // whose LMAs are taken from this region.
  143. Section_list::const_iterator
  144. get_lma_section_list_end() const
  145. { return this->lma_sections_.end(); }
  146. Output_section_definition*
  147. get_last_section() const
  148. { return this->last_section_; }
  149. void
  150. set_last_section(Output_section_definition* sec)
  151. { this->last_section_ = sec; }
  152. private:
  153. std::string name_;
  154. unsigned int attributes_;
  155. Expression* start_;
  156. Expression* length_;
  157. // The offset to the next free byte in the region.
  158. // Note - for compatibility with GNU LD we only maintain one offset
  159. // regardless of whether the region is being used for VMA values,
  160. // LMA values, or both.
  161. uint64_t current_offset_;
  162. // A list of sections whose VMAs are set inside this region.
  163. Section_list vma_sections_;
  164. // A list of sections whose LMAs are set inside this region.
  165. Section_list lma_sections_;
  166. // The latest section to make use of this region.
  167. Output_section_definition* last_section_;
  168. };
  169. // Return true if the provided section flags
  170. // are compatible with this region's attributes.
  171. bool
  172. Memory_region::attributes_compatible(elfcpp::Elf_Xword flags,
  173. elfcpp::Elf_Xword type) const
  174. {
  175. unsigned int attrs = this->attributes_;
  176. // No attributes means that this region is not compatible with anything.
  177. if (attrs == 0)
  178. return false;
  179. bool match = true;
  180. do
  181. {
  182. switch (attrs & - attrs)
  183. {
  184. case MEM_EXECUTABLE:
  185. if ((flags & elfcpp::SHF_EXECINSTR) == 0)
  186. match = false;
  187. break;
  188. case MEM_WRITEABLE:
  189. if ((flags & elfcpp::SHF_WRITE) == 0)
  190. match = false;
  191. break;
  192. case MEM_READABLE:
  193. // All sections are presumed readable.
  194. break;
  195. case MEM_ALLOCATABLE:
  196. if ((flags & elfcpp::SHF_ALLOC) == 0)
  197. match = false;
  198. break;
  199. case MEM_INITIALIZED:
  200. if ((type & elfcpp::SHT_NOBITS) != 0)
  201. match = false;
  202. break;
  203. }
  204. attrs &= ~ (attrs & - attrs);
  205. }
  206. while (attrs != 0);
  207. return match;
  208. }
  209. // Print a memory region.
  210. void
  211. Memory_region::print(FILE* f) const
  212. {
  213. fprintf(f, " %s", this->name_.c_str());
  214. unsigned int attrs = this->attributes_;
  215. if (attrs != 0)
  216. {
  217. fprintf(f, " (");
  218. do
  219. {
  220. switch (attrs & - attrs)
  221. {
  222. case MEM_EXECUTABLE: fputc('x', f); break;
  223. case MEM_WRITEABLE: fputc('w', f); break;
  224. case MEM_READABLE: fputc('r', f); break;
  225. case MEM_ALLOCATABLE: fputc('a', f); break;
  226. case MEM_INITIALIZED: fputc('i', f); break;
  227. default:
  228. gold_unreachable();
  229. }
  230. attrs &= ~ (attrs & - attrs);
  231. }
  232. while (attrs != 0);
  233. fputc(')', f);
  234. }
  235. fprintf(f, " : origin = ");
  236. this->start_->print(f);
  237. fprintf(f, ", length = ");
  238. this->length_->print(f);
  239. fprintf(f, "\n");
  240. }
  241. // Manage orphan sections. This is intended to be largely compatible
  242. // with the GNU linker. The Linux kernel implicitly relies on
  243. // something similar to the GNU linker's orphan placement. We
  244. // originally used a simpler scheme here, but it caused the kernel
  245. // build to fail, and was also rather inefficient.
  246. class Orphan_section_placement
  247. {
  248. private:
  249. typedef Script_sections::Elements_iterator Elements_iterator;
  250. public:
  251. Orphan_section_placement();
  252. // Handle an output section during initialization of this mapping.
  253. void
  254. output_section_init(const std::string& name, Output_section*,
  255. Elements_iterator location);
  256. // Initialize the last location.
  257. void
  258. last_init(Elements_iterator location);
  259. // Set *PWHERE to the address of an iterator pointing to the
  260. // location to use for an orphan section. Return true if the
  261. // iterator has a value, false otherwise.
  262. bool
  263. find_place(Output_section*, Elements_iterator** pwhere);
  264. // Update PLACE_LAST_ALLOC.
  265. void
  266. update_last_alloc(Elements_iterator where);
  267. // Return the iterator being used for sections at the very end of
  268. // the linker script.
  269. Elements_iterator
  270. last_place() const;
  271. private:
  272. // The places that we specifically recognize. This list is copied
  273. // from the GNU linker.
  274. enum Place_index
  275. {
  276. PLACE_TEXT,
  277. PLACE_RODATA,
  278. PLACE_DATA,
  279. PLACE_TLS,
  280. PLACE_TLS_BSS,
  281. PLACE_BSS,
  282. PLACE_LAST_ALLOC,
  283. PLACE_REL,
  284. PLACE_INTERP,
  285. PLACE_NONALLOC,
  286. PLACE_LAST,
  287. PLACE_MAX
  288. };
  289. // The information we keep for a specific place.
  290. struct Place
  291. {
  292. // The name of sections for this place.
  293. const char* name;
  294. // Whether we have a location for this place.
  295. bool have_location;
  296. // The iterator for this place.
  297. Elements_iterator location;
  298. };
  299. // Initialize one place element.
  300. void
  301. initialize_place(Place_index, const char*);
  302. // The places.
  303. Place places_[PLACE_MAX];
  304. // True if this is the first call to output_section_init.
  305. bool first_init_;
  306. };
  307. // Initialize Orphan_section_placement.
  308. Orphan_section_placement::Orphan_section_placement()
  309. : first_init_(true)
  310. {
  311. this->initialize_place(PLACE_TEXT, ".text");
  312. this->initialize_place(PLACE_RODATA, ".rodata");
  313. this->initialize_place(PLACE_DATA, ".data");
  314. this->initialize_place(PLACE_TLS, NULL);
  315. this->initialize_place(PLACE_TLS_BSS, NULL);
  316. this->initialize_place(PLACE_BSS, ".bss");
  317. this->initialize_place(PLACE_LAST_ALLOC, NULL);
  318. this->initialize_place(PLACE_REL, NULL);
  319. this->initialize_place(PLACE_INTERP, ".interp");
  320. this->initialize_place(PLACE_NONALLOC, NULL);
  321. this->initialize_place(PLACE_LAST, NULL);
  322. }
  323. // Initialize one place element.
  324. void
  325. Orphan_section_placement::initialize_place(Place_index index, const char* name)
  326. {
  327. this->places_[index].name = name;
  328. this->places_[index].have_location = false;
  329. }
  330. // While initializing the Orphan_section_placement information, this
  331. // is called once for each output section named in the linker script.
  332. // If we found an output section during the link, it will be passed in
  333. // OS.
  334. void
  335. Orphan_section_placement::output_section_init(const std::string& name,
  336. Output_section* os,
  337. Elements_iterator location)
  338. {
  339. bool first_init = this->first_init_;
  340. this->first_init_ = false;
  341. // Remember the last allocated section. Any orphan bss sections
  342. // will be placed after it.
  343. if (os != NULL
  344. && (os->flags() & elfcpp::SHF_ALLOC) != 0)
  345. {
  346. this->places_[PLACE_LAST_ALLOC].location = location;
  347. this->places_[PLACE_LAST_ALLOC].have_location = true;
  348. }
  349. for (int i = 0; i < PLACE_MAX; ++i)
  350. {
  351. if (this->places_[i].name != NULL && this->places_[i].name == name)
  352. {
  353. if (this->places_[i].have_location)
  354. {
  355. // We have already seen a section with this name.
  356. return;
  357. }
  358. this->places_[i].location = location;
  359. this->places_[i].have_location = true;
  360. // If we just found the .bss section, restart the search for
  361. // an unallocated section. This follows the GNU linker's
  362. // behaviour.
  363. if (i == PLACE_BSS)
  364. this->places_[PLACE_NONALLOC].have_location = false;
  365. return;
  366. }
  367. }
  368. // Relocation sections.
  369. if (!this->places_[PLACE_REL].have_location
  370. && os != NULL
  371. && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
  372. && (os->flags() & elfcpp::SHF_ALLOC) != 0)
  373. {
  374. this->places_[PLACE_REL].location = location;
  375. this->places_[PLACE_REL].have_location = true;
  376. }
  377. // We find the location for unallocated sections by finding the
  378. // first debugging or comment section after the BSS section (if
  379. // there is one).
  380. if (!this->places_[PLACE_NONALLOC].have_location
  381. && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
  382. {
  383. // We add orphan sections after the location in PLACES_. We
  384. // want to store unallocated sections before LOCATION. If this
  385. // is the very first section, we can't use it.
  386. if (!first_init)
  387. {
  388. --location;
  389. this->places_[PLACE_NONALLOC].location = location;
  390. this->places_[PLACE_NONALLOC].have_location = true;
  391. }
  392. }
  393. }
  394. // Initialize the last location.
  395. void
  396. Orphan_section_placement::last_init(Elements_iterator location)
  397. {
  398. this->places_[PLACE_LAST].location = location;
  399. this->places_[PLACE_LAST].have_location = true;
  400. }
  401. // Set *PWHERE to the address of an iterator pointing to the location
  402. // to use for an orphan section. Return true if the iterator has a
  403. // value, false otherwise.
  404. bool
  405. Orphan_section_placement::find_place(Output_section* os,
  406. Elements_iterator** pwhere)
  407. {
  408. // Figure out where OS should go. This is based on the GNU linker
  409. // code. FIXME: The GNU linker handles small data sections
  410. // specially, but we don't.
  411. elfcpp::Elf_Word type = os->type();
  412. elfcpp::Elf_Xword flags = os->flags();
  413. Place_index index;
  414. if ((flags & elfcpp::SHF_ALLOC) == 0
  415. && !Layout::is_debug_info_section(os->name()))
  416. index = PLACE_NONALLOC;
  417. else if ((flags & elfcpp::SHF_ALLOC) == 0)
  418. index = PLACE_LAST;
  419. else if (type == elfcpp::SHT_NOTE)
  420. index = PLACE_INTERP;
  421. else if ((flags & elfcpp::SHF_TLS) != 0)
  422. {
  423. if (type == elfcpp::SHT_NOBITS)
  424. index = PLACE_TLS_BSS;
  425. else
  426. index = PLACE_TLS;
  427. }
  428. else if (type == elfcpp::SHT_NOBITS)
  429. index = PLACE_BSS;
  430. else if ((flags & elfcpp::SHF_WRITE) != 0)
  431. index = PLACE_DATA;
  432. else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
  433. index = PLACE_REL;
  434. else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
  435. index = PLACE_RODATA;
  436. else
  437. index = PLACE_TEXT;
  438. // If we don't have a location yet, try to find one based on a
  439. // plausible ordering of sections.
  440. if (!this->places_[index].have_location)
  441. {
  442. Place_index follow;
  443. switch (index)
  444. {
  445. default:
  446. follow = PLACE_MAX;
  447. break;
  448. case PLACE_RODATA:
  449. follow = PLACE_TEXT;
  450. break;
  451. case PLACE_DATA:
  452. follow = PLACE_RODATA;
  453. if (!this->places_[PLACE_RODATA].have_location)
  454. follow = PLACE_TEXT;
  455. break;
  456. case PLACE_BSS:
  457. follow = PLACE_LAST_ALLOC;
  458. break;
  459. case PLACE_REL:
  460. follow = PLACE_TEXT;
  461. break;
  462. case PLACE_INTERP:
  463. follow = PLACE_TEXT;
  464. break;
  465. case PLACE_TLS:
  466. follow = PLACE_DATA;
  467. break;
  468. case PLACE_TLS_BSS:
  469. follow = PLACE_TLS;
  470. if (!this->places_[PLACE_TLS].have_location)
  471. follow = PLACE_DATA;
  472. break;
  473. }
  474. if (follow != PLACE_MAX && this->places_[follow].have_location)
  475. {
  476. // Set the location of INDEX to the location of FOLLOW. The
  477. // location of INDEX will then be incremented by the caller,
  478. // so anything in INDEX will continue to be after anything
  479. // in FOLLOW.
  480. this->places_[index].location = this->places_[follow].location;
  481. this->places_[index].have_location = true;
  482. }
  483. }
  484. *pwhere = &this->places_[index].location;
  485. bool ret = this->places_[index].have_location;
  486. // The caller will set the location.
  487. this->places_[index].have_location = true;
  488. return ret;
  489. }
  490. // Update PLACE_LAST_ALLOC.
  491. void
  492. Orphan_section_placement::update_last_alloc(Elements_iterator elem)
  493. {
  494. Elements_iterator prev = elem;
  495. --prev;
  496. if (this->places_[PLACE_LAST_ALLOC].have_location
  497. && this->places_[PLACE_LAST_ALLOC].location == prev)
  498. {
  499. this->places_[PLACE_LAST_ALLOC].have_location = true;
  500. this->places_[PLACE_LAST_ALLOC].location = elem;
  501. }
  502. }
  503. // Return the iterator being used for sections at the very end of the
  504. // linker script.
  505. Orphan_section_placement::Elements_iterator
  506. Orphan_section_placement::last_place() const
  507. {
  508. gold_assert(this->places_[PLACE_LAST].have_location);
  509. return this->places_[PLACE_LAST].location;
  510. }
  511. // An element in a SECTIONS clause.
  512. class Sections_element
  513. {
  514. public:
  515. Sections_element()
  516. { }
  517. virtual ~Sections_element()
  518. { }
  519. // Return whether an output section is relro.
  520. virtual bool
  521. is_relro() const
  522. { return false; }
  523. // Record that an output section is relro.
  524. virtual void
  525. set_is_relro()
  526. { }
  527. // Create any required output sections. The only real
  528. // implementation is in Output_section_definition.
  529. virtual void
  530. create_sections(Layout*)
  531. { }
  532. // Add any symbol being defined to the symbol table.
  533. virtual void
  534. add_symbols_to_table(Symbol_table*)
  535. { }
  536. // Finalize symbols and check assertions.
  537. virtual void
  538. finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
  539. { }
  540. // Return the output section name to use for an input file name and
  541. // section name. This only real implementation is in
  542. // Output_section_definition.
  543. virtual const char*
  544. output_section_name(const char*, const char*, Output_section***,
  545. Script_sections::Section_type*, bool*, bool)
  546. { return NULL; }
  547. // Initialize OSP with an output section.
  548. virtual void
  549. orphan_section_init(Orphan_section_placement*,
  550. Script_sections::Elements_iterator)
  551. { }
  552. // Set section addresses. This includes applying assignments if the
  553. // expression is an absolute value.
  554. virtual void
  555. set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
  556. uint64_t*)
  557. { }
  558. // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
  559. // this section is constrained, and the input sections do not match,
  560. // return the constraint, and set *POSD.
  561. virtual Section_constraint
  562. check_constraint(Output_section_definition**)
  563. { return CONSTRAINT_NONE; }
  564. // See if this is the alternate output section for a constrained
  565. // output section. If it is, transfer the Output_section and return
  566. // true. Otherwise return false.
  567. virtual bool
  568. alternate_constraint(Output_section_definition*, Section_constraint)
  569. { return false; }
  570. // Get the list of segments to use for an allocated section when
  571. // using a PHDRS clause. If this is an allocated section, return
  572. // the Output_section, and set *PHDRS_LIST (the first parameter) to
  573. // the list of PHDRS to which it should be attached. If the PHDRS
  574. // were not specified, don't change *PHDRS_LIST. When not returning
  575. // NULL, set *ORPHAN (the second parameter) according to whether
  576. // this is an orphan section--one that is not mentioned in the
  577. // linker script.
  578. virtual Output_section*
  579. allocate_to_segment(String_list**, bool*)
  580. { return NULL; }
  581. // Look for an output section by name and return the address, the
  582. // load address, the alignment, and the size. This is used when an
  583. // expression refers to an output section which was not actually
  584. // created. This returns true if the section was found, false
  585. // otherwise. The only real definition is for
  586. // Output_section_definition.
  587. virtual bool
  588. get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
  589. uint64_t*) const
  590. { return false; }
  591. // Return the associated Output_section if there is one.
  592. virtual Output_section*
  593. get_output_section() const
  594. { return NULL; }
  595. // Set the section's memory regions.
  596. virtual void
  597. set_memory_region(Memory_region*, bool)
  598. { gold_error(_("Attempt to set a memory region for a non-output section")); }
  599. // Print the element for debugging purposes.
  600. virtual void
  601. print(FILE* f) const = 0;
  602. };
  603. // An assignment in a SECTIONS clause outside of an output section.
  604. class Sections_element_assignment : public Sections_element
  605. {
  606. public:
  607. Sections_element_assignment(const char* name, size_t namelen,
  608. Expression* val, bool provide, bool hidden)
  609. : assignment_(name, namelen, false, val, provide, hidden)
  610. { }
  611. // Add the symbol to the symbol table.
  612. void
  613. add_symbols_to_table(Symbol_table* symtab)
  614. { this->assignment_.add_to_table(symtab); }
  615. // Finalize the symbol.
  616. void
  617. finalize_symbols(Symbol_table* symtab, const Layout* layout,
  618. uint64_t* dot_value)
  619. {
  620. this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
  621. }
  622. // Set the section address. There is no section here, but if the
  623. // value is absolute, we set the symbol. This permits us to use
  624. // absolute symbols when setting dot.
  625. void
  626. set_section_addresses(Symbol_table* symtab, Layout* layout,
  627. uint64_t* dot_value, uint64_t*, uint64_t*)
  628. {
  629. this->assignment_.set_if_absolute(symtab, layout, true, *dot_value, NULL);
  630. }
  631. // Print for debugging.
  632. void
  633. print(FILE* f) const
  634. {
  635. fprintf(f, " ");
  636. this->assignment_.print(f);
  637. }
  638. private:
  639. Symbol_assignment assignment_;
  640. };
  641. // An assignment to the dot symbol in a SECTIONS clause outside of an
  642. // output section.
  643. class Sections_element_dot_assignment : public Sections_element
  644. {
  645. public:
  646. Sections_element_dot_assignment(Expression* val)
  647. : val_(val)
  648. { }
  649. // Finalize the symbol.
  650. void
  651. finalize_symbols(Symbol_table* symtab, const Layout* layout,
  652. uint64_t* dot_value)
  653. {
  654. // We ignore the section of the result because outside of an
  655. // output section definition the dot symbol is always considered
  656. // to be absolute.
  657. *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
  658. NULL, NULL, NULL, false);
  659. }
  660. // Update the dot symbol while setting section addresses.
  661. void
  662. set_section_addresses(Symbol_table* symtab, Layout* layout,
  663. uint64_t* dot_value, uint64_t* dot_alignment,
  664. uint64_t* load_address)
  665. {
  666. *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
  667. NULL, NULL, dot_alignment, false);
  668. *load_address = *dot_value;
  669. }
  670. // Print for debugging.
  671. void
  672. print(FILE* f) const
  673. {
  674. fprintf(f, " . = ");
  675. this->val_->print(f);
  676. fprintf(f, "\n");
  677. }
  678. private:
  679. Expression* val_;
  680. };
  681. // An assertion in a SECTIONS clause outside of an output section.
  682. class Sections_element_assertion : public Sections_element
  683. {
  684. public:
  685. Sections_element_assertion(Expression* check, const char* message,
  686. size_t messagelen)
  687. : assertion_(check, message, messagelen)
  688. { }
  689. // Check the assertion.
  690. void
  691. finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
  692. { this->assertion_.check(symtab, layout); }
  693. // Print for debugging.
  694. void
  695. print(FILE* f) const
  696. {
  697. fprintf(f, " ");
  698. this->assertion_.print(f);
  699. }
  700. private:
  701. Script_assertion assertion_;
  702. };
  703. // An element in an output section in a SECTIONS clause.
  704. class Output_section_element
  705. {
  706. public:
  707. // A list of input sections.
  708. typedef std::list<Output_section::Input_section> Input_section_list;
  709. Output_section_element()
  710. { }
  711. virtual ~Output_section_element()
  712. { }
  713. // Return whether this element requires an output section to exist.
  714. virtual bool
  715. needs_output_section() const
  716. { return false; }
  717. // Add any symbol being defined to the symbol table.
  718. virtual void
  719. add_symbols_to_table(Symbol_table*)
  720. { }
  721. // Finalize symbols and check assertions.
  722. virtual void
  723. finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
  724. { }
  725. // Return whether this element matches FILE_NAME and SECTION_NAME.
  726. // The only real implementation is in Output_section_element_input.
  727. virtual bool
  728. match_name(const char*, const char*, bool *) const
  729. { return false; }
  730. // Set section addresses. This includes applying assignments if the
  731. // expression is an absolute value.
  732. virtual void
  733. set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
  734. uint64_t*, uint64_t*, Output_section**, std::string*,
  735. Input_section_list*)
  736. { }
  737. // Print the element for debugging purposes.
  738. virtual void
  739. print(FILE* f) const = 0;
  740. protected:
  741. // Return a fill string that is LENGTH bytes long, filling it with
  742. // FILL.
  743. std::string
  744. get_fill_string(const std::string* fill, section_size_type length) const;
  745. };
  746. std::string
  747. Output_section_element::get_fill_string(const std::string* fill,
  748. section_size_type length) const
  749. {
  750. std::string this_fill;
  751. this_fill.reserve(length);
  752. while (this_fill.length() + fill->length() <= length)
  753. this_fill += *fill;
  754. if (this_fill.length() < length)
  755. this_fill.append(*fill, 0, length - this_fill.length());
  756. return this_fill;
  757. }
  758. // A symbol assignment in an output section.
  759. class Output_section_element_assignment : public Output_section_element
  760. {
  761. public:
  762. Output_section_element_assignment(const char* name, size_t namelen,
  763. Expression* val, bool provide,
  764. bool hidden)
  765. : assignment_(name, namelen, false, val, provide, hidden)
  766. { }
  767. // Add the symbol to the symbol table.
  768. void
  769. add_symbols_to_table(Symbol_table* symtab)
  770. { this->assignment_.add_to_table(symtab); }
  771. // Finalize the symbol.
  772. void
  773. finalize_symbols(Symbol_table* symtab, const Layout* layout,
  774. uint64_t* dot_value, Output_section** dot_section)
  775. {
  776. this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
  777. *dot_section);
  778. }
  779. // Set the section address. There is no section here, but if the
  780. // value is absolute, we set the symbol. This permits us to use
  781. // absolute symbols when setting dot.
  782. void
  783. set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
  784. uint64_t, uint64_t* dot_value, uint64_t*,
  785. Output_section** dot_section, std::string*,
  786. Input_section_list*)
  787. {
  788. this->assignment_.set_if_absolute(symtab, layout, true, *dot_value,
  789. *dot_section);
  790. }
  791. // Print for debugging.
  792. void
  793. print(FILE* f) const
  794. {
  795. fprintf(f, " ");
  796. this->assignment_.print(f);
  797. }
  798. private:
  799. Symbol_assignment assignment_;
  800. };
  801. // An assignment to the dot symbol in an output section.
  802. class Output_section_element_dot_assignment : public Output_section_element
  803. {
  804. public:
  805. Output_section_element_dot_assignment(Expression* val)
  806. : val_(val)
  807. { }
  808. // An assignment to dot within an output section is enough to force
  809. // the output section to exist.
  810. bool
  811. needs_output_section() const
  812. { return true; }
  813. // Finalize the symbol.
  814. void
  815. finalize_symbols(Symbol_table* symtab, const Layout* layout,
  816. uint64_t* dot_value, Output_section** dot_section)
  817. {
  818. *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
  819. *dot_section, dot_section, NULL,
  820. true);
  821. }
  822. // Update the dot symbol while setting section addresses.
  823. void
  824. set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
  825. uint64_t, uint64_t* dot_value, uint64_t*,
  826. Output_section** dot_section, std::string*,
  827. Input_section_list*);
  828. // Print for debugging.
  829. void
  830. print(FILE* f) const
  831. {
  832. fprintf(f, " . = ");
  833. this->val_->print(f);
  834. fprintf(f, "\n");
  835. }
  836. private:
  837. Expression* val_;
  838. };
  839. // Update the dot symbol while setting section addresses.
  840. void
  841. Output_section_element_dot_assignment::set_section_addresses(
  842. Symbol_table* symtab,
  843. Layout* layout,
  844. Output_section* output_section,
  845. uint64_t,
  846. uint64_t* dot_value,
  847. uint64_t* dot_alignment,
  848. Output_section** dot_section,
  849. std::string* fill,
  850. Input_section_list*)
  851. {
  852. uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
  853. *dot_value, *dot_section,
  854. dot_section, dot_alignment,
  855. true);
  856. if (next_dot < *dot_value)
  857. gold_error(_("dot may not move backward"));
  858. if (next_dot > *dot_value && output_section != NULL)
  859. {
  860. section_size_type length = convert_to_section_size_type(next_dot
  861. - *dot_value);
  862. Output_section_data* posd;
  863. if (fill->empty())
  864. posd = new Output_data_zero_fill(length, 0);
  865. else
  866. {
  867. std::string this_fill = this->get_fill_string(fill, length);
  868. posd = new Output_data_const(this_fill, 0);
  869. }
  870. output_section->add_output_section_data(posd);
  871. layout->new_output_section_data_from_script(posd);
  872. }
  873. *dot_value = next_dot;
  874. }
  875. // An assertion in an output section.
  876. class Output_section_element_assertion : public Output_section_element
  877. {
  878. public:
  879. Output_section_element_assertion(Expression* check, const char* message,
  880. size_t messagelen)
  881. : assertion_(check, message, messagelen)
  882. { }
  883. void
  884. print(FILE* f) const
  885. {
  886. fprintf(f, " ");
  887. this->assertion_.print(f);
  888. }
  889. private:
  890. Script_assertion assertion_;
  891. };
  892. // We use a special instance of Output_section_data to handle BYTE,
  893. // SHORT, etc. This permits forward references to symbols in the
  894. // expressions.
  895. class Output_data_expression : public Output_section_data
  896. {
  897. public:
  898. Output_data_expression(int size, bool is_signed, Expression* val,
  899. const Symbol_table* symtab, const Layout* layout,
  900. uint64_t dot_value, Output_section* dot_section)
  901. : Output_section_data(size, 0, true),
  902. is_signed_(is_signed), val_(val), symtab_(symtab),
  903. layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
  904. { }
  905. protected:
  906. // Write the data to the output file.
  907. void
  908. do_write(Output_file*);
  909. // Write the data to a buffer.
  910. void
  911. do_write_to_buffer(unsigned char*);
  912. // Write to a map file.
  913. void
  914. do_print_to_mapfile(Mapfile* mapfile) const
  915. { mapfile->print_output_data(this, _("** expression")); }
  916. private:
  917. template<bool big_endian>
  918. void
  919. endian_write_to_buffer(uint64_t, unsigned char*);
  920. bool is_signed_;
  921. Expression* val_;
  922. const Symbol_table* symtab_;
  923. const Layout* layout_;
  924. uint64_t dot_value_;
  925. Output_section* dot_section_;
  926. };
  927. // Write the data element to the output file.
  928. void
  929. Output_data_expression::do_write(Output_file* of)
  930. {
  931. unsigned char* view = of->get_output_view(this->offset(), this->data_size());
  932. this->write_to_buffer(view);
  933. of->write_output_view(this->offset(), this->data_size(), view);
  934. }
  935. // Write the data element to a buffer.
  936. void
  937. Output_data_expression::do_write_to_buffer(unsigned char* buf)
  938. {
  939. uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
  940. true, this->dot_value_,
  941. this->dot_section_, NULL, NULL,
  942. false);
  943. if (parameters->target().is_big_endian())
  944. this->endian_write_to_buffer<true>(val, buf);
  945. else
  946. this->endian_write_to_buffer<false>(val, buf);
  947. }
  948. template<bool big_endian>
  949. void
  950. Output_data_expression::endian_write_to_buffer(uint64_t val,
  951. unsigned char* buf)
  952. {
  953. switch (this->data_size())
  954. {
  955. case 1:
  956. elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
  957. break;
  958. case 2:
  959. elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
  960. break;
  961. case 4:
  962. elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
  963. break;
  964. case 8:
  965. if (parameters->target().get_size() == 32)
  966. {
  967. val &= 0xffffffff;
  968. if (this->is_signed_ && (val & 0x80000000) != 0)
  969. val |= 0xffffffff00000000LL;
  970. }
  971. elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
  972. break;
  973. default:
  974. gold_unreachable();
  975. }
  976. }
  977. // A data item in an output section.
  978. class Output_section_element_data : public Output_section_element
  979. {
  980. public:
  981. Output_section_element_data(int size, bool is_signed, Expression* val)
  982. : size_(size), is_signed_(is_signed), val_(val)
  983. { }
  984. // If there is a data item, then we must create an output section.
  985. bool
  986. needs_output_section() const
  987. { return true; }
  988. // Finalize symbols--we just need to update dot.
  989. void
  990. finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
  991. Output_section**)
  992. { *dot_value += this->size_; }
  993. // Store the value in the section.
  994. void
  995. set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
  996. uint64_t* dot_value, uint64_t*, Output_section**,
  997. std::string*, Input_section_list*);
  998. // Print for debugging.
  999. void
  1000. print(FILE*) const;
  1001. private:
  1002. // The size in bytes.
  1003. int size_;
  1004. // Whether the value is signed.
  1005. bool is_signed_;
  1006. // The value.
  1007. Expression* val_;
  1008. };
  1009. // Store the value in the section.
  1010. void
  1011. Output_section_element_data::set_section_addresses(
  1012. Symbol_table* symtab,
  1013. Layout* layout,
  1014. Output_section* os,
  1015. uint64_t,
  1016. uint64_t* dot_value,
  1017. uint64_t*,
  1018. Output_section** dot_section,
  1019. std::string*,
  1020. Input_section_list*)
  1021. {
  1022. gold_assert(os != NULL);
  1023. Output_data_expression* expression =
  1024. new Output_data_expression(this->size_, this->is_signed_, this->val_,
  1025. symtab, layout, *dot_value, *dot_section);
  1026. os->add_output_section_data(expression);
  1027. layout->new_output_section_data_from_script(expression);
  1028. *dot_value += this->size_;
  1029. }
  1030. // Print for debugging.
  1031. void
  1032. Output_section_element_data::print(FILE* f) const
  1033. {
  1034. const char* s;
  1035. switch (this->size_)
  1036. {
  1037. case 1:
  1038. s = "BYTE";
  1039. break;
  1040. case 2:
  1041. s = "SHORT";
  1042. break;
  1043. case 4:
  1044. s = "LONG";
  1045. break;
  1046. case 8:
  1047. if (this->is_signed_)
  1048. s = "SQUAD";
  1049. else
  1050. s = "QUAD";
  1051. break;
  1052. default:
  1053. gold_unreachable();
  1054. }
  1055. fprintf(f, " %s(", s);
  1056. this->val_->print(f);
  1057. fprintf(f, ")\n");
  1058. }
  1059. // A fill value setting in an output section.
  1060. class Output_section_element_fill : public Output_section_element
  1061. {
  1062. public:
  1063. Output_section_element_fill(Expression* val)
  1064. : val_(val)
  1065. { }
  1066. // Update the fill value while setting section addresses.
  1067. void
  1068. set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
  1069. uint64_t, uint64_t* dot_value, uint64_t*,
  1070. Output_section** dot_section,
  1071. std::string* fill, Input_section_list*)
  1072. {
  1073. Output_section* fill_section;
  1074. uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
  1075. *dot_value, *dot_section,
  1076. &fill_section, NULL, false);
  1077. if (fill_section != NULL)
  1078. gold_warning(_("fill value is not absolute"));
  1079. // FIXME: The GNU linker supports fill values of arbitrary length.
  1080. unsigned char fill_buff[4];
  1081. elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
  1082. fill->assign(reinterpret_cast<char*>(fill_buff), 4);
  1083. }
  1084. // Print for debugging.
  1085. void
  1086. print(FILE* f) const
  1087. {
  1088. fprintf(f, " FILL(");
  1089. this->val_->print(f);
  1090. fprintf(f, ")\n");
  1091. }
  1092. private:
  1093. // The new fill value.
  1094. Expression* val_;
  1095. };
  1096. // An input section specification in an output section
  1097. class Output_section_element_input : public Output_section_element
  1098. {
  1099. public:
  1100. Output_section_element_input(const Input_section_spec* spec, bool keep);
  1101. // Finalize symbols--just update the value of the dot symbol.
  1102. void
  1103. finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
  1104. Output_section** dot_section)
  1105. {
  1106. *dot_value = this->final_dot_value_;
  1107. *dot_section = this->final_dot_section_;
  1108. }
  1109. // See whether we match FILE_NAME and SECTION_NAME as an input section.
  1110. // If we do then also indicate whether the section should be KEPT.
  1111. bool
  1112. match_name(const char* file_name, const char* section_name, bool* keep) const;
  1113. // Set the section address.
  1114. void
  1115. set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
  1116. uint64_t subalign, uint64_t* dot_value, uint64_t*,
  1117. Output_section**, std::string* fill,
  1118. Input_section_list*);
  1119. // Print for debugging.
  1120. void
  1121. print(FILE* f) const;
  1122. private:
  1123. // An input section pattern.
  1124. struct Input_section_pattern
  1125. {
  1126. std::string pattern;
  1127. bool pattern_is_wildcard;
  1128. Sort_wildcard sort;
  1129. Input_section_pattern(const char* patterna, size_t patternlena,
  1130. Sort_wildcard sorta)
  1131. : pattern(patterna, patternlena),
  1132. pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
  1133. sort(sorta)
  1134. { }
  1135. };
  1136. typedef std::vector<Input_section_pattern> Input_section_patterns;
  1137. // Filename_exclusions is a pair of filename pattern and a bool
  1138. // indicating whether the filename is a wildcard.
  1139. typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
  1140. // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
  1141. // indicates whether this is a wildcard pattern.
  1142. static inline bool
  1143. match(const char* string, const char* pattern, bool is_wildcard_pattern)
  1144. {
  1145. return (is_wildcard_pattern
  1146. ? fnmatch(pattern, string, 0) == 0
  1147. : strcmp(string, pattern) == 0);
  1148. }
  1149. // See if we match a file name.
  1150. bool
  1151. match_file_name(const char* file_name) const;
  1152. // The file name pattern. If this is the empty string, we match all
  1153. // files.
  1154. std::string filename_pattern_;
  1155. // Whether the file name pattern is a wildcard.
  1156. bool filename_is_wildcard_;
  1157. // How the file names should be sorted. This may only be
  1158. // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
  1159. Sort_wildcard filename_sort_;
  1160. // The list of file names to exclude.
  1161. Filename_exclusions filename_exclusions_;
  1162. // The list of input section patterns.
  1163. Input_section_patterns input_section_patterns_;
  1164. // Whether to keep this section when garbage collecting.
  1165. bool keep_;
  1166. // The value of dot after including all matching sections.
  1167. uint64_t final_dot_value_;
  1168. // The section where dot is defined after including all matching
  1169. // sections.
  1170. Output_section* final_dot_section_;
  1171. };
  1172. // Construct Output_section_element_input. The parser records strings
  1173. // as pointers into a copy of the script file, which will go away when
  1174. // parsing is complete. We make sure they are in std::string objects.
  1175. Output_section_element_input::Output_section_element_input(
  1176. const Input_section_spec* spec,
  1177. bool keep)
  1178. : filename_pattern_(),
  1179. filename_is_wildcard_(false),
  1180. filename_sort_(spec->file.sort),
  1181. filename_exclusions_(),
  1182. input_section_patterns_(),
  1183. keep_(keep),
  1184. final_dot_value_(0),
  1185. final_dot_section_(NULL)
  1186. {
  1187. // The filename pattern "*" is common, and matches all files. Turn
  1188. // it into the empty string.
  1189. if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
  1190. this->filename_pattern_.assign(spec->file.name.value,
  1191. spec->file.name.length);
  1192. this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
  1193. if (spec->input_sections.exclude != NULL)
  1194. {
  1195. for (String_list::const_iterator p =
  1196. spec->input_sections.exclude->begin();
  1197. p != spec->input_sections.exclude->end();
  1198. ++p)
  1199. {
  1200. bool is_wildcard = is_wildcard_string((*p).c_str());
  1201. this->filename_exclusions_.push_back(std::make_pair(*p,
  1202. is_wildcard));
  1203. }
  1204. }
  1205. if (spec->input_sections.sections != NULL)
  1206. {
  1207. Input_section_patterns& isp(this->input_section_patterns_);
  1208. for (String_sort_list::const_iterator p =
  1209. spec->input_sections.sections->begin();
  1210. p != spec->input_sections.sections->end();
  1211. ++p)
  1212. isp.push_back(Input_section_pattern(p->name.value, p->name.length,
  1213. p->sort));
  1214. }
  1215. }
  1216. // See whether we match FILE_NAME.
  1217. bool
  1218. Output_section_element_input::match_file_name(const char* file_name) const
  1219. {
  1220. if (!this->filename_pattern_.empty())
  1221. {
  1222. // If we were called with no filename, we refuse to match a
  1223. // pattern which requires a file name.
  1224. if (file_name == NULL)
  1225. return false;
  1226. if (!match(file_name, this->filename_pattern_.c_str(),
  1227. this->filename_is_wildcard_))
  1228. return false;
  1229. }
  1230. if (file_name != NULL)
  1231. {
  1232. // Now we have to see whether FILE_NAME matches one of the
  1233. // exclusion patterns, if any.
  1234. for (Filename_exclusions::const_iterator p =
  1235. this->filename_exclusions_.begin();
  1236. p != this->filename_exclusions_.end();
  1237. ++p)
  1238. {
  1239. if (match(file_name, p->first.c_str(), p->second))
  1240. return false;
  1241. }
  1242. }
  1243. return true;
  1244. }
  1245. // See whether we match FILE_NAME and SECTION_NAME. If we do then
  1246. // KEEP indicates whether the section should survive garbage collection.
  1247. bool
  1248. Output_section_element_input::match_name(const char* file_name,
  1249. const char* section_name,
  1250. bool *keep) const
  1251. {
  1252. if (!this->match_file_name(file_name))
  1253. return false;
  1254. *keep = this->keep_;
  1255. // If there are no section name patterns, then we match.
  1256. if (this->input_section_patterns_.empty())
  1257. return true;
  1258. // See whether we match the section name patterns.
  1259. for (Input_section_patterns::const_iterator p =
  1260. this->input_section_patterns_.begin();
  1261. p != this->input_section_patterns_.end();
  1262. ++p)
  1263. {
  1264. if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
  1265. return true;
  1266. }
  1267. // We didn't match any section names, so we didn't match.
  1268. return false;
  1269. }
  1270. // Information we use to sort the input sections.
  1271. class Input_section_info
  1272. {
  1273. public:
  1274. Input_section_info(const Output_section::Input_section& input_section)
  1275. : input_section_(input_section), section_name_(),
  1276. size_(0), addralign_(1)
  1277. { }
  1278. // Return the simple input section.
  1279. const Output_section::Input_section&
  1280. input_section() const
  1281. { return this->input_section_; }
  1282. // Return the object.
  1283. Relobj*
  1284. relobj() const
  1285. { return this->input_section_.relobj(); }
  1286. // Return the section index.
  1287. unsigned int
  1288. shndx()
  1289. { return this->input_section_.shndx(); }
  1290. // Return the section name.
  1291. const std::string&
  1292. section_name() const
  1293. { return this->section_name_; }
  1294. // Set the section name.
  1295. void
  1296. set_section_name(const std::string name)
  1297. {
  1298. if (is_compressed_debug_section(name.c_str()))
  1299. this->section_name_ = corresponding_uncompressed_section_name(name);
  1300. else
  1301. this->section_name_ = name;
  1302. }
  1303. // Return the section size.
  1304. uint64_t
  1305. size() const
  1306. { return this->size_; }
  1307. // Set the section size.
  1308. void
  1309. set_size(uint64_t size)
  1310. { this->size_ = size; }
  1311. // Return the address alignment.
  1312. uint64_t
  1313. addralign() const
  1314. { return this->addralign_; }
  1315. // Set the address alignment.
  1316. void
  1317. set_addralign(uint64_t addralign)
  1318. { this->addralign_ = addralign; }
  1319. private:
  1320. // Input section, can be a relaxed section.
  1321. Output_section::Input_section input_section_;
  1322. // Name of the section.
  1323. std::string section_name_;
  1324. // Section size.
  1325. uint64_t size_;
  1326. // Address alignment.
  1327. uint64_t addralign_;
  1328. };
  1329. // A class to sort the input sections.
  1330. class Input_section_sorter
  1331. {
  1332. public:
  1333. Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
  1334. : filename_sort_(filename_sort), section_sort_(section_sort)
  1335. { }
  1336. bool
  1337. operator()(const Input_section_info&, const Input_section_info&) const;
  1338. private:
  1339. static unsigned long
  1340. get_init_priority(const char*);
  1341. Sort_wildcard filename_sort_;
  1342. Sort_wildcard section_sort_;
  1343. };
  1344. // Return a relative priority of the section with the specified NAME
  1345. // (a lower value meand a higher priority), or 0 if it should be compared
  1346. // with others as strings.
  1347. // The implementation of this function is copied from ld/ldlang.c.
  1348. unsigned long
  1349. Input_section_sorter::get_init_priority(const char* name)
  1350. {
  1351. char* end;
  1352. unsigned long init_priority;
  1353. // GCC uses the following section names for the init_priority
  1354. // attribute with numerical values 101 and 65535 inclusive. A
  1355. // lower value means a higher priority.
  1356. //
  1357. // 1: .init_array.NNNN/.fini_array.NNNN: Where NNNN is the
  1358. // decimal numerical value of the init_priority attribute.
  1359. // The order of execution in .init_array is forward and
  1360. // .fini_array is backward.
  1361. // 2: .ctors.NNNN/.dtors.NNNN: Where NNNN is 65535 minus the
  1362. // decimal numerical value of the init_priority attribute.
  1363. // The order of execution in .ctors is backward and .dtors
  1364. // is forward.
  1365. if (strncmp(name, ".init_array.", 12) == 0
  1366. || strncmp(name, ".fini_array.", 12) == 0)
  1367. {
  1368. init_priority = strtoul(name + 12, &end, 10);
  1369. return *end ? 0 : init_priority;
  1370. }
  1371. else if (strncmp(name, ".ctors.", 7) == 0
  1372. || strncmp(name, ".dtors.", 7) == 0)
  1373. {
  1374. init_priority = strtoul(name + 7, &end, 10);
  1375. return *end ? 0 : 65535 - init_priority;
  1376. }
  1377. return 0;
  1378. }
  1379. bool
  1380. Input_section_sorter::operator()(const Input_section_info& isi1,
  1381. const Input_section_info& isi2) const
  1382. {
  1383. if (this->section_sort_ == SORT_WILDCARD_BY_INIT_PRIORITY)
  1384. {
  1385. unsigned long ip1 = get_init_priority(isi1.section_name().c_str());
  1386. unsigned long ip2 = get_init_priority(isi2.section_name().c_str());
  1387. if (ip1 != 0 && ip2 != 0 && ip1 != ip2)
  1388. return ip1 < ip2;
  1389. }
  1390. if (this->section_sort_ == SORT_WILDCARD_BY_NAME
  1391. || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
  1392. || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
  1393. && isi1.addralign() == isi2.addralign())
  1394. || this->section_sort_ == SORT_WILDCARD_BY_INIT_PRIORITY)
  1395. {
  1396. if (isi1.section_name() != isi2.section_name())
  1397. return isi1.section_name() < isi2.section_name();
  1398. }
  1399. if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
  1400. || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
  1401. || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
  1402. {
  1403. if (isi1.addralign() != isi2.addralign())
  1404. return isi1.addralign() < isi2.addralign();
  1405. }
  1406. if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
  1407. {
  1408. if (isi1.relobj()->name() != isi2.relobj()->name())
  1409. return (isi1.relobj()->name() < isi2.relobj()->name());
  1410. }
  1411. // Otherwise we leave them in the same order.
  1412. return false;
  1413. }
  1414. // Set the section address. Look in INPUT_SECTIONS for sections which
  1415. // match this spec, sort them as specified, and add them to the output
  1416. // section.
  1417. void
  1418. Output_section_element_input::set_section_addresses(
  1419. Symbol_table*,
  1420. Layout* layout,
  1421. Output_section* output_section,
  1422. uint64_t subalign,
  1423. uint64_t* dot_value,
  1424. uint64_t*,
  1425. Output_section** dot_section,
  1426. std::string* fill,
  1427. Input_section_list* input_sections)
  1428. {
  1429. // We build a list of sections which match each
  1430. // Input_section_pattern.
  1431. // If none of the patterns specify a sort option, we throw all
  1432. // matching input sections into a single bin, in the order we
  1433. // find them. Otherwise, we put matching input sections into
  1434. // a separate bin for each pattern, and sort each one as
  1435. // specified. Thus, an input section spec like this:
  1436. // *(.foo .bar)
  1437. // will group all .foo and .bar sections in the order seen,
  1438. // whereas this:
  1439. // *(.foo) *(.bar)
  1440. // will group all .foo sections followed by all .bar sections.
  1441. // This matches Gnu ld behavior.
  1442. // Things get really weird, though, when you add a sort spec
  1443. // on some, but not all, of the patterns, like this:
  1444. // *(SORT_BY_NAME(.foo) .bar)
  1445. // We do not attempt to match Gnu ld behavior in this case.
  1446. typedef std::vector<std::vector<Input_section_info> > Matching_sections;
  1447. size_t input_pattern_count = this->input_section_patterns_.size();
  1448. size_t bin_count = 1;
  1449. bool any_patterns_with_sort = false;
  1450. for (size_t i = 0; i < input_pattern_count; ++i)
  1451. {
  1452. const Input_section_pattern& isp(this->input_section_patterns_[i]);
  1453. if (isp.sort != SORT_WILDCARD_NONE)
  1454. any_patterns_with_sort = true;
  1455. }
  1456. if (any_patterns_with_sort)
  1457. bin_count = input_pattern_count;
  1458. Matching_sections matching_sections(bin_count);
  1459. // Look through the list of sections for this output section. Add
  1460. // each one which matches to one of the elements of
  1461. // MATCHING_SECTIONS.
  1462. Input_section_list::iterator p = input_sections->begin();
  1463. while (p != input_sections->end())
  1464. {
  1465. Relobj* relobj = p->relobj();
  1466. unsigned int shndx = p->shndx();
  1467. Input_section_info isi(*p);
  1468. // Calling section_name and section_addralign is not very
  1469. // efficient.
  1470. // Lock the object so that we can get information about the
  1471. // section. This is OK since we know we are single-threaded
  1472. // here.
  1473. {
  1474. const Task* task = reinterpret_cast<const Task*>(-1);
  1475. Task_lock_obj<Object> tl(task, relobj);
  1476. isi.set_section_name(relobj->section_name(shndx));
  1477. if (p->is_relaxed_input_section())
  1478. {
  1479. // We use current data size because relaxed section sizes may not
  1480. // have finalized yet.
  1481. isi.set_size(p->relaxed_input_section()->current_data_size());
  1482. isi.set_addralign(p->relaxed_input_section()->addralign());
  1483. }
  1484. else
  1485. {
  1486. isi.set_size(relobj->section_size(shndx));
  1487. isi.set_addralign(relobj->section_addralign(shndx));
  1488. }
  1489. }
  1490. if (!this->match_file_name(relobj->name().c_str()))
  1491. ++p;
  1492. else if (this->input_section_patterns_.empty())
  1493. {
  1494. matching_sections[0].push_back(isi);
  1495. p = input_sections->erase(p);
  1496. }
  1497. else
  1498. {
  1499. size_t i;
  1500. for (i = 0; i < input_pattern_count; ++i)
  1501. {
  1502. const Input_section_pattern&
  1503. isp(this->input_section_patterns_[i]);
  1504. if (match(isi.section_name().c_str(), isp.pattern.c_str(),
  1505. isp.pattern_is_wildcard))
  1506. break;
  1507. }
  1508. if (i >= input_pattern_count)
  1509. ++p;
  1510. else
  1511. {
  1512. if (i >= bin_count)
  1513. i = 0;
  1514. matching_sections[i].push_back(isi);
  1515. p = input_sections->erase(p);
  1516. }
  1517. }
  1518. }
  1519. // Look through MATCHING_SECTIONS. Sort each one as specified,
  1520. // using a stable sort so that we get the default order when
  1521. // sections are otherwise equal. Add each input section to the
  1522. // output section.
  1523. uint64_t dot = *dot_value;
  1524. for (size_t i = 0; i < bin_count; ++i)
  1525. {
  1526. if (matching_sections[i].empty())
  1527. continue;
  1528. gold_assert(output_section != NULL);
  1529. const Input_section_pattern& isp(this->input_section_patterns_[i]);
  1530. if (isp.sort != SORT_WILDCARD_NONE
  1531. || this->filename_sort_ != SORT_WILDCARD_NONE)
  1532. std::stable_sort(matching_sections[i].begin(),
  1533. matching_sections[i].end(),
  1534. Input_section_sorter(this->filename_sort_,
  1535. isp.sort));
  1536. for (std::vector<Input_section_info>::const_iterator p =
  1537. matching_sections[i].begin();
  1538. p != matching_sections[i].end();
  1539. ++p)
  1540. {
  1541. // Override the original address alignment if SUBALIGN is specified.
  1542. // We need to make a copy of the input section to modify the
  1543. // alignment.
  1544. Output_section::Input_section sis(p->input_section());
  1545. uint64_t this_subalign = sis.addralign();
  1546. if (!sis.is_input_section())
  1547. sis.output_section_data()->finalize_data_size();
  1548. uint64_t data_size = sis.data_size();
  1549. if (subalign > 0)
  1550. {
  1551. this_subalign = subalign;
  1552. sis.set_addralign(subalign);
  1553. }
  1554. uint64_t address = align_address(dot, this_subalign);
  1555. if (address > dot && !fill->empty())
  1556. {
  1557. section_size_type length =
  1558. convert_to_section_size_type(address - dot);
  1559. std::string this_fill = this->get_fill_string(fill, length);
  1560. Output_section_data* posd = new Output_data_const(this_fill, 0);
  1561. output_section->add_output_section_data(posd);
  1562. layout->new_output_section_data_from_script(posd);
  1563. }
  1564. output_section->add_script_input_section(sis);
  1565. dot = address + data_size;
  1566. }
  1567. }
  1568. // An SHF_TLS/SHT_NOBITS section does not take up any
  1569. // address space.
  1570. if (output_section == NULL
  1571. || (output_section->flags() & elfcpp::SHF_TLS) == 0
  1572. || output_section->type() != elfcpp::SHT_NOBITS)
  1573. *dot_value = dot;
  1574. this->final_dot_value_ = *dot_value;
  1575. this->final_dot_section_ = *dot_section;
  1576. }
  1577. // Print for debugging.
  1578. void
  1579. Output_section_element_input::print(FILE* f) const
  1580. {
  1581. fprintf(f, " ");
  1582. if (this->keep_)
  1583. fprintf(f, "KEEP(");
  1584. if (!this->filename_pattern_.empty())
  1585. {
  1586. bool need_close_paren = false;
  1587. switch (this->filename_sort_)
  1588. {
  1589. case SORT_WILDCARD_NONE:
  1590. break;
  1591. case SORT_WILDCARD_BY_NAME:
  1592. fprintf(f, "SORT_BY_NAME(");
  1593. need_close_paren = true;
  1594. break;
  1595. default:
  1596. gold_unreachable();
  1597. }
  1598. fprintf(f, "%s", this->filename_pattern_.c_str());
  1599. if (need_close_paren)
  1600. fprintf(f, ")");
  1601. }
  1602. if (!this->input_section_patterns_.empty()
  1603. || !this->filename_exclusions_.empty())
  1604. {
  1605. fprintf(f, "(");
  1606. bool need_space = false;
  1607. if (!this->filename_exclusions_.empty())
  1608. {
  1609. fprintf(f, "EXCLUDE_FILE(");
  1610. bool need_comma = false;
  1611. for (Filename_exclusions::const_iterator p =
  1612. this->filename_exclusions_.begin();
  1613. p != this->filename_exclusions_.end();
  1614. ++p)
  1615. {
  1616. if (need_comma)
  1617. fprintf(f, ", ");
  1618. fprintf(f, "%s", p->first.c_str());
  1619. need_comma = true;
  1620. }
  1621. fprintf(f, ")");
  1622. need_space = true;
  1623. }
  1624. for (Input_section_patterns::const_iterator p =
  1625. this->input_section_patterns_.begin();
  1626. p != this->input_section_patterns_.end();
  1627. ++p)
  1628. {
  1629. if (need_space)
  1630. fprintf(f, " ");
  1631. int close_parens = 0;
  1632. switch (p->sort)
  1633. {
  1634. case SORT_WILDCARD_NONE:
  1635. break;
  1636. case SORT_WILDCARD_BY_NAME:
  1637. fprintf(f, "SORT_BY_NAME(");
  1638. close_parens = 1;
  1639. break;
  1640. case SORT_WILDCARD_BY_ALIGNMENT:
  1641. fprintf(f, "SORT_BY_ALIGNMENT(");
  1642. close_parens = 1;
  1643. break;
  1644. case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
  1645. fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
  1646. close_parens = 2;
  1647. break;
  1648. case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
  1649. fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
  1650. close_parens = 2;
  1651. break;
  1652. case SORT_WILDCARD_BY_INIT_PRIORITY:
  1653. fprintf(f, "SORT_BY_INIT_PRIORITY(");
  1654. close_parens = 1;
  1655. break;
  1656. default:
  1657. gold_unreachable();
  1658. }
  1659. fprintf(f, "%s", p->pattern.c_str());
  1660. for (int i = 0; i < close_parens; ++i)
  1661. fprintf(f, ")");
  1662. need_space = true;
  1663. }
  1664. fprintf(f, ")");
  1665. }
  1666. if (this->keep_)
  1667. fprintf(f, ")");
  1668. fprintf(f, "\n");
  1669. }
  1670. // An output section.
  1671. class Output_section_definition : public Sections_element
  1672. {
  1673. public:
  1674. typedef Output_section_element::Input_section_list Input_section_list;
  1675. Output_section_definition(const char* name, size_t namelen,
  1676. const Parser_output_section_header* header);
  1677. // Finish the output section with the information in the trailer.
  1678. void
  1679. finish(const Parser_output_section_trailer* trailer);
  1680. // Add a symbol to be defined.
  1681. void
  1682. add_symbol_assignment(const char* name, size_t length, Expression* value,
  1683. bool provide, bool hidden);
  1684. // Add an assignment to the special dot symbol.
  1685. void
  1686. add_dot_assignment(Expression* value);
  1687. // Add an assertion.
  1688. void
  1689. add_assertion(Expression* check, const char* message, size_t messagelen);
  1690. // Add a data item to the current output section.
  1691. void
  1692. add_data(int size, bool is_signed, Expression* val);
  1693. // Add a setting for the fill value.
  1694. void
  1695. add_fill(Expression* val);
  1696. // Add an input section specification.
  1697. void
  1698. add_input_section(const Input_section_spec* spec, bool keep);
  1699. // Return whether the output section is relro.
  1700. bool
  1701. is_relro() const
  1702. { return this->is_relro_; }
  1703. // Record that the output section is relro.
  1704. void
  1705. set_is_relro()
  1706. { this->is_relro_ = true; }
  1707. // Create any required output sections.
  1708. void
  1709. create_sections(Layout*);
  1710. // Add any symbols being defined to the symbol table.
  1711. void
  1712. add_symbols_to_table(Symbol_table* symtab);
  1713. // Finalize symbols and check assertions.
  1714. void
  1715. finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
  1716. // Return the output section name to use for an input file name and
  1717. // section name.
  1718. const char*
  1719. output_section_name(const char* file_name, const char* section_name,
  1720. Output_section***, Script_sections::Section_type*,
  1721. bool*, bool);
  1722. // Initialize OSP with an output section.
  1723. void
  1724. orphan_section_init(Orphan_section_placement* osp,
  1725. Script_sections::Elements_iterator p)
  1726. { osp->output_section_init(this->name_, this->output_section_, p); }
  1727. // Set the section address.
  1728. void
  1729. set_section_addresses(Symbol_table* symtab, Layout* layout,
  1730. uint64_t* dot_value, uint64_t*,
  1731. uint64_t* load_address);
  1732. // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
  1733. // this section is constrained, and the input sections do not match,
  1734. // return the constraint, and set *POSD.
  1735. Section_constraint
  1736. check_constraint(Output_section_definition** posd);
  1737. // See if this is the alternate output section for a constrained
  1738. // output section. If it is, transfer the Output_section and return
  1739. // true. Otherwise return false.
  1740. bool
  1741. alternate_constraint(Output_section_definition*, Section_constraint);
  1742. // Get the list of segments to use for an allocated section when
  1743. // using a PHDRS clause.
  1744. Output_section*
  1745. allocate_to_segment(String_list** phdrs_list, bool* orphan);
  1746. // Look for an output section by name and return the address, the
  1747. // load address, the alignment, and the size. This is used when an
  1748. // expression refers to an output section which was not actually
  1749. // created. This returns true if the section was found, false
  1750. // otherwise.
  1751. bool
  1752. get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
  1753. uint64_t*) const;
  1754. // Return the associated Output_section if there is one.
  1755. Output_section*
  1756. get_output_section() const
  1757. { return this->output_section_; }
  1758. // Print the contents to the FILE. This is for debugging.
  1759. void
  1760. print(FILE*) const;
  1761. // Return the output section type if specified or Script_sections::ST_NONE.
  1762. Script_sections::Section_type
  1763. section_type() const;
  1764. // Store the memory region to use.
  1765. void
  1766. set_memory_region(Memory_region*, bool set_vma);
  1767. void
  1768. set_section_vma(Expression* address)
  1769. { this->address_ = address; }
  1770. void
  1771. set_section_lma(Expression* address)
  1772. { this->load_address_ = address; }
  1773. const std::string&
  1774. get_section_name() const
  1775. { return this->name_; }
  1776. private:
  1777. static const char*
  1778. script_section_type_name(Script_section_type);
  1779. typedef std::vector<Output_section_element*> Output_section_elements;
  1780. // The output section name.
  1781. std::string name_;
  1782. // The address. This may be NULL.
  1783. Expression* address_;
  1784. // The load address. This may be NULL.
  1785. Expression* load_address_;
  1786. // The alignment. This may be NULL.
  1787. Expression* align_;
  1788. // The input section alignment. This may be NULL.
  1789. Expression* subalign_;
  1790. // The constraint, if any.
  1791. Section_constraint constraint_;
  1792. // The fill value. This may be NULL.
  1793. Expression* fill_;
  1794. // The list of segments this section should go into. This may be
  1795. // NULL.
  1796. String_list* phdrs_;
  1797. // The list of elements defining the section.
  1798. Output_section_elements elements_;
  1799. // The Output_section created for this definition. This will be
  1800. // NULL if none was created.
  1801. Output_section* output_section_;
  1802. // The address after it has been evaluated.
  1803. uint64_t evaluated_address_;
  1804. // The load address after it has been evaluated.
  1805. uint64_t evaluated_load_address_;
  1806. // The alignment after it has been evaluated.
  1807. uint64_t evaluated_addralign_;
  1808. // The output section is relro.
  1809. bool is_relro_;
  1810. // The output section type if specified.
  1811. enum Script_section_type script_section_type_;
  1812. };
  1813. // Constructor.
  1814. Output_section_definition::Output_section_definition(
  1815. const char* name,
  1816. size_t namelen,
  1817. const Parser_output_section_header* header)
  1818. : name_(name, namelen),
  1819. address_(header->address),
  1820. load_address_(header->load_address),
  1821. align_(header->align),
  1822. subalign_(header->subalign),
  1823. constraint_(header->constraint),
  1824. fill_(NULL),
  1825. phdrs_(NULL),
  1826. elements_(),
  1827. output_section_(NULL),
  1828. evaluated_address_(0),
  1829. evaluated_load_address_(0),
  1830. evaluated_addralign_(0),
  1831. is_relro_(false),
  1832. script_section_type_(header->section_type)
  1833. {
  1834. }
  1835. // Finish an output section.
  1836. void
  1837. Output_section_definition::finish(const Parser_output_section_trailer* trailer)
  1838. {
  1839. this->fill_ = trailer->fill;
  1840. this->phdrs_ = trailer->phdrs;
  1841. }
  1842. // Add a symbol to be defined.
  1843. void
  1844. Output_section_definition::add_symbol_assignment(const char* name,
  1845. size_t length,
  1846. Expression* value,
  1847. bool provide,
  1848. bool hidden)
  1849. {
  1850. Output_section_element* p = new Output_section_element_assignment(name,
  1851. length,
  1852. value,
  1853. provide,
  1854. hidden);
  1855. this->elements_.push_back(p);
  1856. }
  1857. // Add an assignment to the special dot symbol.
  1858. void
  1859. Output_section_definition::add_dot_assignment(Expression* value)
  1860. {
  1861. Output_section_element* p = new Output_section_element_dot_assignment(value);
  1862. this->elements_.push_back(p);
  1863. }
  1864. // Add an assertion.
  1865. void
  1866. Output_section_definition::add_assertion(Expression* check,
  1867. const char* message,
  1868. size_t messagelen)
  1869. {
  1870. Output_section_element* p = new Output_section_element_assertion(check,
  1871. message,
  1872. messagelen);
  1873. this->elements_.push_back(p);
  1874. }
  1875. // Add a data item to the current output section.
  1876. void
  1877. Output_section_definition::add_data(int size, bool is_signed, Expression* val)
  1878. {
  1879. Output_section_element* p = new Output_section_element_data(size, is_signed,
  1880. val);
  1881. this->elements_.push_back(p);
  1882. }
  1883. // Add a setting for the fill value.
  1884. void
  1885. Output_section_definition::add_fill(Expression* val)
  1886. {
  1887. Output_section_element* p = new Output_section_element_fill(val);
  1888. this->elements_.push_back(p);
  1889. }
  1890. // Add an input section specification.
  1891. void
  1892. Output_section_definition::add_input_section(const Input_section_spec* spec,
  1893. bool keep)
  1894. {
  1895. Output_section_element* p = new Output_section_element_input(spec, keep);
  1896. this->elements_.push_back(p);
  1897. }
  1898. // Create any required output sections. We need an output section if
  1899. // there is a data statement here.
  1900. void
  1901. Output_section_definition::create_sections(Layout* layout)
  1902. {
  1903. if (this->output_section_ != NULL)
  1904. return;
  1905. for (Output_section_elements::const_iterator p = this->elements_.begin();
  1906. p != this->elements_.end();
  1907. ++p)
  1908. {
  1909. if ((*p)->needs_output_section())
  1910. {
  1911. const char* name = this->name_.c_str();
  1912. this->output_section_ =
  1913. layout->make_output_section_for_script(name, this->section_type());
  1914. return;
  1915. }
  1916. }
  1917. }
  1918. // Add any symbols being defined to the symbol table.
  1919. void
  1920. Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
  1921. {
  1922. for (Output_section_elements::iterator p = this->elements_.begin();
  1923. p != this->elements_.end();
  1924. ++p)
  1925. (*p)->add_symbols_to_table(symtab);
  1926. }
  1927. // Finalize symbols and check assertions.
  1928. void
  1929. Output_section_definition::finalize_symbols(Symbol_table* symtab,
  1930. const Layout* layout,
  1931. uint64_t* dot_value)
  1932. {
  1933. if (this->output_section_ != NULL)
  1934. *dot_value = this->output_section_->address();
  1935. else
  1936. {
  1937. uint64_t address = *dot_value;
  1938. if (this->address_ != NULL)
  1939. {
  1940. address = this->address_->eval_with_dot(symtab, layout, true,
  1941. *dot_value, NULL,
  1942. NULL, NULL, false);
  1943. }
  1944. if (this->align_ != NULL)
  1945. {
  1946. uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
  1947. *dot_value, NULL,
  1948. NULL, NULL, false);
  1949. address = align_address(address, align);
  1950. }
  1951. *dot_value = address;
  1952. }
  1953. Output_section* dot_section = this->output_section_;
  1954. for (Output_section_elements::iterator p = this->elements_.begin();
  1955. p != this->elements_.end();
  1956. ++p)
  1957. (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
  1958. }
  1959. // Return the output section name to use for an input section name.
  1960. const char*
  1961. Output_section_definition::output_section_name(
  1962. const char* file_name,
  1963. const char* section_name,
  1964. Output_section*** slot,
  1965. Script_sections::Section_type* psection_type,
  1966. bool* keep,
  1967. bool match_input_spec)
  1968. {
  1969. // If the section is a linker-created output section, just look for a match
  1970. // on the output section name.
  1971. if (!match_input_spec && this->name_ != "/DISCARD/")
  1972. {
  1973. if (this->name_ != section_name)
  1974. return NULL;
  1975. *slot = &this->output_section_;
  1976. *psection_type = this->section_type();
  1977. return this->name_.c_str();
  1978. }
  1979. // Ask each element whether it matches NAME.
  1980. for (Output_section_elements::const_iterator p = this->elements_.begin();
  1981. p != this->elements_.end();
  1982. ++p)
  1983. {
  1984. if ((*p)->match_name(file_name, section_name, keep))
  1985. {
  1986. // We found a match for NAME, which means that it should go
  1987. // into this output section.
  1988. *slot = &this->output_section_;
  1989. *psection_type = this->section_type();
  1990. return this->name_.c_str();
  1991. }
  1992. }
  1993. // We don't know about this section name.
  1994. return NULL;
  1995. }
  1996. // Return true if memory from START to START + LENGTH is contained
  1997. // within a memory region.
  1998. bool
  1999. Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
  2000. uint64_t start, uint64_t length) const
  2001. {
  2002. if (this->memory_regions_ == NULL)
  2003. return false;
  2004. for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
  2005. mr != this->memory_regions_->end();
  2006. ++mr)
  2007. {
  2008. uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
  2009. uint64_t l = (*mr)->length()->eval(symtab, layout, false);
  2010. if (s <= start
  2011. && (s + l) >= (start + length))
  2012. return true;
  2013. }
  2014. return false;
  2015. }
  2016. // Find a memory region that should be used by a given output SECTION.
  2017. // If provided set PREVIOUS_SECTION_RETURN to point to the last section
  2018. // that used the return memory region.
  2019. Memory_region*
  2020. Script_sections::find_memory_region(
  2021. Output_section_definition* section,
  2022. bool find_vma_region,
  2023. bool explicit_only,
  2024. Output_section_definition** previous_section_return)
  2025. {
  2026. if (previous_section_return != NULL)
  2027. * previous_section_return = NULL;
  2028. // Walk the memory regions specified in this script, if any.
  2029. if (this->memory_regions_ == NULL)
  2030. return NULL;
  2031. // The /DISCARD/ section never gets assigned to any region.
  2032. if (section->get_section_name() == "/DISCARD/")
  2033. return NULL;
  2034. Memory_region* first_match = NULL;
  2035. // First check to see if a region has been assigned to this section.
  2036. for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
  2037. mr != this->memory_regions_->end();
  2038. ++mr)
  2039. {
  2040. if (find_vma_region)
  2041. {
  2042. for (Memory_region::Section_list::const_iterator s =
  2043. (*mr)->get_vma_section_list_start();
  2044. s != (*mr)->get_vma_section_list_end();
  2045. ++s)
  2046. if ((*s) == section)
  2047. {
  2048. (*mr)->set_last_section(section);
  2049. return *mr;
  2050. }
  2051. }
  2052. else
  2053. {
  2054. for (Memory_region::Section_list::const_iterator s =
  2055. (*mr)->get_lma_section_list_start();
  2056. s != (*mr)->get_lma_section_list_end();
  2057. ++s)
  2058. if ((*s) == section)
  2059. {
  2060. (*mr)->set_last_section(section);
  2061. return *mr;
  2062. }
  2063. }
  2064. if (!explicit_only)
  2065. {
  2066. // Make a note of the first memory region whose attributes
  2067. // are compatible with the section. If we do not find an
  2068. // explicit region assignment, then we will return this region.
  2069. Output_section* out_sec = section->get_output_section();
  2070. if (first_match == NULL
  2071. && out_sec != NULL
  2072. && (*mr)->attributes_compatible(out_sec->flags(),
  2073. out_sec->type()))
  2074. first_match = *mr;
  2075. }
  2076. }
  2077. // With LMA computations, if an explicit region has not been specified then
  2078. // we will want to set the difference between the VMA and the LMA of the
  2079. // section were searching for to be the same as the difference between the
  2080. // VMA and LMA of the last section to be added to first matched region.
  2081. // Hence, if it was asked for, we return a pointer to the last section
  2082. // known to be used by the first matched region.
  2083. if (first_match != NULL
  2084. && previous_section_return != NULL)
  2085. *previous_section_return = first_match->get_last_section();
  2086. return first_match;
  2087. }
  2088. // Set the section address. Note that the OUTPUT_SECTION_ field will
  2089. // be NULL if no input sections were mapped to this output section.
  2090. // We still have to adjust dot and process symbol assignments.
  2091. void
  2092. Output_section_definition::set_section_addresses(Symbol_table* symtab,
  2093. Layout* layout,
  2094. uint64_t* dot_value,
  2095. uint64_t* dot_alignment,
  2096. uint64_t* load_address)
  2097. {
  2098. Memory_region* vma_region = NULL;
  2099. Memory_region* lma_region = NULL;
  2100. Script_sections* script_sections =
  2101. layout->script_options()->script_sections();
  2102. uint64_t address;
  2103. uint64_t old_dot_value = *dot_value;
  2104. uint64_t old_load_address = *load_address;
  2105. // If input section sorting is requested via --section-ordering-file or
  2106. // linker plugins, then do it here. This is important because we want
  2107. // any sorting specified in the linker scripts, which will be done after
  2108. // this, to take precedence. The final order of input sections is then
  2109. // guaranteed to be according to the linker script specification.
  2110. if (this->output_section_ != NULL
  2111. && this->output_section_->input_section_order_specified())
  2112. this->output_section_->sort_attached_input_sections();
  2113. // Decide the start address for the section. The algorithm is:
  2114. // 1) If an address has been specified in a linker script, use that.
  2115. // 2) Otherwise if a memory region has been specified for the section,
  2116. // use the next free address in the region.
  2117. // 3) Otherwise if memory regions have been specified find the first
  2118. // region whose attributes are compatible with this section and
  2119. // install it into that region.
  2120. // 4) Otherwise use the current location counter.
  2121. if (this->output_section_ != NULL
  2122. // Check for --section-start.
  2123. && parameters->options().section_start(this->output_section_->name(),
  2124. &address))
  2125. ;
  2126. else if (this->address_ == NULL)
  2127. {
  2128. vma_region = script_sections->find_memory_region(this, true, false, NULL);
  2129. if (vma_region != NULL)
  2130. address = vma_region->get_current_address()->eval(symtab, layout,
  2131. false);
  2132. else
  2133. address = *dot_value;
  2134. }
  2135. else
  2136. {
  2137. vma_region = script_sections->find_memory_region(this, true, true, NULL);
  2138. address = this->address_->eval_with_dot(symtab, layout, true,
  2139. *dot_value, NULL, NULL,
  2140. dot_alignment, false);
  2141. if (vma_region != NULL)
  2142. vma_region->set_address(address, symtab, layout);
  2143. }
  2144. uint64_t align;
  2145. if (this->align_ == NULL)
  2146. {
  2147. if (this->output_section_ == NULL)
  2148. align = 0;
  2149. else
  2150. align = this->output_section_->addralign();
  2151. }
  2152. else
  2153. {
  2154. Output_section* align_section;
  2155. align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
  2156. NULL, &align_section, NULL, false);
  2157. if (align_section != NULL)
  2158. gold_warning(_("alignment of section %s is not absolute"),
  2159. this->name_.c_str());
  2160. if (this->output_section_ != NULL)
  2161. this->output_section_->set_addralign(align);
  2162. }
  2163. uint64_t subalign;
  2164. if (this->subalign_ == NULL)
  2165. subalign = 0;
  2166. else
  2167. {
  2168. Output_section* subalign_section;
  2169. subalign = this->subalign_->eval_with_dot(symtab, layout, true,
  2170. *dot_value, NULL,
  2171. &subalign_section, NULL,
  2172. false);
  2173. if (subalign_section != NULL)
  2174. gold_warning(_("subalign of section %s is not absolute"),
  2175. this->name_.c_str());
  2176. // Reserve a value of 0 to mean there is no SUBALIGN property.
  2177. if (subalign == 0)
  2178. subalign = 1;
  2179. // The external alignment of the output section must be at least
  2180. // as large as that of the input sections. If there is no
  2181. // explicit ALIGN property, we set the output section alignment
  2182. // to match the input section alignment.
  2183. if (align < subalign || this->align_ == NULL)
  2184. {
  2185. align = subalign;
  2186. this->output_section_->set_addralign(align);
  2187. }
  2188. }
  2189. address = align_address(address, align);
  2190. uint64_t start_address = address;
  2191. *dot_value = address;
  2192. // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
  2193. // forced to zero, regardless of what the linker script wants.
  2194. if (this->output_section_ != NULL
  2195. && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
  2196. || this->output_section_->is_noload()))
  2197. this->output_section_->set_address(address);
  2198. this->evaluated_address_ = address;
  2199. this->evaluated_addralign_ = align;
  2200. uint64_t laddr;
  2201. if (this->load_address_ == NULL)
  2202. {
  2203. Output_section_definition* previous_section;
  2204. // Determine if an LMA region has been set for this section.
  2205. lma_region = script_sections->find_memory_region(this, false, false,
  2206. &previous_section);
  2207. if (lma_region != NULL)
  2208. {
  2209. if (previous_section == NULL)
  2210. // The LMA address was explicitly set to the given region.
  2211. laddr = lma_region->get_current_address()->eval(symtab, layout,
  2212. false);
  2213. else
  2214. {
  2215. // We are not going to use the discovered lma_region, so
  2216. // make sure that we do not update it in the code below.
  2217. lma_region = NULL;
  2218. if (this->address_ != NULL || previous_section == this)
  2219. {
  2220. // Either an explicit VMA address has been set, or an
  2221. // explicit VMA region has been set, so set the LMA equal to
  2222. // the VMA.
  2223. laddr = address;
  2224. }
  2225. else
  2226. {
  2227. // The LMA address was not explicitly or implicitly set.
  2228. //
  2229. // We have been given the first memory region that is
  2230. // compatible with the current section and a pointer to the
  2231. // last section to use this region. Set the LMA of this
  2232. // section so that the difference between its' VMA and LMA
  2233. // is the same as the difference between the VMA and LMA of
  2234. // the last section in the given region.
  2235. laddr = address + (previous_section->evaluated_load_address_
  2236. - previous_section->evaluated_address_);
  2237. }
  2238. }
  2239. if (this->output_section_ != NULL)
  2240. this->output_section_->set_load_address(laddr);
  2241. }
  2242. else
  2243. {
  2244. // Do not set the load address of the output section, if one exists.
  2245. // This allows future sections to determine what the load address
  2246. // should be. If none is ever set, it will default to being the
  2247. // same as the vma address.
  2248. laddr = address;
  2249. }
  2250. }
  2251. else
  2252. {
  2253. laddr = this->load_address_->eval_with_dot(symtab, layout, true,
  2254. *dot_value,
  2255. this->output_section_,
  2256. NULL, NULL, false);
  2257. if (this->output_section_ != NULL)
  2258. this->output_section_->set_load_address(laddr);
  2259. }
  2260. this->evaluated_load_address_ = laddr;
  2261. std::string fill;
  2262. if (this->fill_ != NULL)
  2263. {
  2264. // FIXME: The GNU linker supports fill values of arbitrary
  2265. // length.
  2266. Output_section* fill_section;
  2267. uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
  2268. *dot_value,
  2269. NULL, &fill_section,
  2270. NULL, false);
  2271. if (fill_section != NULL)
  2272. gold_warning(_("fill of section %s is not absolute"),
  2273. this->name_.c_str());
  2274. unsigned char fill_buff[4];
  2275. elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
  2276. fill.assign(reinterpret_cast<char*>(fill_buff), 4);
  2277. }
  2278. Input_section_list input_sections;
  2279. if (this->output_section_ != NULL)
  2280. {
  2281. // Get the list of input sections attached to this output
  2282. // section. This will leave the output section with only
  2283. // Output_section_data entries.
  2284. address += this->output_section_->get_input_sections(address,
  2285. fill,
  2286. &input_sections);
  2287. *dot_value = address;
  2288. }
  2289. Output_section* dot_section = this->output_section_;
  2290. for (Output_section_elements::iterator p = this->elements_.begin();
  2291. p != this->elements_.end();
  2292. ++p)
  2293. (*p)->set_section_addresses(symtab, layout, this->output_section_,
  2294. subalign, dot_value, dot_alignment,
  2295. &dot_section, &fill, &input_sections);
  2296. gold_assert(input_sections.empty());
  2297. if (vma_region != NULL)
  2298. {
  2299. // Update the VMA region being used by the section now that we know how
  2300. // big it is. Use the current address in the region, rather than
  2301. // start_address because that might have been aligned upwards and we
  2302. // need to allow for the padding.
  2303. Expression* addr = vma_region->get_current_address();
  2304. uint64_t size = *dot_value - addr->eval(symtab, layout, false);
  2305. vma_region->increment_offset(this->get_section_name(), size,
  2306. symtab, layout);
  2307. }
  2308. // If the LMA region is different from the VMA region, then increment the
  2309. // offset there as well. Note that we use the same "dot_value -
  2310. // start_address" formula that is used in the load_address assignment below.
  2311. if (lma_region != NULL && lma_region != vma_region)
  2312. lma_region->increment_offset(this->get_section_name(),
  2313. *dot_value - start_address,
  2314. symtab, layout);
  2315. // Compute the load address for the following section.
  2316. if (this->output_section_ == NULL)
  2317. *load_address = *dot_value;
  2318. else if (this->load_address_ == NULL)
  2319. {
  2320. if (lma_region == NULL)
  2321. *load_address = *dot_value;
  2322. else
  2323. *load_address =
  2324. lma_region->get_current_address()->eval(symtab, layout, false);
  2325. }
  2326. else
  2327. *load_address = (this->output_section_->load_address()
  2328. + (*dot_value - start_address));
  2329. if (this->output_section_ != NULL)
  2330. {
  2331. if (this->is_relro_)
  2332. this->output_section_->set_is_relro();
  2333. else
  2334. this->output_section_->clear_is_relro();
  2335. // If this is a NOLOAD section, keep dot and load address unchanged.
  2336. if (this->output_section_->is_noload())
  2337. {
  2338. *dot_value = old_dot_value;
  2339. *load_address = old_load_address;
  2340. }
  2341. }
  2342. }
  2343. // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
  2344. // this section is constrained, and the input sections do not match,
  2345. // return the constraint, and set *POSD.
  2346. Section_constraint
  2347. Output_section_definition::check_constraint(Output_section_definition** posd)
  2348. {
  2349. switch (this->constraint_)
  2350. {
  2351. case CONSTRAINT_NONE:
  2352. return CONSTRAINT_NONE;
  2353. case CONSTRAINT_ONLY_IF_RO:
  2354. if (this->output_section_ != NULL
  2355. && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
  2356. {
  2357. *posd = this;
  2358. return CONSTRAINT_ONLY_IF_RO;
  2359. }
  2360. return CONSTRAINT_NONE;
  2361. case CONSTRAINT_ONLY_IF_RW:
  2362. if (this->output_section_ != NULL
  2363. && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
  2364. {
  2365. *posd = this;
  2366. return CONSTRAINT_ONLY_IF_RW;
  2367. }
  2368. return CONSTRAINT_NONE;
  2369. case CONSTRAINT_SPECIAL:
  2370. if (this->output_section_ != NULL)
  2371. gold_error(_("SPECIAL constraints are not implemented"));
  2372. return CONSTRAINT_NONE;
  2373. default:
  2374. gold_unreachable();
  2375. }
  2376. }
  2377. // See if this is the alternate output section for a constrained
  2378. // output section. If it is, transfer the Output_section and return
  2379. // true. Otherwise return false.
  2380. bool
  2381. Output_section_definition::alternate_constraint(
  2382. Output_section_definition* posd,
  2383. Section_constraint constraint)
  2384. {
  2385. if (this->name_ != posd->name_)
  2386. return false;
  2387. switch (constraint)
  2388. {
  2389. case CONSTRAINT_ONLY_IF_RO:
  2390. if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
  2391. return false;
  2392. break;
  2393. case CONSTRAINT_ONLY_IF_RW:
  2394. if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
  2395. return false;
  2396. break;
  2397. default:
  2398. gold_unreachable();
  2399. }
  2400. // We have found the alternate constraint. We just need to move
  2401. // over the Output_section. When constraints are used properly,
  2402. // THIS should not have an output_section pointer, as all the input
  2403. // sections should have matched the other definition.
  2404. if (this->output_section_ != NULL)
  2405. gold_error(_("mismatched definition for constrained sections"));
  2406. this->output_section_ = posd->output_section_;
  2407. posd->output_section_ = NULL;
  2408. if (this->is_relro_)
  2409. this->output_section_->set_is_relro();
  2410. else
  2411. this->output_section_->clear_is_relro();
  2412. return true;
  2413. }
  2414. // Get the list of segments to use for an allocated section when using
  2415. // a PHDRS clause.
  2416. Output_section*
  2417. Output_section_definition::allocate_to_segment(String_list** phdrs_list,
  2418. bool* orphan)
  2419. {
  2420. // Update phdrs_list even if we don't have an output section. It
  2421. // might be used by the following sections.
  2422. if (this->phdrs_ != NULL)
  2423. *phdrs_list = this->phdrs_;
  2424. if (this->output_section_ == NULL)
  2425. return NULL;
  2426. if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
  2427. return NULL;
  2428. *orphan = false;
  2429. return this->output_section_;
  2430. }
  2431. // Look for an output section by name and return the address, the load
  2432. // address, the alignment, and the size. This is used when an
  2433. // expression refers to an output section which was not actually
  2434. // created. This returns true if the section was found, false
  2435. // otherwise.
  2436. bool
  2437. Output_section_definition::get_output_section_info(const char* name,
  2438. uint64_t* address,
  2439. uint64_t* load_address,
  2440. uint64_t* addralign,
  2441. uint64_t* size) const
  2442. {
  2443. if (this->name_ != name)
  2444. return false;
  2445. if (this->output_section_ != NULL)
  2446. {
  2447. *address = this->output_section_->address();
  2448. if (this->output_section_->has_load_address())
  2449. *load_address = this->output_section_->load_address();
  2450. else
  2451. *load_address = *address;
  2452. *addralign = this->output_section_->addralign();
  2453. *size = this->output_section_->current_data_size();
  2454. }
  2455. else
  2456. {
  2457. *address = this->evaluated_address_;
  2458. *load_address = this->evaluated_load_address_;
  2459. *addralign = this->evaluated_addralign_;
  2460. *size = 0;
  2461. }
  2462. return true;
  2463. }
  2464. // Print for debugging.
  2465. void
  2466. Output_section_definition::print(FILE* f) const
  2467. {
  2468. fprintf(f, " %s ", this->name_.c_str());
  2469. if (this->address_ != NULL)
  2470. {
  2471. this->address_->print(f);
  2472. fprintf(f, " ");
  2473. }
  2474. if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
  2475. fprintf(f, "(%s) ",
  2476. this->script_section_type_name(this->script_section_type_));
  2477. fprintf(f, ": ");
  2478. if (this->load_address_ != NULL)
  2479. {
  2480. fprintf(f, "AT(");
  2481. this->load_address_->print(f);
  2482. fprintf(f, ") ");
  2483. }
  2484. if (this->align_ != NULL)
  2485. {
  2486. fprintf(f, "ALIGN(");
  2487. this->align_->print(f);
  2488. fprintf(f, ") ");
  2489. }
  2490. if (this->subalign_ != NULL)
  2491. {
  2492. fprintf(f, "SUBALIGN(");
  2493. this->subalign_->print(f);
  2494. fprintf(f, ") ");
  2495. }
  2496. fprintf(f, "{\n");
  2497. for (Output_section_elements::const_iterator p = this->elements_.begin();
  2498. p != this->elements_.end();
  2499. ++p)
  2500. (*p)->print(f);
  2501. fprintf(f, " }");
  2502. if (this->fill_ != NULL)
  2503. {
  2504. fprintf(f, " = ");
  2505. this->fill_->print(f);
  2506. }
  2507. if (this->phdrs_ != NULL)
  2508. {
  2509. for (String_list::const_iterator p = this->phdrs_->begin();
  2510. p != this->phdrs_->end();
  2511. ++p)
  2512. fprintf(f, " :%s", p->c_str());
  2513. }
  2514. fprintf(f, "\n");
  2515. }
  2516. Script_sections::Section_type
  2517. Output_section_definition::section_type() const
  2518. {
  2519. switch (this->script_section_type_)
  2520. {
  2521. case SCRIPT_SECTION_TYPE_NONE:
  2522. return Script_sections::ST_NONE;
  2523. case SCRIPT_SECTION_TYPE_NOLOAD:
  2524. return Script_sections::ST_NOLOAD;
  2525. case SCRIPT_SECTION_TYPE_COPY:
  2526. case SCRIPT_SECTION_TYPE_DSECT:
  2527. case SCRIPT_SECTION_TYPE_INFO:
  2528. case SCRIPT_SECTION_TYPE_OVERLAY:
  2529. // There are not really support so we treat them as ST_NONE. The
  2530. // parse should have issued errors for them already.
  2531. return Script_sections::ST_NONE;
  2532. default:
  2533. gold_unreachable();
  2534. }
  2535. }
  2536. // Return the name of a script section type.
  2537. const char*
  2538. Output_section_definition::script_section_type_name(
  2539. Script_section_type script_section_type)
  2540. {
  2541. switch (script_section_type)
  2542. {
  2543. case SCRIPT_SECTION_TYPE_NONE:
  2544. return "NONE";
  2545. case SCRIPT_SECTION_TYPE_NOLOAD:
  2546. return "NOLOAD";
  2547. case SCRIPT_SECTION_TYPE_DSECT:
  2548. return "DSECT";
  2549. case SCRIPT_SECTION_TYPE_COPY:
  2550. return "COPY";
  2551. case SCRIPT_SECTION_TYPE_INFO:
  2552. return "INFO";
  2553. case SCRIPT_SECTION_TYPE_OVERLAY:
  2554. return "OVERLAY";
  2555. default:
  2556. gold_unreachable();
  2557. }
  2558. }
  2559. void
  2560. Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
  2561. {
  2562. gold_assert(mr != NULL);
  2563. // Add the current section to the specified region's list.
  2564. mr->add_section(this, set_vma);
  2565. }
  2566. // An output section created to hold orphaned input sections. These
  2567. // do not actually appear in linker scripts. However, for convenience
  2568. // when setting the output section addresses, we put a marker to these
  2569. // sections in the appropriate place in the list of SECTIONS elements.
  2570. class Orphan_output_section : public Sections_element
  2571. {
  2572. public:
  2573. Orphan_output_section(Output_section* os)
  2574. : os_(os)
  2575. { }
  2576. // Return whether the orphan output section is relro. We can just
  2577. // check the output section because we always set the flag, if
  2578. // needed, just after we create the Orphan_output_section.
  2579. bool
  2580. is_relro() const
  2581. { return this->os_->is_relro(); }
  2582. // Initialize OSP with an output section. This should have been
  2583. // done already.
  2584. void
  2585. orphan_section_init(Orphan_section_placement*,
  2586. Script_sections::Elements_iterator)
  2587. { gold_unreachable(); }
  2588. // Set section addresses.
  2589. void
  2590. set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
  2591. uint64_t*);
  2592. // Get the list of segments to use for an allocated section when
  2593. // using a PHDRS clause.
  2594. Output_section*
  2595. allocate_to_segment(String_list**, bool*);
  2596. // Return the associated Output_section.
  2597. Output_section*
  2598. get_output_section() const
  2599. { return this->os_; }
  2600. // Print for debugging.
  2601. void
  2602. print(FILE* f) const
  2603. {
  2604. fprintf(f, " marker for orphaned output section %s\n",
  2605. this->os_->name());
  2606. }
  2607. private:
  2608. Output_section* os_;
  2609. };
  2610. // Set section addresses.
  2611. void
  2612. Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
  2613. uint64_t* dot_value,
  2614. uint64_t*,
  2615. uint64_t* load_address)
  2616. {
  2617. typedef std::list<Output_section::Input_section> Input_section_list;
  2618. bool have_load_address = *load_address != *dot_value;
  2619. uint64_t address = *dot_value;
  2620. address = align_address(address, this->os_->addralign());
  2621. // If input section sorting is requested via --section-ordering-file or
  2622. // linker plugins, then do it here. This is important because we want
  2623. // any sorting specified in the linker scripts, which will be done after
  2624. // this, to take precedence. The final order of input sections is then
  2625. // guaranteed to be according to the linker script specification.
  2626. if (this->os_ != NULL
  2627. && this->os_->input_section_order_specified())
  2628. this->os_->sort_attached_input_sections();
  2629. // For a relocatable link, all orphan sections are put at
  2630. // address 0. In general we expect all sections to be at
  2631. // address 0 for a relocatable link, but we permit the linker
  2632. // script to override that for specific output sections.
  2633. if (parameters->options().relocatable())
  2634. {
  2635. address = 0;
  2636. *load_address = 0;
  2637. have_load_address = false;
  2638. }
  2639. if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
  2640. {
  2641. this->os_->set_address(address);
  2642. if (have_load_address)
  2643. this->os_->set_load_address(align_address(*load_address,
  2644. this->os_->addralign()));
  2645. }
  2646. Input_section_list input_sections;
  2647. address += this->os_->get_input_sections(address, "", &input_sections);
  2648. for (Input_section_list::iterator p = input_sections.begin();
  2649. p != input_sections.end();
  2650. ++p)
  2651. {
  2652. uint64_t addralign = p->addralign();
  2653. if (!p->is_input_section())
  2654. p->output_section_data()->finalize_data_size();
  2655. uint64_t size = p->data_size();
  2656. address = align_address(address, addralign);
  2657. this->os_->add_script_input_section(*p);
  2658. address += size;
  2659. }
  2660. if (parameters->options().relocatable())
  2661. {
  2662. // For a relocatable link, reset DOT_VALUE to 0.
  2663. *dot_value = 0;
  2664. *load_address = 0;
  2665. }
  2666. else if (this->os_ == NULL
  2667. || (this->os_->flags() & elfcpp::SHF_TLS) == 0
  2668. || this->os_->type() != elfcpp::SHT_NOBITS)
  2669. {
  2670. // An SHF_TLS/SHT_NOBITS section does not take up any address space.
  2671. if (!have_load_address)
  2672. *load_address = address;
  2673. else
  2674. *load_address += address - *dot_value;
  2675. *dot_value = address;
  2676. }
  2677. }
  2678. // Get the list of segments to use for an allocated section when using
  2679. // a PHDRS clause. If this is an allocated section, return the
  2680. // Output_section. We don't change the list of segments.
  2681. Output_section*
  2682. Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
  2683. {
  2684. if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
  2685. return NULL;
  2686. *orphan = true;
  2687. return this->os_;
  2688. }
  2689. // Class Phdrs_element. A program header from a PHDRS clause.
  2690. class Phdrs_element
  2691. {
  2692. public:
  2693. Phdrs_element(const char* name, size_t namelen, unsigned int type,
  2694. bool includes_filehdr, bool includes_phdrs,
  2695. bool is_flags_valid, unsigned int flags,
  2696. Expression* load_address)
  2697. : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
  2698. includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
  2699. flags_(flags), load_address_(load_address), load_address_value_(0),
  2700. segment_(NULL)
  2701. { }
  2702. // Return the name of this segment.
  2703. const std::string&
  2704. name() const
  2705. { return this->name_; }
  2706. // Return the type of the segment.
  2707. unsigned int
  2708. type() const
  2709. { return this->type_; }
  2710. // Whether to include the file header.
  2711. bool
  2712. includes_filehdr() const
  2713. { return this->includes_filehdr_; }
  2714. // Whether to include the program headers.
  2715. bool
  2716. includes_phdrs() const
  2717. { return this->includes_phdrs_; }
  2718. // Return whether there is a load address.
  2719. bool
  2720. has_load_address() const
  2721. { return this->load_address_ != NULL; }
  2722. // Evaluate the load address expression if there is one.
  2723. void
  2724. eval_load_address(Symbol_table* symtab, Layout* layout)
  2725. {
  2726. if (this->load_address_ != NULL)
  2727. this->load_address_value_ = this->load_address_->eval(symtab, layout,
  2728. true);
  2729. }
  2730. // Return the load address.
  2731. uint64_t
  2732. load_address() const
  2733. {
  2734. gold_assert(this->load_address_ != NULL);
  2735. return this->load_address_value_;
  2736. }
  2737. // Create the segment.
  2738. Output_segment*
  2739. create_segment(Layout* layout)
  2740. {
  2741. this->segment_ = layout->make_output_segment(this->type_, this->flags_);
  2742. return this->segment_;
  2743. }
  2744. // Return the segment.
  2745. Output_segment*
  2746. segment()
  2747. { return this->segment_; }
  2748. // Release the segment.
  2749. void
  2750. release_segment()
  2751. { this->segment_ = NULL; }
  2752. // Set the segment flags if appropriate.
  2753. void
  2754. set_flags_if_valid()
  2755. {
  2756. if (this->is_flags_valid_)
  2757. this->segment_->set_flags(this->flags_);
  2758. }
  2759. // Print for debugging.
  2760. void
  2761. print(FILE*) const;
  2762. private:
  2763. // The name used in the script.
  2764. std::string name_;
  2765. // The type of the segment (PT_LOAD, etc.).
  2766. unsigned int type_;
  2767. // Whether this segment includes the file header.
  2768. bool includes_filehdr_;
  2769. // Whether this segment includes the section headers.
  2770. bool includes_phdrs_;
  2771. // Whether the flags were explicitly specified.
  2772. bool is_flags_valid_;
  2773. // The flags for this segment (PF_R, etc.) if specified.
  2774. unsigned int flags_;
  2775. // The expression for the load address for this segment. This may
  2776. // be NULL.
  2777. Expression* load_address_;
  2778. // The actual load address from evaluating the expression.
  2779. uint64_t load_address_value_;
  2780. // The segment itself.
  2781. Output_segment* segment_;
  2782. };
  2783. // Print for debugging.
  2784. void
  2785. Phdrs_element::print(FILE* f) const
  2786. {
  2787. fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
  2788. if (this->includes_filehdr_)
  2789. fprintf(f, " FILEHDR");
  2790. if (this->includes_phdrs_)
  2791. fprintf(f, " PHDRS");
  2792. if (this->is_flags_valid_)
  2793. fprintf(f, " FLAGS(%u)", this->flags_);
  2794. if (this->load_address_ != NULL)
  2795. {
  2796. fprintf(f, " AT(");
  2797. this->load_address_->print(f);
  2798. fprintf(f, ")");
  2799. }
  2800. fprintf(f, ";\n");
  2801. }
  2802. // Add a memory region.
  2803. void
  2804. Script_sections::add_memory_region(const char* name, size_t namelen,
  2805. unsigned int attributes,
  2806. Expression* start, Expression* length)
  2807. {
  2808. if (this->memory_regions_ == NULL)
  2809. this->memory_regions_ = new Memory_regions();
  2810. else if (this->find_memory_region(name, namelen))
  2811. {
  2812. gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
  2813. name);
  2814. // FIXME: Add a GOLD extension to allow multiple regions with the same
  2815. // name. This would amount to a single region covering disjoint blocks
  2816. // of memory, which is useful for embedded devices.
  2817. }
  2818. // FIXME: Check the length and start values. Currently we allow
  2819. // non-constant expressions for these values, whereas LD does not.
  2820. // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
  2821. // describe a region that packs from the end address going down, rather
  2822. // than the start address going up. This would be useful for embedded
  2823. // devices.
  2824. this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
  2825. start, length));
  2826. }
  2827. // Find a memory region.
  2828. Memory_region*
  2829. Script_sections::find_memory_region(const char* name, size_t namelen)
  2830. {
  2831. if (this->memory_regions_ == NULL)
  2832. return NULL;
  2833. for (Memory_regions::const_iterator m = this->memory_regions_->begin();
  2834. m != this->memory_regions_->end();
  2835. ++m)
  2836. if ((*m)->name_match(name, namelen))
  2837. return *m;
  2838. return NULL;
  2839. }
  2840. // Find a memory region's origin.
  2841. Expression*
  2842. Script_sections::find_memory_region_origin(const char* name, size_t namelen)
  2843. {
  2844. Memory_region* mr = find_memory_region(name, namelen);
  2845. if (mr == NULL)
  2846. return NULL;
  2847. return mr->start_address();
  2848. }
  2849. // Find a memory region's length.
  2850. Expression*
  2851. Script_sections::find_memory_region_length(const char* name, size_t namelen)
  2852. {
  2853. Memory_region* mr = find_memory_region(name, namelen);
  2854. if (mr == NULL)
  2855. return NULL;
  2856. return mr->length();
  2857. }
  2858. // Set the memory region to use for the current section.
  2859. void
  2860. Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
  2861. {
  2862. gold_assert(!this->sections_elements_->empty());
  2863. this->sections_elements_->back()->set_memory_region(mr, set_vma);
  2864. }
  2865. // Class Script_sections.
  2866. Script_sections::Script_sections()
  2867. : saw_sections_clause_(false),
  2868. in_sections_clause_(false),
  2869. sections_elements_(NULL),
  2870. output_section_(NULL),
  2871. memory_regions_(NULL),
  2872. phdrs_elements_(NULL),
  2873. orphan_section_placement_(NULL),
  2874. data_segment_align_start_(),
  2875. saw_data_segment_align_(false),
  2876. saw_relro_end_(false),
  2877. saw_segment_start_expression_(false),
  2878. segments_created_(false)
  2879. {
  2880. }
  2881. // Start a SECTIONS clause.
  2882. void
  2883. Script_sections::start_sections()
  2884. {
  2885. gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
  2886. this->saw_sections_clause_ = true;
  2887. this->in_sections_clause_ = true;
  2888. if (this->sections_elements_ == NULL)
  2889. this->sections_elements_ = new Sections_elements;
  2890. }
  2891. // Finish a SECTIONS clause.
  2892. void
  2893. Script_sections::finish_sections()
  2894. {
  2895. gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
  2896. this->in_sections_clause_ = false;
  2897. }
  2898. // Add a symbol to be defined.
  2899. void
  2900. Script_sections::add_symbol_assignment(const char* name, size_t length,
  2901. Expression* val, bool provide,
  2902. bool hidden)
  2903. {
  2904. if (this->output_section_ != NULL)
  2905. this->output_section_->add_symbol_assignment(name, length, val,
  2906. provide, hidden);
  2907. else
  2908. {
  2909. Sections_element* p = new Sections_element_assignment(name, length,
  2910. val, provide,
  2911. hidden);
  2912. this->sections_elements_->push_back(p);
  2913. }
  2914. }
  2915. // Add an assignment to the special dot symbol.
  2916. void
  2917. Script_sections::add_dot_assignment(Expression* val)
  2918. {
  2919. if (this->output_section_ != NULL)
  2920. this->output_section_->add_dot_assignment(val);
  2921. else
  2922. {
  2923. // The GNU linker permits assignments to . to appears outside of
  2924. // a SECTIONS clause, and treats it as appearing inside, so
  2925. // sections_elements_ may be NULL here.
  2926. if (this->sections_elements_ == NULL)
  2927. {
  2928. this->sections_elements_ = new Sections_elements;
  2929. this->saw_sections_clause_ = true;
  2930. }
  2931. Sections_element* p = new Sections_element_dot_assignment(val);
  2932. this->sections_elements_->push_back(p);
  2933. }
  2934. }
  2935. // Add an assertion.
  2936. void
  2937. Script_sections::add_assertion(Expression* check, const char* message,
  2938. size_t messagelen)
  2939. {
  2940. if (this->output_section_ != NULL)
  2941. this->output_section_->add_assertion(check, message, messagelen);
  2942. else
  2943. {
  2944. Sections_element* p = new Sections_element_assertion(check, message,
  2945. messagelen);
  2946. this->sections_elements_->push_back(p);
  2947. }
  2948. }
  2949. // Start processing entries for an output section.
  2950. void
  2951. Script_sections::start_output_section(
  2952. const char* name,
  2953. size_t namelen,
  2954. const Parser_output_section_header* header)
  2955. {
  2956. Output_section_definition* posd = new Output_section_definition(name,
  2957. namelen,
  2958. header);
  2959. this->sections_elements_->push_back(posd);
  2960. gold_assert(this->output_section_ == NULL);
  2961. this->output_section_ = posd;
  2962. }
  2963. // Stop processing entries for an output section.
  2964. void
  2965. Script_sections::finish_output_section(
  2966. const Parser_output_section_trailer* trailer)
  2967. {
  2968. gold_assert(this->output_section_ != NULL);
  2969. this->output_section_->finish(trailer);
  2970. this->output_section_ = NULL;
  2971. }
  2972. // Add a data item to the current output section.
  2973. void
  2974. Script_sections::add_data(int size, bool is_signed, Expression* val)
  2975. {
  2976. gold_assert(this->output_section_ != NULL);
  2977. this->output_section_->add_data(size, is_signed, val);
  2978. }
  2979. // Add a fill value setting to the current output section.
  2980. void
  2981. Script_sections::add_fill(Expression* val)
  2982. {
  2983. gold_assert(this->output_section_ != NULL);
  2984. this->output_section_->add_fill(val);
  2985. }
  2986. // Add an input section specification to the current output section.
  2987. void
  2988. Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
  2989. {
  2990. gold_assert(this->output_section_ != NULL);
  2991. this->output_section_->add_input_section(spec, keep);
  2992. }
  2993. // This is called when we see DATA_SEGMENT_ALIGN. It means that any
  2994. // subsequent output sections may be relro.
  2995. void
  2996. Script_sections::data_segment_align()
  2997. {
  2998. if (this->saw_data_segment_align_)
  2999. gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
  3000. gold_assert(!this->sections_elements_->empty());
  3001. Sections_elements::iterator p = this->sections_elements_->end();
  3002. --p;
  3003. this->data_segment_align_start_ = p;
  3004. this->saw_data_segment_align_ = true;
  3005. }
  3006. // This is called when we see DATA_SEGMENT_RELRO_END. It means that
  3007. // any output sections seen since DATA_SEGMENT_ALIGN are relro.
  3008. void
  3009. Script_sections::data_segment_relro_end()
  3010. {
  3011. if (this->saw_relro_end_)
  3012. gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
  3013. "in a linker script"));
  3014. this->saw_relro_end_ = true;
  3015. if (!this->saw_data_segment_align_)
  3016. gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
  3017. else
  3018. {
  3019. Sections_elements::iterator p = this->data_segment_align_start_;
  3020. for (++p; p != this->sections_elements_->end(); ++p)
  3021. (*p)->set_is_relro();
  3022. }
  3023. }
  3024. // Create any required sections.
  3025. void
  3026. Script_sections::create_sections(Layout* layout)
  3027. {
  3028. if (!this->saw_sections_clause_)
  3029. return;
  3030. for (Sections_elements::iterator p = this->sections_elements_->begin();
  3031. p != this->sections_elements_->end();
  3032. ++p)
  3033. (*p)->create_sections(layout);
  3034. }
  3035. // Add any symbols we are defining to the symbol table.
  3036. void
  3037. Script_sections::add_symbols_to_table(Symbol_table* symtab)
  3038. {
  3039. if (!this->saw_sections_clause_)
  3040. return;
  3041. for (Sections_elements::iterator p = this->sections_elements_->begin();
  3042. p != this->sections_elements_->end();
  3043. ++p)
  3044. (*p)->add_symbols_to_table(symtab);
  3045. }
  3046. // Finalize symbols and check assertions.
  3047. void
  3048. Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
  3049. {
  3050. if (!this->saw_sections_clause_)
  3051. return;
  3052. uint64_t dot_value = 0;
  3053. for (Sections_elements::iterator p = this->sections_elements_->begin();
  3054. p != this->sections_elements_->end();
  3055. ++p)
  3056. (*p)->finalize_symbols(symtab, layout, &dot_value);
  3057. }
  3058. // Return the name of the output section to use for an input file name
  3059. // and section name.
  3060. const char*
  3061. Script_sections::output_section_name(
  3062. const char* file_name,
  3063. const char* section_name,
  3064. Output_section*** output_section_slot,
  3065. Script_sections::Section_type* psection_type,
  3066. bool* keep,
  3067. bool is_input_section)
  3068. {
  3069. for (Sections_elements::const_iterator p = this->sections_elements_->begin();
  3070. p != this->sections_elements_->end();
  3071. ++p)
  3072. {
  3073. const char* ret = (*p)->output_section_name(file_name, section_name,
  3074. output_section_slot,
  3075. psection_type, keep,
  3076. is_input_section);
  3077. if (ret != NULL)
  3078. {
  3079. // The special name /DISCARD/ means that the input section
  3080. // should be discarded.
  3081. if (strcmp(ret, "/DISCARD/") == 0)
  3082. {
  3083. *output_section_slot = NULL;
  3084. *psection_type = Script_sections::ST_NONE;
  3085. return NULL;
  3086. }
  3087. return ret;
  3088. }
  3089. }
  3090. // We have an orphan section.
  3091. *output_section_slot = NULL;
  3092. *psection_type = Script_sections::ST_NONE;
  3093. *keep = false;
  3094. General_options::Orphan_handling orphan_handling =
  3095. parameters->options().orphan_handling_enum();
  3096. if (orphan_handling == General_options::ORPHAN_DISCARD)
  3097. return NULL;
  3098. if (orphan_handling == General_options::ORPHAN_ERROR)
  3099. {
  3100. if (file_name == NULL)
  3101. gold_error(_("unplaced orphan section '%s'"), section_name);
  3102. else
  3103. gold_error(_("unplaced orphan section '%s' from '%s'"),
  3104. section_name, file_name);
  3105. return NULL;
  3106. }
  3107. if (orphan_handling == General_options::ORPHAN_WARN)
  3108. {
  3109. if (file_name == NULL)
  3110. gold_warning(_("orphan section '%s' is being placed in section '%s'"),
  3111. section_name, section_name);
  3112. else
  3113. gold_warning(_("orphan section '%s' from '%s' is being placed "
  3114. "in section '%s'"),
  3115. section_name, file_name, section_name);
  3116. }
  3117. // If we couldn't find a mapping for the name, the output section
  3118. // gets the name of the input section.
  3119. return section_name;
  3120. }
  3121. // Place a marker for an orphan output section into the SECTIONS
  3122. // clause.
  3123. void
  3124. Script_sections::place_orphan(Output_section* os)
  3125. {
  3126. Orphan_section_placement* osp = this->orphan_section_placement_;
  3127. if (osp == NULL)
  3128. {
  3129. // Initialize the Orphan_section_placement structure.
  3130. osp = new Orphan_section_placement();
  3131. for (Sections_elements::iterator p = this->sections_elements_->begin();
  3132. p != this->sections_elements_->end();
  3133. ++p)
  3134. (*p)->orphan_section_init(osp, p);
  3135. gold_assert(!this->sections_elements_->empty());
  3136. Sections_elements::iterator last = this->sections_elements_->end();
  3137. --last;
  3138. osp->last_init(last);
  3139. this->orphan_section_placement_ = osp;
  3140. }
  3141. Orphan_output_section* orphan = new Orphan_output_section(os);
  3142. // Look for where to put ORPHAN.
  3143. Sections_elements::iterator* where;
  3144. if (osp->find_place(os, &where))
  3145. {
  3146. if ((**where)->is_relro())
  3147. os->set_is_relro();
  3148. else
  3149. os->clear_is_relro();
  3150. // We want to insert ORPHAN after *WHERE, and then update *WHERE
  3151. // so that the next one goes after this one.
  3152. Sections_elements::iterator p = *where;
  3153. gold_assert(p != this->sections_elements_->end());
  3154. ++p;
  3155. *where = this->sections_elements_->insert(p, orphan);
  3156. }
  3157. else
  3158. {
  3159. os->clear_is_relro();
  3160. // We don't have a place to put this orphan section. Put it,
  3161. // and all other sections like it, at the end, but before the
  3162. // sections which always come at the end.
  3163. Sections_elements::iterator last = osp->last_place();
  3164. *where = this->sections_elements_->insert(last, orphan);
  3165. }
  3166. if ((os->flags() & elfcpp::SHF_ALLOC) != 0)
  3167. osp->update_last_alloc(*where);
  3168. }
  3169. // Set the addresses of all the output sections. Walk through all the
  3170. // elements, tracking the dot symbol. Apply assignments which set
  3171. // absolute symbol values, in case they are used when setting dot.
  3172. // Fill in data statement values. As we find output sections, set the
  3173. // address, set the address of all associated input sections, and
  3174. // update dot. Return the segment which should hold the file header
  3175. // and segment headers, if any.
  3176. Output_segment*
  3177. Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
  3178. {
  3179. gold_assert(this->saw_sections_clause_);
  3180. // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
  3181. // for our representation.
  3182. for (Sections_elements::iterator p = this->sections_elements_->begin();
  3183. p != this->sections_elements_->end();
  3184. ++p)
  3185. {
  3186. Output_section_definition* posd;
  3187. Section_constraint failed_constraint = (*p)->check_constraint(&posd);
  3188. if (failed_constraint != CONSTRAINT_NONE)
  3189. {
  3190. Sections_elements::iterator q;
  3191. for (q = this->sections_elements_->begin();
  3192. q != this->sections_elements_->end();
  3193. ++q)
  3194. {
  3195. if (q != p)
  3196. {
  3197. if ((*q)->alternate_constraint(posd, failed_constraint))
  3198. break;
  3199. }
  3200. }
  3201. if (q == this->sections_elements_->end())
  3202. gold_error(_("no matching section constraint"));
  3203. }
  3204. }
  3205. // Force the alignment of the first TLS section to be the maximum
  3206. // alignment of all TLS sections.
  3207. Output_section* first_tls = NULL;
  3208. uint64_t tls_align = 0;
  3209. for (Sections_elements::const_iterator p = this->sections_elements_->begin();
  3210. p != this->sections_elements_->end();
  3211. ++p)
  3212. {
  3213. Output_section* os = (*p)->get_output_section();
  3214. if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
  3215. {
  3216. if (first_tls == NULL)
  3217. first_tls = os;
  3218. if (os->addralign() > tls_align)
  3219. tls_align = os->addralign();
  3220. }
  3221. }
  3222. if (first_tls != NULL)
  3223. first_tls->set_addralign(tls_align);
  3224. // For a relocatable link, we implicitly set dot to zero.
  3225. uint64_t dot_value = 0;
  3226. uint64_t dot_alignment = 0;
  3227. uint64_t load_address = 0;
  3228. // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
  3229. // to set section addresses. If the script has any SEGMENT_START
  3230. // expression, we do not set the section addresses.
  3231. bool use_tsection_options =
  3232. (!this->saw_segment_start_expression_
  3233. && (parameters->options().user_set_Ttext()
  3234. || parameters->options().user_set_Tdata()
  3235. || parameters->options().user_set_Tbss()));
  3236. for (Sections_elements::iterator p = this->sections_elements_->begin();
  3237. p != this->sections_elements_->end();
  3238. ++p)
  3239. {
  3240. Output_section* os = (*p)->get_output_section();
  3241. // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
  3242. // the special sections by names and doing dot assignments.
  3243. if (use_tsection_options
  3244. && os != NULL
  3245. && (os->flags() & elfcpp::SHF_ALLOC) != 0)
  3246. {
  3247. uint64_t new_dot_value = dot_value;
  3248. if (parameters->options().user_set_Ttext()
  3249. && strcmp(os->name(), ".text") == 0)
  3250. new_dot_value = parameters->options().Ttext();
  3251. else if (parameters->options().user_set_Tdata()
  3252. && strcmp(os->name(), ".data") == 0)
  3253. new_dot_value = parameters->options().Tdata();
  3254. else if (parameters->options().user_set_Tbss()
  3255. && strcmp(os->name(), ".bss") == 0)
  3256. new_dot_value = parameters->options().Tbss();
  3257. // Update dot and load address if necessary.
  3258. if (new_dot_value < dot_value)
  3259. gold_error(_("dot may not move backward"));
  3260. else if (new_dot_value != dot_value)
  3261. {
  3262. dot_value = new_dot_value;
  3263. load_address = new_dot_value;
  3264. }
  3265. }
  3266. (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
  3267. &load_address);
  3268. }
  3269. if (this->phdrs_elements_ != NULL)
  3270. {
  3271. for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
  3272. p != this->phdrs_elements_->end();
  3273. ++p)
  3274. (*p)->eval_load_address(symtab, layout);
  3275. }
  3276. return this->create_segments(layout, dot_alignment);
  3277. }
  3278. // Sort the sections in order to put them into segments.
  3279. class Sort_output_sections
  3280. {
  3281. public:
  3282. Sort_output_sections(const Script_sections::Sections_elements* elements)
  3283. : elements_(elements)
  3284. { }
  3285. bool
  3286. operator()(const Output_section* os1, const Output_section* os2) const;
  3287. private:
  3288. int
  3289. script_compare(const Output_section* os1, const Output_section* os2) const;
  3290. private:
  3291. const Script_sections::Sections_elements* elements_;
  3292. };
  3293. bool
  3294. Sort_output_sections::operator()(const Output_section* os1,
  3295. const Output_section* os2) const
  3296. {
  3297. // Sort first by the load address.
  3298. uint64_t lma1 = (os1->has_load_address()
  3299. ? os1->load_address()
  3300. : os1->address());
  3301. uint64_t lma2 = (os2->has_load_address()
  3302. ? os2->load_address()
  3303. : os2->address());
  3304. if (lma1 != lma2)
  3305. return lma1 < lma2;
  3306. // Then sort by the virtual address.
  3307. if (os1->address() != os2->address())
  3308. return os1->address() < os2->address();
  3309. // If the linker script says which of these sections is first, go
  3310. // with what it says.
  3311. int i = this->script_compare(os1, os2);
  3312. if (i != 0)
  3313. return i < 0;
  3314. // Sort PROGBITS before NOBITS.
  3315. bool nobits1 = os1->type() == elfcpp::SHT_NOBITS;
  3316. bool nobits2 = os2->type() == elfcpp::SHT_NOBITS;
  3317. if (nobits1 != nobits2)
  3318. return nobits2;
  3319. // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
  3320. // beginning.
  3321. bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
  3322. bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
  3323. if (tls1 != tls2)
  3324. return nobits1 ? tls1 : tls2;
  3325. // Sort non-NOLOAD before NOLOAD.
  3326. if (os1->is_noload() && !os2->is_noload())
  3327. return true;
  3328. if (!os1->is_noload() && os2->is_noload())
  3329. return true;
  3330. // The sections seem practically identical. Sort by name to get a
  3331. // stable sort.
  3332. return os1->name() < os2->name();
  3333. }
  3334. // Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
  3335. // if either OS1 or OS2 is not mentioned. This ensures that we keep
  3336. // empty sections in the order in which they appear in a linker
  3337. // script.
  3338. int
  3339. Sort_output_sections::script_compare(const Output_section* os1,
  3340. const Output_section* os2) const
  3341. {
  3342. if (this->elements_ == NULL)
  3343. return 0;
  3344. bool found_os1 = false;
  3345. bool found_os2 = false;
  3346. for (Script_sections::Sections_elements::const_iterator
  3347. p = this->elements_->begin();
  3348. p != this->elements_->end();
  3349. ++p)
  3350. {
  3351. if (os2 == (*p)->get_output_section())
  3352. {
  3353. if (found_os1)
  3354. return -1;
  3355. found_os2 = true;
  3356. }
  3357. else if (os1 == (*p)->get_output_section())
  3358. {
  3359. if (found_os2)
  3360. return 1;
  3361. found_os1 = true;
  3362. }
  3363. }
  3364. return 0;
  3365. }
  3366. // Return whether OS is a BSS section. This is a SHT_NOBITS section.
  3367. // We treat a section with the SHF_TLS flag set as taking up space
  3368. // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
  3369. // space for them in the file.
  3370. bool
  3371. Script_sections::is_bss_section(const Output_section* os)
  3372. {
  3373. return (os->type() == elfcpp::SHT_NOBITS
  3374. && (os->flags() & elfcpp::SHF_TLS) == 0);
  3375. }
  3376. // Return the size taken by the file header and the program headers.
  3377. size_t
  3378. Script_sections::total_header_size(Layout* layout) const
  3379. {
  3380. size_t segment_count = layout->segment_count();
  3381. size_t file_header_size;
  3382. size_t segment_headers_size;
  3383. if (parameters->target().get_size() == 32)
  3384. {
  3385. file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
  3386. segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
  3387. }
  3388. else if (parameters->target().get_size() == 64)
  3389. {
  3390. file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
  3391. segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
  3392. }
  3393. else
  3394. gold_unreachable();
  3395. return file_header_size + segment_headers_size;
  3396. }
  3397. // Return the amount we have to subtract from the LMA to accommodate
  3398. // headers of the given size. The complication is that the file
  3399. // header have to be at the start of a page, as otherwise it will not
  3400. // be at the start of the file.
  3401. uint64_t
  3402. Script_sections::header_size_adjustment(uint64_t lma,
  3403. size_t sizeof_headers) const
  3404. {
  3405. const uint64_t abi_pagesize = parameters->target().abi_pagesize();
  3406. uint64_t hdr_lma = lma - sizeof_headers;
  3407. hdr_lma &= ~(abi_pagesize - 1);
  3408. return lma - hdr_lma;
  3409. }
  3410. // Create the PT_LOAD segments when using a SECTIONS clause. Returns
  3411. // the segment which should hold the file header and segment headers,
  3412. // if any.
  3413. Output_segment*
  3414. Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
  3415. {
  3416. gold_assert(this->saw_sections_clause_);
  3417. if (parameters->options().relocatable())
  3418. return NULL;
  3419. if (this->saw_phdrs_clause())
  3420. return create_segments_from_phdrs_clause(layout, dot_alignment);
  3421. Layout::Section_list sections;
  3422. layout->get_allocated_sections(&sections);
  3423. // Sort the sections by address.
  3424. std::stable_sort(sections.begin(), sections.end(),
  3425. Sort_output_sections(this->sections_elements_));
  3426. this->create_note_and_tls_segments(layout, &sections);
  3427. // Walk through the sections adding them to PT_LOAD segments.
  3428. const uint64_t abi_pagesize = parameters->target().abi_pagesize();
  3429. Output_segment* first_seg = NULL;
  3430. Output_segment* current_seg = NULL;
  3431. bool is_current_seg_readonly = true;
  3432. uint64_t last_vma = 0;
  3433. uint64_t last_lma = 0;
  3434. uint64_t last_size = 0;
  3435. bool in_bss = false;
  3436. for (Layout::Section_list::iterator p = sections.begin();
  3437. p != sections.end();
  3438. ++p)
  3439. {
  3440. const uint64_t vma = (*p)->address();
  3441. const uint64_t lma = ((*p)->has_load_address()
  3442. ? (*p)->load_address()
  3443. : vma);
  3444. const uint64_t size = (*p)->current_data_size();
  3445. bool need_new_segment;
  3446. if (current_seg == NULL)
  3447. need_new_segment = true;
  3448. else if (lma - vma != last_lma - last_vma)
  3449. {
  3450. // This section has a different LMA relationship than the
  3451. // last one; we need a new segment.
  3452. need_new_segment = true;
  3453. }
  3454. else if (align_address(last_lma + last_size, abi_pagesize)
  3455. < align_address(lma, abi_pagesize))
  3456. {
  3457. // Putting this section in the segment would require
  3458. // skipping a page.
  3459. need_new_segment = true;
  3460. }
  3461. else if (in_bss && !is_bss_section(*p))
  3462. {
  3463. // A non-BSS section can not follow a BSS section in the
  3464. // same segment.
  3465. need_new_segment = true;
  3466. }
  3467. else if (is_current_seg_readonly
  3468. && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
  3469. && !parameters->options().omagic())
  3470. {
  3471. // Don't put a writable section in the same segment as a
  3472. // non-writable section.
  3473. need_new_segment = true;
  3474. }
  3475. else
  3476. {
  3477. // Otherwise, reuse the existing segment.
  3478. need_new_segment = false;
  3479. }
  3480. elfcpp::Elf_Word seg_flags =
  3481. Layout::section_flags_to_segment((*p)->flags());
  3482. if (need_new_segment)
  3483. {
  3484. current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
  3485. seg_flags);
  3486. current_seg->set_addresses(vma, lma);
  3487. current_seg->set_minimum_p_align(dot_alignment);
  3488. if (first_seg == NULL)
  3489. first_seg = current_seg;
  3490. is_current_seg_readonly = true;
  3491. in_bss = false;
  3492. }
  3493. current_seg->add_output_section_to_load(layout, *p, seg_flags);
  3494. if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
  3495. is_current_seg_readonly = false;
  3496. if (is_bss_section(*p) && size > 0)
  3497. in_bss = true;
  3498. last_vma = vma;
  3499. last_lma = lma;
  3500. last_size = size;
  3501. }
  3502. // An ELF program should work even if the program headers are not in
  3503. // a PT_LOAD segment. However, it appears that the Linux kernel
  3504. // does not set the AT_PHDR auxiliary entry in that case. It sets
  3505. // the load address to p_vaddr - p_offset of the first PT_LOAD
  3506. // segment. It then sets AT_PHDR to the load address plus the
  3507. // offset to the program headers, e_phoff in the file header. This
  3508. // fails when the program headers appear in the file before the
  3509. // first PT_LOAD segment. Therefore, we always create a PT_LOAD
  3510. // segment to hold the file header and the program headers. This is
  3511. // effectively what the GNU linker does, and it is slightly more
  3512. // efficient in any case. We try to use the first PT_LOAD segment
  3513. // if we can, otherwise we make a new one.
  3514. if (first_seg == NULL)
  3515. return NULL;
  3516. // -n or -N mean that the program is not demand paged and there is
  3517. // no need to put the program headers in a PT_LOAD segment.
  3518. if (parameters->options().nmagic() || parameters->options().omagic())
  3519. return NULL;
  3520. size_t sizeof_headers = this->total_header_size(layout);
  3521. uint64_t vma = first_seg->vaddr();
  3522. uint64_t lma = first_seg->paddr();
  3523. uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
  3524. if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
  3525. {
  3526. first_seg->set_addresses(vma - subtract, lma - subtract);
  3527. return first_seg;
  3528. }
  3529. // If there is no room to squeeze in the headers, then punt. The
  3530. // resulting executable probably won't run on GNU/Linux, but we
  3531. // trust that the user knows what they are doing.
  3532. if (lma < subtract || vma < subtract)
  3533. return NULL;
  3534. // If memory regions have been specified and the address range
  3535. // we are about to use is not contained within any region then
  3536. // issue a warning message about the segment we are going to
  3537. // create. It will be outside of any region and so possibly
  3538. // using non-existent or protected memory. We test LMA rather
  3539. // than VMA since we assume that the headers will never be
  3540. // relocated.
  3541. if (this->memory_regions_ != NULL
  3542. && !this->block_in_region (NULL, layout, lma - subtract, subtract))
  3543. gold_warning(_("creating a segment to contain the file and program"
  3544. " headers outside of any MEMORY region"));
  3545. Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
  3546. elfcpp::PF_R);
  3547. load_seg->set_addresses(vma - subtract, lma - subtract);
  3548. return load_seg;
  3549. }
  3550. // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
  3551. // segment if there are any SHT_TLS sections.
  3552. void
  3553. Script_sections::create_note_and_tls_segments(
  3554. Layout* layout,
  3555. const Layout::Section_list* sections)
  3556. {
  3557. gold_assert(!this->saw_phdrs_clause());
  3558. bool saw_tls = false;
  3559. for (Layout::Section_list::const_iterator p = sections->begin();
  3560. p != sections->end();
  3561. ++p)
  3562. {
  3563. if ((*p)->type() == elfcpp::SHT_NOTE)
  3564. {
  3565. elfcpp::Elf_Word seg_flags =
  3566. Layout::section_flags_to_segment((*p)->flags());
  3567. Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
  3568. seg_flags);
  3569. oseg->add_output_section_to_nonload(*p, seg_flags);
  3570. // Incorporate any subsequent SHT_NOTE sections, in the
  3571. // hopes that the script is sensible.
  3572. Layout::Section_list::const_iterator pnext = p + 1;
  3573. while (pnext != sections->end()
  3574. && (*pnext)->type() == elfcpp::SHT_NOTE)
  3575. {
  3576. seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
  3577. oseg->add_output_section_to_nonload(*pnext, seg_flags);
  3578. p = pnext;
  3579. ++pnext;
  3580. }
  3581. }
  3582. if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
  3583. {
  3584. if (saw_tls)
  3585. gold_error(_("TLS sections are not adjacent"));
  3586. elfcpp::Elf_Word seg_flags =
  3587. Layout::section_flags_to_segment((*p)->flags());
  3588. Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
  3589. seg_flags);
  3590. oseg->add_output_section_to_nonload(*p, seg_flags);
  3591. Layout::Section_list::const_iterator pnext = p + 1;
  3592. while (pnext != sections->end()
  3593. && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
  3594. {
  3595. seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
  3596. oseg->add_output_section_to_nonload(*pnext, seg_flags);
  3597. p = pnext;
  3598. ++pnext;
  3599. }
  3600. saw_tls = true;
  3601. }
  3602. // If we see a section named .interp then put the .interp section
  3603. // in a PT_INTERP segment.
  3604. // This is for GNU ld compatibility.
  3605. if (strcmp((*p)->name(), ".interp") == 0)
  3606. {
  3607. elfcpp::Elf_Word seg_flags =
  3608. Layout::section_flags_to_segment((*p)->flags());
  3609. Output_segment* oseg = layout->make_output_segment(elfcpp::PT_INTERP,
  3610. seg_flags);
  3611. oseg->add_output_section_to_nonload(*p, seg_flags);
  3612. }
  3613. }
  3614. this->segments_created_ = true;
  3615. }
  3616. // Add a program header. The PHDRS clause is syntactically distinct
  3617. // from the SECTIONS clause, but we implement it with the SECTIONS
  3618. // support because PHDRS is useless if there is no SECTIONS clause.
  3619. void
  3620. Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
  3621. bool includes_filehdr, bool includes_phdrs,
  3622. bool is_flags_valid, unsigned int flags,
  3623. Expression* load_address)
  3624. {
  3625. if (this->phdrs_elements_ == NULL)
  3626. this->phdrs_elements_ = new Phdrs_elements();
  3627. this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
  3628. includes_filehdr,
  3629. includes_phdrs,
  3630. is_flags_valid, flags,
  3631. load_address));
  3632. }
  3633. // Return the number of segments we expect to create based on the
  3634. // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
  3635. size_t
  3636. Script_sections::expected_segment_count(const Layout* layout) const
  3637. {
  3638. // If we've already created the segments, we won't be adding any more.
  3639. if (this->segments_created_)
  3640. return 0;
  3641. if (this->saw_phdrs_clause())
  3642. return this->phdrs_elements_->size();
  3643. Layout::Section_list sections;
  3644. layout->get_allocated_sections(&sections);
  3645. // We assume that we will need two PT_LOAD segments.
  3646. size_t ret = 2;
  3647. bool saw_note = false;
  3648. bool saw_tls = false;
  3649. bool saw_interp = false;
  3650. for (Layout::Section_list::const_iterator p = sections.begin();
  3651. p != sections.end();
  3652. ++p)
  3653. {
  3654. if ((*p)->type() == elfcpp::SHT_NOTE)
  3655. {
  3656. // Assume that all note sections will fit into a single
  3657. // PT_NOTE segment.
  3658. if (!saw_note)
  3659. {
  3660. ++ret;
  3661. saw_note = true;
  3662. }
  3663. }
  3664. else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
  3665. {
  3666. // There can only be one PT_TLS segment.
  3667. if (!saw_tls)
  3668. {
  3669. ++ret;
  3670. saw_tls = true;
  3671. }
  3672. }
  3673. else if (strcmp((*p)->name(), ".interp") == 0)
  3674. {
  3675. // There can only be one PT_INTERP segment.
  3676. if (!saw_interp)
  3677. {
  3678. ++ret;
  3679. saw_interp = true;
  3680. }
  3681. }
  3682. }
  3683. return ret;
  3684. }
  3685. // Create the segments from a PHDRS clause. Return the segment which
  3686. // should hold the file header and program headers, if any.
  3687. Output_segment*
  3688. Script_sections::create_segments_from_phdrs_clause(Layout* layout,
  3689. uint64_t dot_alignment)
  3690. {
  3691. this->attach_sections_using_phdrs_clause(layout);
  3692. return this->set_phdrs_clause_addresses(layout, dot_alignment);
  3693. }
  3694. // Create the segments from the PHDRS clause, and put the output
  3695. // sections in them.
  3696. void
  3697. Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
  3698. {
  3699. typedef std::map<std::string, Output_segment*> Name_to_segment;
  3700. Name_to_segment name_to_segment;
  3701. for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
  3702. p != this->phdrs_elements_->end();
  3703. ++p)
  3704. name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
  3705. this->segments_created_ = true;
  3706. // Walk through the output sections and attach them to segments.
  3707. // Output sections in the script which do not list segments are
  3708. // attached to the same set of segments as the immediately preceding
  3709. // output section.
  3710. String_list* phdr_names = NULL;
  3711. bool load_segments_only = false;
  3712. for (Sections_elements::const_iterator p = this->sections_elements_->begin();
  3713. p != this->sections_elements_->end();
  3714. ++p)
  3715. {
  3716. bool is_orphan;
  3717. String_list* old_phdr_names = phdr_names;
  3718. Output_section* os = (*p)->allocate_to_segment(&phdr_names, &is_orphan);
  3719. if (os == NULL)
  3720. continue;
  3721. elfcpp::Elf_Word seg_flags =
  3722. Layout::section_flags_to_segment(os->flags());
  3723. if (phdr_names == NULL)
  3724. {
  3725. // Don't worry about empty orphan sections.
  3726. if (is_orphan && os->current_data_size() > 0)
  3727. gold_error(_("allocated section %s not in any segment"),
  3728. os->name());
  3729. // To avoid later crashes drop this section into the first
  3730. // PT_LOAD segment.
  3731. for (Phdrs_elements::const_iterator ppe =
  3732. this->phdrs_elements_->begin();
  3733. ppe != this->phdrs_elements_->end();
  3734. ++ppe)
  3735. {
  3736. Output_segment* oseg = (*ppe)->segment();
  3737. if (oseg->type() == elfcpp::PT_LOAD)
  3738. {
  3739. oseg->add_output_section_to_load(layout, os, seg_flags);
  3740. break;
  3741. }
  3742. }
  3743. continue;
  3744. }
  3745. // We see a list of segments names. Disable PT_LOAD segment only
  3746. // filtering.
  3747. if (old_phdr_names != phdr_names)
  3748. load_segments_only = false;
  3749. // If this is an orphan section--one that was not explicitly
  3750. // mentioned in the linker script--then it should not inherit
  3751. // any segment type other than PT_LOAD. Otherwise, e.g., the
  3752. // PT_INTERP segment will pick up following orphan sections,
  3753. // which does not make sense. If this is not an orphan section,
  3754. // we trust the linker script.
  3755. if (is_orphan)
  3756. {
  3757. // Enable PT_LOAD segments only filtering until we see another
  3758. // list of segment names.
  3759. load_segments_only = true;
  3760. }
  3761. bool in_load_segment = false;
  3762. for (String_list::const_iterator q = phdr_names->begin();
  3763. q != phdr_names->end();
  3764. ++q)
  3765. {
  3766. Name_to_segment::const_iterator r = name_to_segment.find(*q);
  3767. if (r == name_to_segment.end())
  3768. gold_error(_("no segment %s"), q->c_str());
  3769. else
  3770. {
  3771. if (load_segments_only
  3772. && r->second->type() != elfcpp::PT_LOAD)
  3773. continue;
  3774. if (r->second->type() != elfcpp::PT_LOAD)
  3775. r->second->add_output_section_to_nonload(os, seg_flags);
  3776. else
  3777. {
  3778. r->second->add_output_section_to_load(layout, os, seg_flags);
  3779. if (in_load_segment)
  3780. gold_error(_("section in two PT_LOAD segments"));
  3781. in_load_segment = true;
  3782. }
  3783. }
  3784. }
  3785. if (!in_load_segment)
  3786. gold_error(_("allocated section not in any PT_LOAD segment"));
  3787. }
  3788. }
  3789. // Set the addresses for segments created from a PHDRS clause. Return
  3790. // the segment which should hold the file header and program headers,
  3791. // if any.
  3792. Output_segment*
  3793. Script_sections::set_phdrs_clause_addresses(Layout* layout,
  3794. uint64_t dot_alignment)
  3795. {
  3796. Output_segment* load_seg = NULL;
  3797. for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
  3798. p != this->phdrs_elements_->end();
  3799. ++p)
  3800. {
  3801. // Note that we have to set the flags after adding the output
  3802. // sections to the segment, as adding an output segment can
  3803. // change the flags.
  3804. (*p)->set_flags_if_valid();
  3805. Output_segment* oseg = (*p)->segment();
  3806. if (oseg->type() != elfcpp::PT_LOAD)
  3807. {
  3808. // The addresses of non-PT_LOAD segments are set from the
  3809. // PT_LOAD segments.
  3810. if ((*p)->has_load_address())
  3811. gold_error(_("may only specify load address for PT_LOAD segment"));
  3812. continue;
  3813. }
  3814. oseg->set_minimum_p_align(dot_alignment);
  3815. // The output sections should have addresses from the SECTIONS
  3816. // clause. The addresses don't have to be in order, so find the
  3817. // one with the lowest load address. Use that to set the
  3818. // address of the segment.
  3819. Output_section* osec = oseg->section_with_lowest_load_address();
  3820. if (osec == NULL)
  3821. {
  3822. oseg->set_addresses(0, 0);
  3823. continue;
  3824. }
  3825. uint64_t vma = osec->address();
  3826. uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
  3827. // Override the load address of the section with the load
  3828. // address specified for the segment.
  3829. if ((*p)->has_load_address())
  3830. {
  3831. if (osec->has_load_address())
  3832. gold_warning(_("PHDRS load address overrides "
  3833. "section %s load address"),
  3834. osec->name());
  3835. lma = (*p)->load_address();
  3836. }
  3837. bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
  3838. if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
  3839. {
  3840. // We could support this if we wanted to.
  3841. gold_error(_("using only one of FILEHDR and PHDRS is "
  3842. "not currently supported"));
  3843. }
  3844. if (headers)
  3845. {
  3846. size_t sizeof_headers = this->total_header_size(layout);
  3847. uint64_t subtract = this->header_size_adjustment(lma,
  3848. sizeof_headers);
  3849. if (lma >= subtract && vma >= subtract)
  3850. {
  3851. lma -= subtract;
  3852. vma -= subtract;
  3853. }
  3854. else
  3855. {
  3856. gold_error(_("sections loaded on first page without room "
  3857. "for file and program headers "
  3858. "are not supported"));
  3859. }
  3860. if (load_seg != NULL)
  3861. gold_error(_("using FILEHDR and PHDRS on more than one "
  3862. "PT_LOAD segment is not currently supported"));
  3863. load_seg = oseg;
  3864. }
  3865. oseg->set_addresses(vma, lma);
  3866. }
  3867. return load_seg;
  3868. }
  3869. // Add the file header and segment headers to non-load segments
  3870. // specified in the PHDRS clause.
  3871. void
  3872. Script_sections::put_headers_in_phdrs(Output_data* file_header,
  3873. Output_data* segment_headers)
  3874. {
  3875. gold_assert(this->saw_phdrs_clause());
  3876. for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
  3877. p != this->phdrs_elements_->end();
  3878. ++p)
  3879. {
  3880. if ((*p)->type() != elfcpp::PT_LOAD)
  3881. {
  3882. if ((*p)->includes_phdrs())
  3883. (*p)->segment()->add_initial_output_data(segment_headers);
  3884. if ((*p)->includes_filehdr())
  3885. (*p)->segment()->add_initial_output_data(file_header);
  3886. }
  3887. }
  3888. }
  3889. // Look for an output section by name and return the address, the load
  3890. // address, the alignment, and the size. This is used when an
  3891. // expression refers to an output section which was not actually
  3892. // created. This returns true if the section was found, false
  3893. // otherwise.
  3894. bool
  3895. Script_sections::get_output_section_info(const char* name, uint64_t* address,
  3896. uint64_t* load_address,
  3897. uint64_t* addralign,
  3898. uint64_t* size) const
  3899. {
  3900. if (!this->saw_sections_clause_)
  3901. return false;
  3902. for (Sections_elements::const_iterator p = this->sections_elements_->begin();
  3903. p != this->sections_elements_->end();
  3904. ++p)
  3905. if ((*p)->get_output_section_info(name, address, load_address, addralign,
  3906. size))
  3907. return true;
  3908. return false;
  3909. }
  3910. // Release all Output_segments. This remove all pointers to all
  3911. // Output_segments.
  3912. void
  3913. Script_sections::release_segments()
  3914. {
  3915. if (this->saw_phdrs_clause())
  3916. {
  3917. for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
  3918. p != this->phdrs_elements_->end();
  3919. ++p)
  3920. (*p)->release_segment();
  3921. }
  3922. this->segments_created_ = false;
  3923. }
  3924. // Print the SECTIONS clause to F for debugging.
  3925. void
  3926. Script_sections::print(FILE* f) const
  3927. {
  3928. if (this->phdrs_elements_ != NULL)
  3929. {
  3930. fprintf(f, "PHDRS {\n");
  3931. for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
  3932. p != this->phdrs_elements_->end();
  3933. ++p)
  3934. (*p)->print(f);
  3935. fprintf(f, "}\n");
  3936. }
  3937. if (this->memory_regions_ != NULL)
  3938. {
  3939. fprintf(f, "MEMORY {\n");
  3940. for (Memory_regions::const_iterator m = this->memory_regions_->begin();
  3941. m != this->memory_regions_->end();
  3942. ++m)
  3943. (*m)->print(f);
  3944. fprintf(f, "}\n");
  3945. }
  3946. if (!this->saw_sections_clause_)
  3947. return;
  3948. fprintf(f, "SECTIONS {\n");
  3949. for (Sections_elements::const_iterator p = this->sections_elements_->begin();
  3950. p != this->sections_elements_->end();
  3951. ++p)
  3952. (*p)->print(f);
  3953. fprintf(f, "}\n");
  3954. }
  3955. } // End namespace gold.