script-sections.cc 123 KB

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