layout.h 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. // layout.h -- lay out output file sections for gold -*- C++ -*-
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_LAYOUT_H
  18. #define GOLD_LAYOUT_H
  19. #include <cstring>
  20. #include <list>
  21. #include <map>
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include "script.h"
  26. #include "workqueue.h"
  27. #include "object.h"
  28. #include "dynobj.h"
  29. #include "stringpool.h"
  30. namespace gold
  31. {
  32. class General_options;
  33. class Incremental_inputs;
  34. class Incremental_binary;
  35. class Input_objects;
  36. class Mapfile;
  37. class Symbol_table;
  38. class Output_section_data;
  39. class Output_section;
  40. class Output_section_headers;
  41. class Output_segment_headers;
  42. class Output_file_header;
  43. class Output_segment;
  44. class Output_data;
  45. class Output_data_reloc_generic;
  46. class Output_data_dynamic;
  47. class Output_symtab_xindex;
  48. class Output_reduced_debug_abbrev_section;
  49. class Output_reduced_debug_info_section;
  50. class Eh_frame;
  51. class Gdb_index;
  52. class Target;
  53. struct Timespec;
  54. // Return TRUE if SECNAME is the name of a compressed debug section.
  55. extern bool
  56. is_compressed_debug_section(const char* secname);
  57. // Return the name of the corresponding uncompressed debug section.
  58. extern std::string
  59. corresponding_uncompressed_section_name(std::string secname);
  60. // Maintain a list of free space within a section, segment, or file.
  61. // Used for incremental update links.
  62. class Free_list
  63. {
  64. public:
  65. struct Free_list_node
  66. {
  67. Free_list_node(off_t start, off_t end)
  68. : start_(start), end_(end)
  69. { }
  70. off_t start_;
  71. off_t end_;
  72. };
  73. typedef std::list<Free_list_node>::const_iterator Const_iterator;
  74. Free_list()
  75. : list_(), last_remove_(list_.begin()), extend_(false), length_(0),
  76. min_hole_(0)
  77. { }
  78. // Initialize the free list for a section of length LEN.
  79. // If EXTEND is true, free space may be allocated past the end.
  80. void
  81. init(off_t len, bool extend);
  82. // Set the minimum hole size that is allowed when allocating
  83. // from the free list.
  84. void
  85. set_min_hole_size(off_t min_hole)
  86. { this->min_hole_ = min_hole; }
  87. // Remove a chunk from the free list.
  88. void
  89. remove(off_t start, off_t end);
  90. // Allocate a chunk of space from the free list of length LEN,
  91. // with alignment ALIGN, and minimum offset MINOFF.
  92. off_t
  93. allocate(off_t len, uint64_t align, off_t minoff);
  94. // Return an iterator for the beginning of the free list.
  95. Const_iterator
  96. begin() const
  97. { return this->list_.begin(); }
  98. // Return an iterator for the end of the free list.
  99. Const_iterator
  100. end() const
  101. { return this->list_.end(); }
  102. // Dump the free list (for debugging).
  103. void
  104. dump();
  105. // Print usage statistics.
  106. static void
  107. print_stats();
  108. private:
  109. typedef std::list<Free_list_node>::iterator Iterator;
  110. // The free list.
  111. std::list<Free_list_node> list_;
  112. // The last node visited during a remove operation.
  113. Iterator last_remove_;
  114. // Whether we can extend past the original length.
  115. bool extend_;
  116. // The total length of the section, segment, or file.
  117. off_t length_;
  118. // The minimum hole size allowed. When allocating from the free list,
  119. // we must not leave a hole smaller than this.
  120. off_t min_hole_;
  121. // Statistics:
  122. // The total number of free lists used.
  123. static unsigned int num_lists;
  124. // The total number of free list nodes used.
  125. static unsigned int num_nodes;
  126. // The total number of calls to Free_list::remove.
  127. static unsigned int num_removes;
  128. // The total number of nodes visited during calls to Free_list::remove.
  129. static unsigned int num_remove_visits;
  130. // The total number of calls to Free_list::allocate.
  131. static unsigned int num_allocates;
  132. // The total number of nodes visited during calls to Free_list::allocate.
  133. static unsigned int num_allocate_visits;
  134. };
  135. // This task function handles mapping the input sections to output
  136. // sections and laying them out in memory.
  137. class Layout_task_runner : public Task_function_runner
  138. {
  139. public:
  140. // OPTIONS is the command line options, INPUT_OBJECTS is the list of
  141. // input objects, SYMTAB is the symbol table, LAYOUT is the layout
  142. // object.
  143. Layout_task_runner(const General_options& options,
  144. const Input_objects* input_objects,
  145. Symbol_table* symtab,
  146. Target* target,
  147. Layout* layout,
  148. Mapfile* mapfile)
  149. : options_(options), input_objects_(input_objects), symtab_(symtab),
  150. target_(target), layout_(layout), mapfile_(mapfile)
  151. { }
  152. // Run the operation.
  153. void
  154. run(Workqueue*, const Task*);
  155. private:
  156. Layout_task_runner(const Layout_task_runner&);
  157. Layout_task_runner& operator=(const Layout_task_runner&);
  158. const General_options& options_;
  159. const Input_objects* input_objects_;
  160. Symbol_table* symtab_;
  161. Target* target_;
  162. Layout* layout_;
  163. Mapfile* mapfile_;
  164. };
  165. // This class holds information about the comdat group or
  166. // .gnu.linkonce section that will be kept for a given signature.
  167. class Kept_section
  168. {
  169. private:
  170. // For a comdat group, we build a mapping from the name of each
  171. // section in the group to the section index and the size in object.
  172. // When we discard a group in some other object file, we use this
  173. // map to figure out which kept section the discarded section is
  174. // associated with. We then use that mapping when processing relocs
  175. // against discarded sections.
  176. struct Comdat_section_info
  177. {
  178. // The section index.
  179. unsigned int shndx;
  180. // The section size.
  181. uint64_t size;
  182. Comdat_section_info(unsigned int a_shndx, uint64_t a_size)
  183. : shndx(a_shndx), size(a_size)
  184. { }
  185. };
  186. // Most comdat groups have only one or two sections, so we use a
  187. // std::map rather than an Unordered_map to optimize for that case
  188. // without paying too heavily for groups with more sections.
  189. typedef std::map<std::string, Comdat_section_info> Comdat_group;
  190. public:
  191. Kept_section()
  192. : object_(NULL), shndx_(0), is_comdat_(false), is_group_name_(false)
  193. { this->u_.linkonce_size = 0; }
  194. // We need to support copies for the signature map in the Layout
  195. // object, but we should never copy an object after it has been
  196. // marked as a comdat section.
  197. Kept_section(const Kept_section& k)
  198. : object_(k.object_), shndx_(k.shndx_), is_comdat_(false),
  199. is_group_name_(k.is_group_name_)
  200. {
  201. gold_assert(!k.is_comdat_);
  202. this->u_.linkonce_size = 0;
  203. }
  204. ~Kept_section()
  205. {
  206. if (this->is_comdat_)
  207. delete this->u_.group_sections;
  208. }
  209. // The object where this section lives.
  210. Relobj*
  211. object() const
  212. { return this->object_; }
  213. // Set the object.
  214. void
  215. set_object(Relobj* object)
  216. {
  217. gold_assert(this->object_ == NULL);
  218. this->object_ = object;
  219. }
  220. // The section index.
  221. unsigned int
  222. shndx() const
  223. { return this->shndx_; }
  224. // Set the section index.
  225. void
  226. set_shndx(unsigned int shndx)
  227. {
  228. gold_assert(this->shndx_ == 0);
  229. this->shndx_ = shndx;
  230. }
  231. // Whether this is a comdat group.
  232. bool
  233. is_comdat() const
  234. { return this->is_comdat_; }
  235. // Set that this is a comdat group.
  236. void
  237. set_is_comdat()
  238. {
  239. gold_assert(!this->is_comdat_);
  240. this->is_comdat_ = true;
  241. this->u_.group_sections = new Comdat_group();
  242. }
  243. // Whether this is associated with the name of a group or section
  244. // rather than the symbol name derived from a linkonce section.
  245. bool
  246. is_group_name() const
  247. { return this->is_group_name_; }
  248. // Note that this represents a comdat group rather than a single
  249. // linkonce section.
  250. void
  251. set_is_group_name()
  252. { this->is_group_name_ = true; }
  253. // Add a section to the group list.
  254. void
  255. add_comdat_section(const std::string& name, unsigned int shndx,
  256. uint64_t size)
  257. {
  258. gold_assert(this->is_comdat_);
  259. Comdat_section_info sinfo(shndx, size);
  260. this->u_.group_sections->insert(std::make_pair(name, sinfo));
  261. }
  262. // Look for a section name in the group list, and return whether it
  263. // was found. If found, returns the section index and size.
  264. bool
  265. find_comdat_section(const std::string& name, unsigned int* pshndx,
  266. uint64_t* psize) const
  267. {
  268. gold_assert(this->is_comdat_);
  269. Comdat_group::const_iterator p = this->u_.group_sections->find(name);
  270. if (p == this->u_.group_sections->end())
  271. return false;
  272. *pshndx = p->second.shndx;
  273. *psize = p->second.size;
  274. return true;
  275. }
  276. // If there is only one section in the group list, return true, and
  277. // return the section index and size.
  278. bool
  279. find_single_comdat_section(unsigned int* pshndx, uint64_t* psize) const
  280. {
  281. gold_assert(this->is_comdat_);
  282. if (this->u_.group_sections->size() != 1)
  283. return false;
  284. Comdat_group::const_iterator p = this->u_.group_sections->begin();
  285. *pshndx = p->second.shndx;
  286. *psize = p->second.size;
  287. return true;
  288. }
  289. // Return the size of a linkonce section.
  290. uint64_t
  291. linkonce_size() const
  292. {
  293. gold_assert(!this->is_comdat_);
  294. return this->u_.linkonce_size;
  295. }
  296. // Set the size of a linkonce section.
  297. void
  298. set_linkonce_size(uint64_t size)
  299. {
  300. gold_assert(!this->is_comdat_);
  301. this->u_.linkonce_size = size;
  302. }
  303. private:
  304. // No assignment.
  305. Kept_section& operator=(const Kept_section&);
  306. // The object containing the comdat group or .gnu.linkonce section.
  307. Relobj* object_;
  308. // Index of the group section for comdats and the section itself for
  309. // .gnu.linkonce.
  310. unsigned int shndx_;
  311. // True if this is for a comdat group rather than a .gnu.linkonce
  312. // section.
  313. bool is_comdat_;
  314. // The Kept_sections are values of a mapping, that maps names to
  315. // them. This field is true if this struct is associated with the
  316. // name of a comdat or .gnu.linkonce, false if it is associated with
  317. // the name of a symbol obtained from the .gnu.linkonce.* name
  318. // through some heuristics.
  319. bool is_group_name_;
  320. union
  321. {
  322. // If the is_comdat_ field is true, this holds a map from names of
  323. // the sections in the group to section indexes in object_ and to
  324. // section sizes.
  325. Comdat_group* group_sections;
  326. // If the is_comdat_ field is false, this holds the size of the
  327. // single section.
  328. uint64_t linkonce_size;
  329. } u_;
  330. };
  331. // The ordering for output sections. This controls how output
  332. // sections are ordered within a PT_LOAD output segment.
  333. enum Output_section_order
  334. {
  335. // Unspecified. Used for non-load segments. Also used for the file
  336. // and segment headers.
  337. ORDER_INVALID,
  338. // The PT_INTERP section should come first, so that the dynamic
  339. // linker can pick it up quickly.
  340. ORDER_INTERP,
  341. // The .note.gnu.property section comes next so that the PT_NOTE
  342. // segment is on the first page of the executable and it won't be
  343. // placed between other note sections with different alignments.
  344. ORDER_PROPERTY_NOTE,
  345. // Loadable read-only note sections come after the .note.gnu.property
  346. // section.
  347. ORDER_RO_NOTE,
  348. // Put read-only sections used by the dynamic linker early in the
  349. // executable to minimize paging.
  350. ORDER_DYNAMIC_LINKER,
  351. // Put reloc sections used by the dynamic linker after other
  352. // sections used by the dynamic linker; otherwise, objcopy and strip
  353. // get confused.
  354. ORDER_DYNAMIC_RELOCS,
  355. // Put the PLT reloc section after the other dynamic relocs;
  356. // otherwise, prelink gets confused.
  357. ORDER_DYNAMIC_PLT_RELOCS,
  358. // The .init section.
  359. ORDER_INIT,
  360. // The PLT.
  361. ORDER_PLT,
  362. // The hot text sections, prefixed by .text.hot.
  363. ORDER_TEXT_HOT,
  364. // The regular text sections.
  365. ORDER_TEXT,
  366. // The startup text sections, prefixed by .text.startup.
  367. ORDER_TEXT_STARTUP,
  368. // The startup text sections, prefixed by .text.startup.
  369. ORDER_TEXT_EXIT,
  370. // The unlikely text sections, prefixed by .text.unlikely.
  371. ORDER_TEXT_UNLIKELY,
  372. // The .fini section.
  373. ORDER_FINI,
  374. // The read-only sections.
  375. ORDER_READONLY,
  376. // The exception frame sections.
  377. ORDER_EHFRAME,
  378. // The TLS sections come first in the data section.
  379. ORDER_TLS_DATA,
  380. ORDER_TLS_BSS,
  381. // Local RELRO (read-only after relocation) sections come before
  382. // non-local RELRO sections. This data will be fully resolved by
  383. // the prelinker.
  384. ORDER_RELRO_LOCAL,
  385. // Non-local RELRO sections are grouped together after local RELRO
  386. // sections. All RELRO sections must be adjacent so that they can
  387. // all be put into a PT_GNU_RELRO segment.
  388. ORDER_RELRO,
  389. // We permit marking exactly one output section as the last RELRO
  390. // section. We do this so that the read-only GOT can be adjacent to
  391. // the writable GOT.
  392. ORDER_RELRO_LAST,
  393. // Similarly, we permit marking exactly one output section as the
  394. // first non-RELRO section.
  395. ORDER_NON_RELRO_FIRST,
  396. // The regular data sections come after the RELRO sections.
  397. ORDER_DATA,
  398. // Large data sections normally go in large data segments.
  399. ORDER_LARGE_DATA,
  400. // Group writable notes so that we can have a single PT_NOTE
  401. // segment.
  402. ORDER_RW_NOTE,
  403. // The small data sections must be at the end of the data sections,
  404. // so that they can be adjacent to the small BSS sections.
  405. ORDER_SMALL_DATA,
  406. // The BSS sections start here.
  407. // The small BSS sections must be at the start of the BSS sections,
  408. // so that they can be adjacent to the small data sections.
  409. ORDER_SMALL_BSS,
  410. // The regular BSS sections.
  411. ORDER_BSS,
  412. // The large BSS sections come after the other BSS sections.
  413. ORDER_LARGE_BSS,
  414. // Maximum value.
  415. ORDER_MAX
  416. };
  417. // This class handles the details of laying out input sections.
  418. class Layout
  419. {
  420. public:
  421. Layout(int number_of_input_files, Script_options*);
  422. ~Layout()
  423. {
  424. delete this->relaxation_debug_check_;
  425. delete this->segment_states_;
  426. }
  427. // For incremental links, record the base file to be modified.
  428. void
  429. set_incremental_base(Incremental_binary* base);
  430. Incremental_binary*
  431. incremental_base()
  432. { return this->incremental_base_; }
  433. // For incremental links, record the initial fixed layout of a section
  434. // from the base file, and return a pointer to the Output_section.
  435. template<int size, bool big_endian>
  436. Output_section*
  437. init_fixed_output_section(const char*, elfcpp::Shdr<size, big_endian>&);
  438. // Given an input section SHNDX, named NAME, with data in SHDR, from
  439. // the object file OBJECT, return the output section where this
  440. // input section should go. RELOC_SHNDX is the index of a
  441. // relocation section which applies to this section, or 0 if none,
  442. // or -1U if more than one. RELOC_TYPE is the type of the
  443. // relocation section if there is one. Set *OFFSET to the offset
  444. // within the output section.
  445. template<int size, bool big_endian>
  446. Output_section*
  447. layout(Sized_relobj_file<size, big_endian> *object, unsigned int shndx,
  448. const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
  449. unsigned int sh_type, unsigned int reloc_shndx,
  450. unsigned int reloc_type, off_t* offset);
  451. std::map<Section_id, unsigned int>*
  452. get_section_order_map()
  453. { return &this->section_order_map_; }
  454. // Struct to store segment info when mapping some input sections to
  455. // unique segments using linker plugins. Mapping an input section to
  456. // a unique segment is done by first placing such input sections in
  457. // unique output sections and then mapping the output section to a
  458. // unique segment. NAME is the name of the output section. FLAGS
  459. // and ALIGN are the extra flags and alignment of the segment.
  460. struct Unique_segment_info
  461. {
  462. // Identifier for the segment. ELF segments don't have names. This
  463. // is used as the name of the output section mapped to the segment.
  464. const char* name;
  465. // Additional segment flags.
  466. uint64_t flags;
  467. // Segment alignment.
  468. uint64_t align;
  469. };
  470. // Mapping from input section to segment.
  471. typedef std::map<Const_section_id, Unique_segment_info*>
  472. Section_segment_map;
  473. // Maps section SECN to SEGMENT s.
  474. void
  475. insert_section_segment_map(Const_section_id secn, Unique_segment_info *s);
  476. // Some input sections require special ordering, for compatibility
  477. // with GNU ld. Given the name of an input section, return -1 if it
  478. // does not require special ordering. Otherwise, return the index
  479. // by which it should be ordered compared to other input sections
  480. // that require special ordering.
  481. static int
  482. special_ordering_of_input_section(const char* name);
  483. bool
  484. is_section_ordering_specified()
  485. { return this->section_ordering_specified_; }
  486. void
  487. set_section_ordering_specified()
  488. { this->section_ordering_specified_ = true; }
  489. bool
  490. is_unique_segment_for_sections_specified() const
  491. { return this->unique_segment_for_sections_specified_; }
  492. void
  493. set_unique_segment_for_sections_specified()
  494. { this->unique_segment_for_sections_specified_ = true; }
  495. bool
  496. is_lto_slim_object () const
  497. { return this->lto_slim_object_; }
  498. void
  499. set_lto_slim_object ()
  500. { this->lto_slim_object_ = true; }
  501. // For incremental updates, allocate a block of memory from the
  502. // free list. Find a block starting at or after MINOFF.
  503. off_t
  504. allocate(off_t len, uint64_t align, off_t minoff)
  505. { return this->free_list_.allocate(len, align, minoff); }
  506. unsigned int
  507. find_section_order_index(const std::string&);
  508. // Read the sequence of input sections from the file specified with
  509. // linker option --section-ordering-file.
  510. void
  511. read_layout_from_file();
  512. // Layout an input reloc section when doing a relocatable link. The
  513. // section is RELOC_SHNDX in OBJECT, with data in SHDR.
  514. // DATA_SECTION is the reloc section to which it refers. RR is the
  515. // relocatable information.
  516. template<int size, bool big_endian>
  517. Output_section*
  518. layout_reloc(Sized_relobj_file<size, big_endian>* object,
  519. unsigned int reloc_shndx,
  520. const elfcpp::Shdr<size, big_endian>& shdr,
  521. Output_section* data_section,
  522. Relocatable_relocs* rr);
  523. // Layout a group section when doing a relocatable link.
  524. template<int size, bool big_endian>
  525. void
  526. layout_group(Symbol_table* symtab,
  527. Sized_relobj_file<size, big_endian>* object,
  528. unsigned int group_shndx,
  529. const char* group_section_name,
  530. const char* signature,
  531. const elfcpp::Shdr<size, big_endian>& shdr,
  532. elfcpp::Elf_Word flags,
  533. std::vector<unsigned int>* shndxes);
  534. // Like layout, only for exception frame sections. OBJECT is an
  535. // object file. SYMBOLS is the contents of the symbol table
  536. // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
  537. // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
  538. // .eh_frame section in OBJECT. SHDR is the section header.
  539. // RELOC_SHNDX is the index of a relocation section which applies to
  540. // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
  541. // is the type of the relocation section if there is one. This
  542. // returns the output section, and sets *OFFSET to the offset.
  543. template<int size, bool big_endian>
  544. Output_section*
  545. layout_eh_frame(Sized_relobj_file<size, big_endian>* object,
  546. const unsigned char* symbols,
  547. off_t symbols_size,
  548. const unsigned char* symbol_names,
  549. off_t symbol_names_size,
  550. unsigned int shndx,
  551. const elfcpp::Shdr<size, big_endian>& shdr,
  552. unsigned int reloc_shndx, unsigned int reloc_type,
  553. off_t* offset);
  554. // After processing all input files, we call this to make sure that
  555. // the optimized .eh_frame sections have been added to the output
  556. // section.
  557. void
  558. finalize_eh_frame_section();
  559. // Add .eh_frame information for a PLT. The FDE must start with a
  560. // 4-byte PC-relative reference to the start of the PLT, followed by
  561. // a 4-byte size of PLT.
  562. void
  563. add_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
  564. size_t cie_length, const unsigned char* fde_data,
  565. size_t fde_length);
  566. // Remove all post-map .eh_frame information for a PLT.
  567. void
  568. remove_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
  569. size_t cie_length);
  570. // Scan a .debug_info or .debug_types section, and add summary
  571. // information to the .gdb_index section.
  572. template<int size, bool big_endian>
  573. void
  574. add_to_gdb_index(bool is_type_unit,
  575. Sized_relobj<size, big_endian>* object,
  576. const unsigned char* symbols,
  577. off_t symbols_size,
  578. unsigned int shndx,
  579. unsigned int reloc_shndx,
  580. unsigned int reloc_type);
  581. // Handle a GNU stack note. This is called once per input object
  582. // file. SEEN_GNU_STACK is true if the object file has a
  583. // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
  584. // from that section if there was one.
  585. void
  586. layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
  587. const Object*);
  588. // Layout a .note.gnu.property section.
  589. void
  590. layout_gnu_property(unsigned int note_type,
  591. unsigned int pr_type,
  592. size_t pr_datasz,
  593. const unsigned char* pr_data,
  594. const Object* object);
  595. // Merge per-object properties with program properties.
  596. void
  597. merge_gnu_properties(const Object* object);
  598. // Add a target-specific property for the output .note.gnu.property section.
  599. void
  600. add_gnu_property(unsigned int note_type,
  601. unsigned int pr_type,
  602. size_t pr_datasz,
  603. const unsigned char* pr_data);
  604. // Add an Output_section_data to the layout. This is used for
  605. // special sections like the GOT section. ORDER is where the
  606. // section should wind up in the output segment. IS_RELRO is true
  607. // for relro sections.
  608. Output_section*
  609. add_output_section_data(const char* name, elfcpp::Elf_Word type,
  610. elfcpp::Elf_Xword flags,
  611. Output_section_data*, Output_section_order order,
  612. bool is_relro);
  613. // Increase the size of the relro segment by this much.
  614. void
  615. increase_relro(unsigned int s)
  616. { this->increase_relro_ += s; }
  617. // Create dynamic sections if necessary.
  618. void
  619. create_initial_dynamic_sections(Symbol_table*);
  620. // Define __start and __stop symbols for output sections.
  621. void
  622. define_section_symbols(Symbol_table*);
  623. // Create automatic note sections.
  624. void
  625. create_notes();
  626. // Create sections for linker scripts.
  627. void
  628. create_script_sections()
  629. { this->script_options_->create_script_sections(this); }
  630. // Define symbols from any linker script.
  631. void
  632. define_script_symbols(Symbol_table* symtab)
  633. { this->script_options_->add_symbols_to_table(symtab); }
  634. // Define symbols for group signatures.
  635. void
  636. define_group_signatures(Symbol_table*);
  637. // Return the Stringpool used for symbol names.
  638. const Stringpool*
  639. sympool() const
  640. { return &this->sympool_; }
  641. // Return the Stringpool used for dynamic symbol names and dynamic
  642. // tags.
  643. const Stringpool*
  644. dynpool() const
  645. { return &this->dynpool_; }
  646. // Return the .dynamic output section. This is only valid after the
  647. // layout has been finalized.
  648. Output_section*
  649. dynamic_section() const
  650. { return this->dynamic_section_; }
  651. // Return the symtab_xindex section used to hold large section
  652. // indexes for the normal symbol table.
  653. Output_symtab_xindex*
  654. symtab_xindex() const
  655. { return this->symtab_xindex_; }
  656. // Return the dynsym_xindex section used to hold large section
  657. // indexes for the dynamic symbol table.
  658. Output_symtab_xindex*
  659. dynsym_xindex() const
  660. { return this->dynsym_xindex_; }
  661. // Return whether a section is a .gnu.linkonce section, given the
  662. // section name.
  663. static inline bool
  664. is_linkonce(const char* name)
  665. { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
  666. // Whether we have added an input section.
  667. bool
  668. have_added_input_section() const
  669. { return this->have_added_input_section_; }
  670. // Return true if a section is a debugging section.
  671. static inline bool
  672. is_debug_info_section(const char* name)
  673. {
  674. // Debugging sections can only be recognized by name.
  675. return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
  676. || strncmp(name, ".zdebug", sizeof(".zdebug") - 1) == 0
  677. || strncmp(name, ".gnu.linkonce.wi.",
  678. sizeof(".gnu.linkonce.wi.") - 1) == 0
  679. || strncmp(name, ".line", sizeof(".line") - 1) == 0
  680. || strncmp(name, ".stab", sizeof(".stab") - 1) == 0
  681. || strncmp(name, ".pdr", sizeof(".pdr") - 1) == 0);
  682. }
  683. // Return true if RELOBJ is an input file whose base name matches
  684. // FILE_NAME. The base name must have an extension of ".o", and
  685. // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
  686. static bool
  687. match_file_name(const Relobj* relobj, const char* file_name);
  688. // Return whether section SHNDX in RELOBJ is a .ctors/.dtors section
  689. // with more than one word being mapped to a .init_array/.fini_array
  690. // section.
  691. bool
  692. is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const;
  693. // Check if a comdat group or .gnu.linkonce section with the given
  694. // NAME is selected for the link. If there is already a section,
  695. // *KEPT_SECTION is set to point to the signature and the function
  696. // returns false. Otherwise, OBJECT, SHNDX,IS_COMDAT, and
  697. // IS_GROUP_NAME are recorded for this NAME in the layout object,
  698. // *KEPT_SECTION is set to the internal copy and the function return
  699. // false.
  700. bool
  701. find_or_add_kept_section(const std::string& name, Relobj* object,
  702. unsigned int shndx, bool is_comdat,
  703. bool is_group_name, Kept_section** kept_section);
  704. // Finalize the layout after all the input sections have been added.
  705. off_t
  706. finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
  707. // Return whether any sections require postprocessing.
  708. bool
  709. any_postprocessing_sections() const
  710. { return this->any_postprocessing_sections_; }
  711. // Return the size of the output file.
  712. off_t
  713. output_file_size() const
  714. { return this->output_file_size_; }
  715. // Return the TLS segment. This will return NULL if there isn't
  716. // one.
  717. Output_segment*
  718. tls_segment() const
  719. { return this->tls_segment_; }
  720. // Return the normal symbol table.
  721. Output_section*
  722. symtab_section() const
  723. {
  724. gold_assert(this->symtab_section_ != NULL);
  725. return this->symtab_section_;
  726. }
  727. // Return the file offset of the normal symbol table.
  728. off_t
  729. symtab_section_offset() const;
  730. // Return the section index of the normal symbol tabl.e
  731. unsigned int
  732. symtab_section_shndx() const;
  733. // Return the dynamic symbol table.
  734. Output_section*
  735. dynsym_section() const
  736. {
  737. gold_assert(this->dynsym_section_ != NULL);
  738. return this->dynsym_section_;
  739. }
  740. // Return the dynamic tags.
  741. Output_data_dynamic*
  742. dynamic_data() const
  743. { return this->dynamic_data_; }
  744. // Write out the output sections.
  745. void
  746. write_output_sections(Output_file* of) const;
  747. // Write out data not associated with an input file or the symbol
  748. // table.
  749. void
  750. write_data(const Symbol_table*, Output_file*) const;
  751. // Write out output sections which can not be written until all the
  752. // input sections are complete.
  753. void
  754. write_sections_after_input_sections(Output_file* of);
  755. // Return an output section named NAME, or NULL if there is none.
  756. Output_section*
  757. find_output_section(const char* name) const;
  758. // Return an output segment of type TYPE, with segment flags SET set
  759. // and segment flags CLEAR clear. Return NULL if there is none.
  760. Output_segment*
  761. find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
  762. elfcpp::Elf_Word clear) const;
  763. // Return the number of segments we expect to produce.
  764. size_t
  765. expected_segment_count() const;
  766. // Set a flag to indicate that an object file uses the static TLS model.
  767. void
  768. set_has_static_tls()
  769. { this->has_static_tls_ = true; }
  770. // Return true if any object file uses the static TLS model.
  771. bool
  772. has_static_tls() const
  773. { return this->has_static_tls_; }
  774. // Return the options which may be set by a linker script.
  775. Script_options*
  776. script_options()
  777. { return this->script_options_; }
  778. const Script_options*
  779. script_options() const
  780. { return this->script_options_; }
  781. // Return the object managing inputs in incremental build. NULL in
  782. // non-incremental builds.
  783. Incremental_inputs*
  784. incremental_inputs() const
  785. { return this->incremental_inputs_; }
  786. // For the target-specific code to add dynamic tags which are common
  787. // to most targets.
  788. void
  789. add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
  790. const Output_data* plt_rel,
  791. const Output_data_reloc_generic* dyn_rel,
  792. bool add_debug, bool dynrel_includes_plt);
  793. // Add a target-specific dynamic tag with constant value.
  794. void
  795. add_target_specific_dynamic_tag(elfcpp::DT tag, unsigned int val);
  796. // Compute and write out the build ID if needed.
  797. void
  798. write_build_id(Output_file*, unsigned char*, size_t) const;
  799. // Rewrite output file in binary format.
  800. void
  801. write_binary(Output_file* in) const;
  802. // Print output sections to the map file.
  803. void
  804. print_to_mapfile(Mapfile*) const;
  805. // Dump statistical information to stderr.
  806. void
  807. print_stats() const;
  808. // A list of segments.
  809. typedef std::vector<Output_segment*> Segment_list;
  810. // A list of sections.
  811. typedef std::vector<Output_section*> Section_list;
  812. // The list of information to write out which is not attached to
  813. // either a section or a segment.
  814. typedef std::vector<Output_data*> Data_list;
  815. // Store the allocated sections into the section list. This is used
  816. // by the linker script code.
  817. void
  818. get_allocated_sections(Section_list*) const;
  819. // Store the executable sections into the section list.
  820. void
  821. get_executable_sections(Section_list*) const;
  822. // Make a section for a linker script to hold data.
  823. Output_section*
  824. make_output_section_for_script(const char* name,
  825. Script_sections::Section_type section_type);
  826. // Make a segment. This is used by the linker script code.
  827. Output_segment*
  828. make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
  829. // Return the number of segments.
  830. size_t
  831. segment_count() const
  832. { return this->segment_list_.size(); }
  833. // Map from section flags to segment flags.
  834. static elfcpp::Elf_Word
  835. section_flags_to_segment(elfcpp::Elf_Xword flags);
  836. // Attach sections to segments.
  837. void
  838. attach_sections_to_segments(const Target*);
  839. // For relaxation clean up, we need to know output section data created
  840. // from a linker script.
  841. void
  842. new_output_section_data_from_script(Output_section_data* posd)
  843. {
  844. if (this->record_output_section_data_from_script_)
  845. this->script_output_section_data_list_.push_back(posd);
  846. }
  847. // Return section list.
  848. const Section_list&
  849. section_list() const
  850. { return this->section_list_; }
  851. // Returns TRUE iff NAME (an input section from RELOBJ) will
  852. // be mapped to an output section that should be KEPT.
  853. bool
  854. keep_input_section(const Relobj*, const char*);
  855. // Add a special output object that will be recreated afresh
  856. // if there is another relaxation iteration.
  857. void
  858. add_relax_output(Output_data* data)
  859. { this->relax_output_list_.push_back(data); }
  860. // Clear out (and free) everything added by add_relax_output.
  861. void
  862. reset_relax_output();
  863. private:
  864. Layout(const Layout&);
  865. Layout& operator=(const Layout&);
  866. // Mapping from input section names to output section names.
  867. struct Section_name_mapping
  868. {
  869. const char* from;
  870. int fromlen;
  871. const char* to;
  872. int tolen;
  873. };
  874. static const Section_name_mapping section_name_mapping[];
  875. static const int section_name_mapping_count;
  876. static const Section_name_mapping text_section_name_mapping[];
  877. static const int text_section_name_mapping_count;
  878. // Find section name NAME in map and return the mapped name if found
  879. // with the length set in PLEN.
  880. static const char* match_section_name(const Section_name_mapping* map,
  881. const int count, const char* name,
  882. size_t* plen);
  883. // During a relocatable link, a list of group sections and
  884. // signatures.
  885. struct Group_signature
  886. {
  887. // The group section.
  888. Output_section* section;
  889. // The signature.
  890. const char* signature;
  891. Group_signature()
  892. : section(NULL), signature(NULL)
  893. { }
  894. Group_signature(Output_section* sectiona, const char* signaturea)
  895. : section(sectiona), signature(signaturea)
  896. { }
  897. };
  898. typedef std::vector<Group_signature> Group_signatures;
  899. // Create a note section, filling in the header.
  900. Output_section*
  901. create_note(const char* name, int note_type, const char* section_name,
  902. size_t descsz, bool allocate, size_t* trailing_padding);
  903. // Create a note section for gnu program properties.
  904. void
  905. create_gnu_properties_note();
  906. // Create a note section for gold version.
  907. void
  908. create_gold_note();
  909. // Record whether the stack must be executable, and a user-supplied size.
  910. void
  911. create_stack_segment();
  912. // Create a build ID note if needed.
  913. void
  914. create_build_id();
  915. // Link .stab and .stabstr sections.
  916. void
  917. link_stabs_sections();
  918. // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
  919. // for the next run of incremental linking to check what has changed.
  920. void
  921. create_incremental_info_sections(Symbol_table*);
  922. // Find the first read-only PT_LOAD segment, creating one if
  923. // necessary.
  924. Output_segment*
  925. find_first_load_seg(const Target*);
  926. // Count the local symbols in the regular symbol table and the dynamic
  927. // symbol table, and build the respective string pools.
  928. void
  929. count_local_symbols(const Task*, const Input_objects*);
  930. // Create the output sections for the symbol table.
  931. void
  932. create_symtab_sections(const Input_objects*, Symbol_table*,
  933. unsigned int, off_t*, unsigned int);
  934. // Create the .shstrtab section.
  935. Output_section*
  936. create_shstrtab();
  937. // Create the section header table.
  938. void
  939. create_shdrs(const Output_section* shstrtab_section, off_t*);
  940. // Create the dynamic symbol table.
  941. void
  942. create_dynamic_symtab(const Input_objects*, Symbol_table*,
  943. Output_section** pdynstr,
  944. unsigned int* plocal_dynamic_count,
  945. unsigned int* pforced_local_dynamic_count,
  946. std::vector<Symbol*>* pdynamic_symbols,
  947. Versions* versions);
  948. // Assign offsets to each local portion of the dynamic symbol table.
  949. void
  950. assign_local_dynsym_offsets(const Input_objects*);
  951. // Finish the .dynamic section and PT_DYNAMIC segment.
  952. void
  953. finish_dynamic_section(const Input_objects*, const Symbol_table*);
  954. // Set the size of the _DYNAMIC symbol.
  955. void
  956. set_dynamic_symbol_size(const Symbol_table*);
  957. // Create the .interp section and PT_INTERP segment.
  958. void
  959. create_interp(const Target* target);
  960. // Create the version sections.
  961. void
  962. create_version_sections(const Versions*,
  963. const Symbol_table*,
  964. unsigned int local_symcount,
  965. const std::vector<Symbol*>& dynamic_symbols,
  966. const Output_section* dynstr);
  967. template<int size, bool big_endian>
  968. void
  969. sized_create_version_sections(const Versions* versions,
  970. const Symbol_table*,
  971. unsigned int local_symcount,
  972. const std::vector<Symbol*>& dynamic_symbols,
  973. const Output_section* dynstr);
  974. // Return whether to include this section in the link.
  975. template<int size, bool big_endian>
  976. bool
  977. include_section(Sized_relobj_file<size, big_endian>* object, const char* name,
  978. const elfcpp::Shdr<size, big_endian>&);
  979. // Return the output section name to use given an input section
  980. // name. Set *PLEN to the length of the name. *PLEN must be
  981. // initialized to the length of NAME.
  982. static const char*
  983. output_section_name(const Relobj*, const char* name, size_t* plen);
  984. // Return the number of allocated output sections.
  985. size_t
  986. allocated_output_section_count() const;
  987. // Return the output section for NAME, TYPE and FLAGS.
  988. Output_section*
  989. get_output_section(const char* name, Stringpool::Key name_key,
  990. elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
  991. Output_section_order order, bool is_relro);
  992. // Clear the input section flags that should not be copied to the
  993. // output section.
  994. elfcpp::Elf_Xword
  995. get_output_section_flags (elfcpp::Elf_Xword input_section_flags);
  996. // Choose the output section for NAME in RELOBJ.
  997. Output_section*
  998. choose_output_section(const Relobj* relobj, const char* name,
  999. elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
  1000. bool is_input_section, Output_section_order order,
  1001. bool is_relro, bool is_reloc, bool match_input_spec);
  1002. // Create a new Output_section.
  1003. Output_section*
  1004. make_output_section(const char* name, elfcpp::Elf_Word type,
  1005. elfcpp::Elf_Xword flags, Output_section_order order,
  1006. bool is_relro);
  1007. // Attach a section to a segment.
  1008. void
  1009. attach_section_to_segment(const Target*, Output_section*);
  1010. // Get section order.
  1011. Output_section_order
  1012. default_section_order(Output_section*, bool is_relro_local);
  1013. // Attach an allocated section to a segment.
  1014. void
  1015. attach_allocated_section_to_segment(const Target*, Output_section*);
  1016. // Make the .eh_frame section.
  1017. Output_section*
  1018. make_eh_frame_section(const Relobj*);
  1019. // Set the final file offsets of all the segments.
  1020. off_t
  1021. set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
  1022. // Set the file offsets of the sections when doing a relocatable
  1023. // link.
  1024. off_t
  1025. set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
  1026. // Set the final file offsets of all the sections not associated
  1027. // with a segment. We set section offsets in three passes: the
  1028. // first handles all allocated sections, the second sections that
  1029. // require postprocessing, and the last the late-bound STRTAB
  1030. // sections (probably only shstrtab, which is the one we care about
  1031. // because it holds section names).
  1032. enum Section_offset_pass
  1033. {
  1034. BEFORE_INPUT_SECTIONS_PASS,
  1035. POSTPROCESSING_SECTIONS_PASS,
  1036. STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
  1037. };
  1038. off_t
  1039. set_section_offsets(off_t, Section_offset_pass pass);
  1040. // Set the final section indexes of all the sections not associated
  1041. // with a segment. Returns the next unused index.
  1042. unsigned int
  1043. set_section_indexes(unsigned int pshndx);
  1044. // Set the section addresses when using a script.
  1045. Output_segment*
  1046. set_section_addresses_from_script(Symbol_table*);
  1047. // Find appropriate places or orphan sections in a script.
  1048. void
  1049. place_orphan_sections_in_script();
  1050. // Return whether SEG1 comes before SEG2 in the output file.
  1051. bool
  1052. segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
  1053. // Use to save and restore segments during relaxation.
  1054. typedef Unordered_map<const Output_segment*, const Output_segment*>
  1055. Segment_states;
  1056. // Save states of current output segments.
  1057. void
  1058. save_segments(Segment_states*);
  1059. // Restore output segment states.
  1060. void
  1061. restore_segments(const Segment_states*);
  1062. // Clean up after relaxation so that it is possible to lay out the
  1063. // sections and segments again.
  1064. void
  1065. clean_up_after_relaxation();
  1066. // Doing preparation work for relaxation. This is factored out to make
  1067. // Layout::finalized a bit smaller and easier to read.
  1068. void
  1069. prepare_for_relaxation();
  1070. // Main body of the relaxation loop, which lays out the section.
  1071. off_t
  1072. relaxation_loop_body(int, Target*, Symbol_table*, Output_segment**,
  1073. Output_segment*, Output_segment_headers*,
  1074. Output_file_header*, unsigned int*);
  1075. // A mapping used for kept comdats/.gnu.linkonce group signatures.
  1076. typedef Unordered_map<std::string, Kept_section> Signatures;
  1077. // Mapping from input section name/type/flags to output section. We
  1078. // use canonicalized strings here.
  1079. typedef std::pair<Stringpool::Key,
  1080. std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
  1081. struct Hash_key
  1082. {
  1083. size_t
  1084. operator()(const Key& k) const;
  1085. };
  1086. typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
  1087. // A comparison class for segments.
  1088. class Compare_segments
  1089. {
  1090. public:
  1091. Compare_segments(Layout* layout)
  1092. : layout_(layout)
  1093. { }
  1094. bool
  1095. operator()(const Output_segment* seg1, const Output_segment* seg2)
  1096. { return this->layout_->segment_precedes(seg1, seg2); }
  1097. private:
  1098. Layout* layout_;
  1099. };
  1100. typedef std::vector<Output_section_data*> Output_section_data_list;
  1101. // Debug checker class.
  1102. class Relaxation_debug_check
  1103. {
  1104. public:
  1105. Relaxation_debug_check()
  1106. : section_infos_()
  1107. { }
  1108. // Check that sections and special data are in reset states.
  1109. void
  1110. check_output_data_for_reset_values(const Layout::Section_list&,
  1111. const Layout::Data_list& special_outputs,
  1112. const Layout::Data_list& relax_outputs);
  1113. // Record information of a section list.
  1114. void
  1115. read_sections(const Layout::Section_list&);
  1116. // Verify a section list with recorded information.
  1117. void
  1118. verify_sections(const Layout::Section_list&);
  1119. private:
  1120. // Information we care about a section.
  1121. struct Section_info
  1122. {
  1123. // Output section described by this.
  1124. Output_section* output_section;
  1125. // Load address.
  1126. uint64_t address;
  1127. // Data size.
  1128. off_t data_size;
  1129. // File offset.
  1130. off_t offset;
  1131. };
  1132. // Section information.
  1133. std::vector<Section_info> section_infos_;
  1134. };
  1135. // Program properties from .note.gnu.property sections.
  1136. struct Gnu_property
  1137. {
  1138. size_t pr_datasz;
  1139. unsigned char* pr_data;
  1140. };
  1141. typedef std::map<unsigned int, Gnu_property> Gnu_properties;
  1142. // The number of input files, for sizing tables.
  1143. int number_of_input_files_;
  1144. // Information set by scripts or by command line options.
  1145. Script_options* script_options_;
  1146. // The output section names.
  1147. Stringpool namepool_;
  1148. // The output symbol names.
  1149. Stringpool sympool_;
  1150. // The dynamic strings, if needed.
  1151. Stringpool dynpool_;
  1152. // The list of group sections and linkonce sections which we have seen.
  1153. Signatures signatures_;
  1154. // The mapping from input section name/type/flags to output sections.
  1155. Section_name_map section_name_map_;
  1156. // The list of output segments.
  1157. Segment_list segment_list_;
  1158. // The list of output sections.
  1159. Section_list section_list_;
  1160. // The list of output sections which are not attached to any output
  1161. // segment.
  1162. Section_list unattached_section_list_;
  1163. // The list of unattached Output_data objects which require special
  1164. // handling because they are not Output_sections.
  1165. Data_list special_output_list_;
  1166. // Like special_output_list_, but cleared and recreated on each
  1167. // iteration of relaxation.
  1168. Data_list relax_output_list_;
  1169. // The section headers.
  1170. Output_section_headers* section_headers_;
  1171. // A pointer to the PT_TLS segment if there is one.
  1172. Output_segment* tls_segment_;
  1173. // A pointer to the PT_GNU_RELRO segment if there is one.
  1174. Output_segment* relro_segment_;
  1175. // A pointer to the PT_INTERP segment if there is one.
  1176. Output_segment* interp_segment_;
  1177. // A backend may increase the size of the PT_GNU_RELRO segment if
  1178. // there is one. This is the amount to increase it by.
  1179. unsigned int increase_relro_;
  1180. // The SHT_SYMTAB output section.
  1181. Output_section* symtab_section_;
  1182. // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
  1183. Output_symtab_xindex* symtab_xindex_;
  1184. // The SHT_DYNSYM output section if there is one.
  1185. Output_section* dynsym_section_;
  1186. // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
  1187. Output_symtab_xindex* dynsym_xindex_;
  1188. // The SHT_DYNAMIC output section if there is one.
  1189. Output_section* dynamic_section_;
  1190. // The _DYNAMIC symbol if there is one.
  1191. Symbol* dynamic_symbol_;
  1192. // The dynamic data which goes into dynamic_section_.
  1193. Output_data_dynamic* dynamic_data_;
  1194. // The exception frame output section if there is one.
  1195. Output_section* eh_frame_section_;
  1196. // The exception frame data for eh_frame_section_.
  1197. Eh_frame* eh_frame_data_;
  1198. // Whether we have added eh_frame_data_ to the .eh_frame section.
  1199. bool added_eh_frame_data_;
  1200. // The exception frame header output section if there is one.
  1201. Output_section* eh_frame_hdr_section_;
  1202. // The data for the .gdb_index section.
  1203. Gdb_index* gdb_index_data_;
  1204. // The space for the build ID checksum if there is one.
  1205. Output_section_data* build_id_note_;
  1206. // The output section containing dwarf abbreviations
  1207. Output_reduced_debug_abbrev_section* debug_abbrev_;
  1208. // The output section containing the dwarf debug info tree
  1209. Output_reduced_debug_info_section* debug_info_;
  1210. // A list of group sections and their signatures.
  1211. Group_signatures group_signatures_;
  1212. // The size of the output file.
  1213. off_t output_file_size_;
  1214. // Whether we have added an input section to an output section.
  1215. bool have_added_input_section_;
  1216. // Whether we have attached the sections to the segments.
  1217. bool sections_are_attached_;
  1218. // Whether we have seen an object file marked to require an
  1219. // executable stack.
  1220. bool input_requires_executable_stack_;
  1221. // Whether we have seen at least one object file with an executable
  1222. // stack marker.
  1223. bool input_with_gnu_stack_note_;
  1224. // Whether we have seen at least one object file without an
  1225. // executable stack marker.
  1226. bool input_without_gnu_stack_note_;
  1227. // Whether we have seen an object file that uses the static TLS model.
  1228. bool has_static_tls_;
  1229. // Whether any sections require postprocessing.
  1230. bool any_postprocessing_sections_;
  1231. // Whether we have resized the signatures_ hash table.
  1232. bool resized_signatures_;
  1233. // Whether we have created a .stab*str output section.
  1234. bool have_stabstr_section_;
  1235. // True if the input sections in the output sections should be sorted
  1236. // as specified in a section ordering file.
  1237. bool section_ordering_specified_;
  1238. // True if some input sections need to be mapped to a unique segment,
  1239. // after being mapped to a unique Output_section.
  1240. bool unique_segment_for_sections_specified_;
  1241. // In incremental build, holds information check the inputs and build the
  1242. // .gnu_incremental_inputs section.
  1243. Incremental_inputs* incremental_inputs_;
  1244. // Whether we record output section data created in script
  1245. bool record_output_section_data_from_script_;
  1246. // Set if this is a slim LTO object not loaded with a compiler plugin
  1247. bool lto_slim_object_;
  1248. // List of output data that needs to be removed at relaxation clean up.
  1249. Output_section_data_list script_output_section_data_list_;
  1250. // Structure to save segment states before entering the relaxation loop.
  1251. Segment_states* segment_states_;
  1252. // A relaxation debug checker. We only create one when in debugging mode.
  1253. Relaxation_debug_check* relaxation_debug_check_;
  1254. // Plugins specify section_ordering using this map. This is set in
  1255. // update_section_order in plugin.cc
  1256. std::map<Section_id, unsigned int> section_order_map_;
  1257. // This maps an input section to a unique segment. This is done by first
  1258. // placing such input sections in unique output sections and then mapping
  1259. // the output section to a unique segment. Unique_segment_info stores
  1260. // any additional flags and alignment of the new segment.
  1261. Section_segment_map section_segment_map_;
  1262. // Hash a pattern to its position in the section ordering file.
  1263. Unordered_map<std::string, unsigned int> input_section_position_;
  1264. // Vector of glob only patterns in the section_ordering file.
  1265. std::vector<std::string> input_section_glob_;
  1266. // For incremental links, the base file to be modified.
  1267. Incremental_binary* incremental_base_;
  1268. // For incremental links, a list of free space within the file.
  1269. Free_list free_list_;
  1270. // Program properties.
  1271. Gnu_properties gnu_properties_;
  1272. };
  1273. // This task handles writing out data in output sections which is not
  1274. // part of an input section, or which requires special handling. When
  1275. // this is done, it unblocks both output_sections_blocker and
  1276. // final_blocker.
  1277. class Write_sections_task : public Task
  1278. {
  1279. public:
  1280. Write_sections_task(const Layout* layout, Output_file* of,
  1281. Task_token* output_sections_blocker,
  1282. Task_token* input_sections_blocker,
  1283. Task_token* final_blocker)
  1284. : layout_(layout), of_(of),
  1285. output_sections_blocker_(output_sections_blocker),
  1286. input_sections_blocker_(input_sections_blocker),
  1287. final_blocker_(final_blocker)
  1288. { }
  1289. // The standard Task methods.
  1290. Task_token*
  1291. is_runnable();
  1292. void
  1293. locks(Task_locker*);
  1294. void
  1295. run(Workqueue*);
  1296. std::string
  1297. get_name() const
  1298. { return "Write_sections_task"; }
  1299. private:
  1300. class Write_sections_locker;
  1301. const Layout* layout_;
  1302. Output_file* of_;
  1303. Task_token* output_sections_blocker_;
  1304. Task_token* input_sections_blocker_;
  1305. Task_token* final_blocker_;
  1306. };
  1307. // This task handles writing out data which is not part of a section
  1308. // or segment.
  1309. class Write_data_task : public Task
  1310. {
  1311. public:
  1312. Write_data_task(const Layout* layout, const Symbol_table* symtab,
  1313. Output_file* of, Task_token* final_blocker)
  1314. : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
  1315. { }
  1316. // The standard Task methods.
  1317. Task_token*
  1318. is_runnable();
  1319. void
  1320. locks(Task_locker*);
  1321. void
  1322. run(Workqueue*);
  1323. std::string
  1324. get_name() const
  1325. { return "Write_data_task"; }
  1326. private:
  1327. const Layout* layout_;
  1328. const Symbol_table* symtab_;
  1329. Output_file* of_;
  1330. Task_token* final_blocker_;
  1331. };
  1332. // This task handles writing out the global symbols.
  1333. class Write_symbols_task : public Task
  1334. {
  1335. public:
  1336. Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
  1337. const Input_objects* /*input_objects*/,
  1338. const Stringpool* sympool, const Stringpool* dynpool,
  1339. Output_file* of, Task_token* final_blocker)
  1340. : layout_(layout), symtab_(symtab),
  1341. sympool_(sympool), dynpool_(dynpool), of_(of),
  1342. final_blocker_(final_blocker)
  1343. { }
  1344. // The standard Task methods.
  1345. Task_token*
  1346. is_runnable();
  1347. void
  1348. locks(Task_locker*);
  1349. void
  1350. run(Workqueue*);
  1351. std::string
  1352. get_name() const
  1353. { return "Write_symbols_task"; }
  1354. private:
  1355. const Layout* layout_;
  1356. const Symbol_table* symtab_;
  1357. const Stringpool* sympool_;
  1358. const Stringpool* dynpool_;
  1359. Output_file* of_;
  1360. Task_token* final_blocker_;
  1361. };
  1362. // This task handles writing out data in output sections which can't
  1363. // be written out until all the input sections have been handled.
  1364. // This is for sections whose contents is based on the contents of
  1365. // other output sections.
  1366. class Write_after_input_sections_task : public Task
  1367. {
  1368. public:
  1369. Write_after_input_sections_task(Layout* layout, Output_file* of,
  1370. Task_token* input_sections_blocker,
  1371. Task_token* final_blocker)
  1372. : layout_(layout), of_(of),
  1373. input_sections_blocker_(input_sections_blocker),
  1374. final_blocker_(final_blocker)
  1375. { }
  1376. // The standard Task methods.
  1377. Task_token*
  1378. is_runnable();
  1379. void
  1380. locks(Task_locker*);
  1381. void
  1382. run(Workqueue*);
  1383. std::string
  1384. get_name() const
  1385. { return "Write_after_input_sections_task"; }
  1386. private:
  1387. Layout* layout_;
  1388. Output_file* of_;
  1389. Task_token* input_sections_blocker_;
  1390. Task_token* final_blocker_;
  1391. };
  1392. // This task function handles computation of the build id.
  1393. // When using --build-id=tree, it schedules the tasks that
  1394. // compute the hashes for each chunk of the file. This task
  1395. // cannot run until we have finalized the size of the output
  1396. // file, after the completion of Write_after_input_sections_task.
  1397. class Build_id_task_runner : public Task_function_runner
  1398. {
  1399. public:
  1400. Build_id_task_runner(const General_options* options, const Layout* layout,
  1401. Output_file* of)
  1402. : options_(options), layout_(layout), of_(of)
  1403. { }
  1404. // Run the operation.
  1405. void
  1406. run(Workqueue*, const Task*);
  1407. private:
  1408. const General_options* options_;
  1409. const Layout* layout_;
  1410. Output_file* of_;
  1411. };
  1412. // This task function handles closing the file.
  1413. class Close_task_runner : public Task_function_runner
  1414. {
  1415. public:
  1416. Close_task_runner(const General_options* options, const Layout* layout,
  1417. Output_file* of, unsigned char* array_of_hashes,
  1418. size_t size_of_hashes)
  1419. : options_(options), layout_(layout), of_(of),
  1420. array_of_hashes_(array_of_hashes), size_of_hashes_(size_of_hashes)
  1421. { }
  1422. // Run the operation.
  1423. void
  1424. run(Workqueue*, const Task*);
  1425. private:
  1426. const General_options* options_;
  1427. const Layout* layout_;
  1428. Output_file* of_;
  1429. unsigned char* const array_of_hashes_;
  1430. const size_t size_of_hashes_;
  1431. };
  1432. // A small helper function to align an address.
  1433. inline uint64_t
  1434. align_address(uint64_t address, uint64_t addralign)
  1435. {
  1436. if (addralign != 0)
  1437. address = (address + addralign - 1) &~ (addralign - 1);
  1438. return address;
  1439. }
  1440. } // End namespace gold.
  1441. #endif // !defined(GOLD_LAYOUT_H)