layout.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. // layout.h -- lay out output file sections for gold -*- C++ -*-
  2. // Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_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. // Loadable read-only note sections come next so that the PT_NOTE
  342. // segment is on the first page of the executable.
  343. ORDER_RO_NOTE,
  344. // Put read-only sections used by the dynamic linker early in the
  345. // executable to minimize paging.
  346. ORDER_DYNAMIC_LINKER,
  347. // Put reloc sections used by the dynamic linker after other
  348. // sections used by the dynamic linker; otherwise, objcopy and strip
  349. // get confused.
  350. ORDER_DYNAMIC_RELOCS,
  351. // Put the PLT reloc section after the other dynamic relocs;
  352. // otherwise, prelink gets confused.
  353. ORDER_DYNAMIC_PLT_RELOCS,
  354. // The .init section.
  355. ORDER_INIT,
  356. // The PLT.
  357. ORDER_PLT,
  358. // The regular text sections.
  359. ORDER_TEXT,
  360. // The .fini section.
  361. ORDER_FINI,
  362. // The read-only sections.
  363. ORDER_READONLY,
  364. // The exception frame sections.
  365. ORDER_EHFRAME,
  366. // The TLS sections come first in the data section.
  367. ORDER_TLS_DATA,
  368. ORDER_TLS_BSS,
  369. // Local RELRO (read-only after relocation) sections come before
  370. // non-local RELRO sections. This data will be fully resolved by
  371. // the prelinker.
  372. ORDER_RELRO_LOCAL,
  373. // Non-local RELRO sections are grouped together after local RELRO
  374. // sections. All RELRO sections must be adjacent so that they can
  375. // all be put into a PT_GNU_RELRO segment.
  376. ORDER_RELRO,
  377. // We permit marking exactly one output section as the last RELRO
  378. // section. We do this so that the read-only GOT can be adjacent to
  379. // the writable GOT.
  380. ORDER_RELRO_LAST,
  381. // Similarly, we permit marking exactly one output section as the
  382. // first non-RELRO section.
  383. ORDER_NON_RELRO_FIRST,
  384. // The regular data sections come after the RELRO sections.
  385. ORDER_DATA,
  386. // Large data sections normally go in large data segments.
  387. ORDER_LARGE_DATA,
  388. // Group writable notes so that we can have a single PT_NOTE
  389. // segment.
  390. ORDER_RW_NOTE,
  391. // The small data sections must be at the end of the data sections,
  392. // so that they can be adjacent to the small BSS sections.
  393. ORDER_SMALL_DATA,
  394. // The BSS sections start here.
  395. // The small BSS sections must be at the start of the BSS sections,
  396. // so that they can be adjacent to the small data sections.
  397. ORDER_SMALL_BSS,
  398. // The regular BSS sections.
  399. ORDER_BSS,
  400. // The large BSS sections come after the other BSS sections.
  401. ORDER_LARGE_BSS,
  402. // Maximum value.
  403. ORDER_MAX
  404. };
  405. // This class handles the details of laying out input sections.
  406. class Layout
  407. {
  408. public:
  409. Layout(int number_of_input_files, Script_options*);
  410. ~Layout()
  411. {
  412. delete this->relaxation_debug_check_;
  413. delete this->segment_states_;
  414. }
  415. // For incremental links, record the base file to be modified.
  416. void
  417. set_incremental_base(Incremental_binary* base);
  418. Incremental_binary*
  419. incremental_base()
  420. { return this->incremental_base_; }
  421. // For incremental links, record the initial fixed layout of a section
  422. // from the base file, and return a pointer to the Output_section.
  423. template<int size, bool big_endian>
  424. Output_section*
  425. init_fixed_output_section(const char*, elfcpp::Shdr<size, big_endian>&);
  426. // Given an input section SHNDX, named NAME, with data in SHDR, from
  427. // the object file OBJECT, return the output section where this
  428. // input section should go. RELOC_SHNDX is the index of a
  429. // relocation section which applies to this section, or 0 if none,
  430. // or -1U if more than one. RELOC_TYPE is the type of the
  431. // relocation section if there is one. Set *OFFSET to the offset
  432. // within the output section.
  433. template<int size, bool big_endian>
  434. Output_section*
  435. layout(Sized_relobj_file<size, big_endian> *object, unsigned int shndx,
  436. const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
  437. unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
  438. std::map<Section_id, unsigned int>*
  439. get_section_order_map()
  440. { return &this->section_order_map_; }
  441. // Struct to store segment info when mapping some input sections to
  442. // unique segments using linker plugins. Mapping an input section to
  443. // a unique segment is done by first placing such input sections in
  444. // unique output sections and then mapping the output section to a
  445. // unique segment. NAME is the name of the output section. FLAGS
  446. // and ALIGN are the extra flags and alignment of the segment.
  447. struct Unique_segment_info
  448. {
  449. // Identifier for the segment. ELF segments dont have names. This
  450. // is used as the name of the output section mapped to the segment.
  451. const char* name;
  452. // Additional segment flags.
  453. uint64_t flags;
  454. // Segment alignment.
  455. uint64_t align;
  456. };
  457. // Mapping from input section to segment.
  458. typedef std::map<Const_section_id, Unique_segment_info*>
  459. Section_segment_map;
  460. // Maps section SECN to SEGMENT s.
  461. void
  462. insert_section_segment_map(Const_section_id secn, Unique_segment_info *s);
  463. // Some input sections require special ordering, for compatibility
  464. // with GNU ld. Given the name of an input section, return -1 if it
  465. // does not require special ordering. Otherwise, return the index
  466. // by which it should be ordered compared to other input sections
  467. // that require special ordering.
  468. static int
  469. special_ordering_of_input_section(const char* name);
  470. bool
  471. is_section_ordering_specified()
  472. { return this->section_ordering_specified_; }
  473. void
  474. set_section_ordering_specified()
  475. { this->section_ordering_specified_ = true; }
  476. bool
  477. is_unique_segment_for_sections_specified() const
  478. { return this->unique_segment_for_sections_specified_; }
  479. void
  480. set_unique_segment_for_sections_specified()
  481. { this->unique_segment_for_sections_specified_ = true; }
  482. // For incremental updates, allocate a block of memory from the
  483. // free list. Find a block starting at or after MINOFF.
  484. off_t
  485. allocate(off_t len, uint64_t align, off_t minoff)
  486. { return this->free_list_.allocate(len, align, minoff); }
  487. unsigned int
  488. find_section_order_index(const std::string&);
  489. // Read the sequence of input sections from the file specified with
  490. // linker option --section-ordering-file.
  491. void
  492. read_layout_from_file();
  493. // Layout an input reloc section when doing a relocatable link. The
  494. // section is RELOC_SHNDX in OBJECT, with data in SHDR.
  495. // DATA_SECTION is the reloc section to which it refers. RR is the
  496. // relocatable information.
  497. template<int size, bool big_endian>
  498. Output_section*
  499. layout_reloc(Sized_relobj_file<size, big_endian>* object,
  500. unsigned int reloc_shndx,
  501. const elfcpp::Shdr<size, big_endian>& shdr,
  502. Output_section* data_section,
  503. Relocatable_relocs* rr);
  504. // Layout a group section when doing a relocatable link.
  505. template<int size, bool big_endian>
  506. void
  507. layout_group(Symbol_table* symtab,
  508. Sized_relobj_file<size, big_endian>* object,
  509. unsigned int group_shndx,
  510. const char* group_section_name,
  511. const char* signature,
  512. const elfcpp::Shdr<size, big_endian>& shdr,
  513. elfcpp::Elf_Word flags,
  514. std::vector<unsigned int>* shndxes);
  515. // Like layout, only for exception frame sections. OBJECT is an
  516. // object file. SYMBOLS is the contents of the symbol table
  517. // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
  518. // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
  519. // .eh_frame section in OBJECT. SHDR is the section header.
  520. // RELOC_SHNDX is the index of a relocation section which applies to
  521. // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
  522. // is the type of the relocation section if there is one. This
  523. // returns the output section, and sets *OFFSET to the offset.
  524. template<int size, bool big_endian>
  525. Output_section*
  526. layout_eh_frame(Sized_relobj_file<size, big_endian>* object,
  527. const unsigned char* symbols,
  528. off_t symbols_size,
  529. const unsigned char* symbol_names,
  530. off_t symbol_names_size,
  531. unsigned int shndx,
  532. const elfcpp::Shdr<size, big_endian>& shdr,
  533. unsigned int reloc_shndx, unsigned int reloc_type,
  534. off_t* offset);
  535. // After processing all input files, we call this to make sure that
  536. // the optimized .eh_frame sections have been added to the output
  537. // section.
  538. void
  539. finalize_eh_frame_section();
  540. // Add .eh_frame information for a PLT. The FDE must start with a
  541. // 4-byte PC-relative reference to the start of the PLT, followed by
  542. // a 4-byte size of PLT.
  543. void
  544. add_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
  545. size_t cie_length, const unsigned char* fde_data,
  546. size_t fde_length);
  547. // Scan a .debug_info or .debug_types section, and add summary
  548. // information to the .gdb_index section.
  549. template<int size, bool big_endian>
  550. void
  551. add_to_gdb_index(bool is_type_unit,
  552. Sized_relobj<size, big_endian>* object,
  553. const unsigned char* symbols,
  554. off_t symbols_size,
  555. unsigned int shndx,
  556. unsigned int reloc_shndx,
  557. unsigned int reloc_type);
  558. // Handle a GNU stack note. This is called once per input object
  559. // file. SEEN_GNU_STACK is true if the object file has a
  560. // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
  561. // from that section if there was one.
  562. void
  563. layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
  564. const Object*);
  565. // Add an Output_section_data to the layout. This is used for
  566. // special sections like the GOT section. ORDER is where the
  567. // section should wind up in the output segment. IS_RELRO is true
  568. // for relro sections.
  569. Output_section*
  570. add_output_section_data(const char* name, elfcpp::Elf_Word type,
  571. elfcpp::Elf_Xword flags,
  572. Output_section_data*, Output_section_order order,
  573. bool is_relro);
  574. // Increase the size of the relro segment by this much.
  575. void
  576. increase_relro(unsigned int s)
  577. { this->increase_relro_ += s; }
  578. // Create dynamic sections if necessary.
  579. void
  580. create_initial_dynamic_sections(Symbol_table*);
  581. // Define __start and __stop symbols for output sections.
  582. void
  583. define_section_symbols(Symbol_table*);
  584. // Create automatic note sections.
  585. void
  586. create_notes();
  587. // Create sections for linker scripts.
  588. void
  589. create_script_sections()
  590. { this->script_options_->create_script_sections(this); }
  591. // Define symbols from any linker script.
  592. void
  593. define_script_symbols(Symbol_table* symtab)
  594. { this->script_options_->add_symbols_to_table(symtab); }
  595. // Define symbols for group signatures.
  596. void
  597. define_group_signatures(Symbol_table*);
  598. // Return the Stringpool used for symbol names.
  599. const Stringpool*
  600. sympool() const
  601. { return &this->sympool_; }
  602. // Return the Stringpool used for dynamic symbol names and dynamic
  603. // tags.
  604. const Stringpool*
  605. dynpool() const
  606. { return &this->dynpool_; }
  607. // Return the .dynamic output section. This is only valid after the
  608. // layout has been finalized.
  609. Output_section*
  610. dynamic_section() const
  611. { return this->dynamic_section_; }
  612. // Return the symtab_xindex section used to hold large section
  613. // indexes for the normal symbol table.
  614. Output_symtab_xindex*
  615. symtab_xindex() const
  616. { return this->symtab_xindex_; }
  617. // Return the dynsym_xindex section used to hold large section
  618. // indexes for the dynamic symbol table.
  619. Output_symtab_xindex*
  620. dynsym_xindex() const
  621. { return this->dynsym_xindex_; }
  622. // Return whether a section is a .gnu.linkonce section, given the
  623. // section name.
  624. static inline bool
  625. is_linkonce(const char* name)
  626. { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
  627. // Whether we have added an input section.
  628. bool
  629. have_added_input_section() const
  630. { return this->have_added_input_section_; }
  631. // Return true if a section is a debugging section.
  632. static inline bool
  633. is_debug_info_section(const char* name)
  634. {
  635. // Debugging sections can only be recognized by name.
  636. return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
  637. || strncmp(name, ".zdebug", sizeof(".zdebug") - 1) == 0
  638. || strncmp(name, ".gnu.linkonce.wi.",
  639. sizeof(".gnu.linkonce.wi.") - 1) == 0
  640. || strncmp(name, ".line", sizeof(".line") - 1) == 0
  641. || strncmp(name, ".stab", sizeof(".stab") - 1) == 0);
  642. }
  643. // Return true if RELOBJ is an input file whose base name matches
  644. // FILE_NAME. The base name must have an extension of ".o", and
  645. // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
  646. static bool
  647. match_file_name(const Relobj* relobj, const char* file_name);
  648. // Return whether section SHNDX in RELOBJ is a .ctors/.dtors section
  649. // with more than one word being mapped to a .init_array/.fini_array
  650. // section.
  651. bool
  652. is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const;
  653. // Check if a comdat group or .gnu.linkonce section with the given
  654. // NAME is selected for the link. If there is already a section,
  655. // *KEPT_SECTION is set to point to the signature and the function
  656. // returns false. Otherwise, OBJECT, SHNDX,IS_COMDAT, and
  657. // IS_GROUP_NAME are recorded for this NAME in the layout object,
  658. // *KEPT_SECTION is set to the internal copy and the function return
  659. // false.
  660. bool
  661. find_or_add_kept_section(const std::string& name, Relobj* object,
  662. unsigned int shndx, bool is_comdat,
  663. bool is_group_name, Kept_section** kept_section);
  664. // Finalize the layout after all the input sections have been added.
  665. off_t
  666. finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
  667. // Return whether any sections require postprocessing.
  668. bool
  669. any_postprocessing_sections() const
  670. { return this->any_postprocessing_sections_; }
  671. // Return the size of the output file.
  672. off_t
  673. output_file_size() const
  674. { return this->output_file_size_; }
  675. // Return the TLS segment. This will return NULL if there isn't
  676. // one.
  677. Output_segment*
  678. tls_segment() const
  679. { return this->tls_segment_; }
  680. // Return the normal symbol table.
  681. Output_section*
  682. symtab_section() const
  683. {
  684. gold_assert(this->symtab_section_ != NULL);
  685. return this->symtab_section_;
  686. }
  687. // Return the file offset of the normal symbol table.
  688. off_t
  689. symtab_section_offset() const;
  690. // Return the section index of the normal symbol tabl.e
  691. unsigned int
  692. symtab_section_shndx() const;
  693. // Return the dynamic symbol table.
  694. Output_section*
  695. dynsym_section() const
  696. {
  697. gold_assert(this->dynsym_section_ != NULL);
  698. return this->dynsym_section_;
  699. }
  700. // Return the dynamic tags.
  701. Output_data_dynamic*
  702. dynamic_data() const
  703. { return this->dynamic_data_; }
  704. // Write out the output sections.
  705. void
  706. write_output_sections(Output_file* of) const;
  707. // Write out data not associated with an input file or the symbol
  708. // table.
  709. void
  710. write_data(const Symbol_table*, Output_file*) const;
  711. // Write out output sections which can not be written until all the
  712. // input sections are complete.
  713. void
  714. write_sections_after_input_sections(Output_file* of);
  715. // Return an output section named NAME, or NULL if there is none.
  716. Output_section*
  717. find_output_section(const char* name) const;
  718. // Return an output segment of type TYPE, with segment flags SET set
  719. // and segment flags CLEAR clear. Return NULL if there is none.
  720. Output_segment*
  721. find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
  722. elfcpp::Elf_Word clear) const;
  723. // Return the number of segments we expect to produce.
  724. size_t
  725. expected_segment_count() const;
  726. // Set a flag to indicate that an object file uses the static TLS model.
  727. void
  728. set_has_static_tls()
  729. { this->has_static_tls_ = true; }
  730. // Return true if any object file uses the static TLS model.
  731. bool
  732. has_static_tls() const
  733. { return this->has_static_tls_; }
  734. // Return the options which may be set by a linker script.
  735. Script_options*
  736. script_options()
  737. { return this->script_options_; }
  738. const Script_options*
  739. script_options() const
  740. { return this->script_options_; }
  741. // Return the object managing inputs in incremental build. NULL in
  742. // non-incremental builds.
  743. Incremental_inputs*
  744. incremental_inputs() const
  745. { return this->incremental_inputs_; }
  746. // For the target-specific code to add dynamic tags which are common
  747. // to most targets.
  748. void
  749. add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
  750. const Output_data* plt_rel,
  751. const Output_data_reloc_generic* dyn_rel,
  752. bool add_debug, bool dynrel_includes_plt);
  753. // Compute and write out the build ID if needed.
  754. void
  755. write_build_id(Output_file*, unsigned char*, size_t) const;
  756. // Rewrite output file in binary format.
  757. void
  758. write_binary(Output_file* in) const;
  759. // Print output sections to the map file.
  760. void
  761. print_to_mapfile(Mapfile*) const;
  762. // Dump statistical information to stderr.
  763. void
  764. print_stats() const;
  765. // A list of segments.
  766. typedef std::vector<Output_segment*> Segment_list;
  767. // A list of sections.
  768. typedef std::vector<Output_section*> Section_list;
  769. // The list of information to write out which is not attached to
  770. // either a section or a segment.
  771. typedef std::vector<Output_data*> Data_list;
  772. // Store the allocated sections into the section list. This is used
  773. // by the linker script code.
  774. void
  775. get_allocated_sections(Section_list*) const;
  776. // Store the executable sections into the section list.
  777. void
  778. get_executable_sections(Section_list*) const;
  779. // Make a section for a linker script to hold data.
  780. Output_section*
  781. make_output_section_for_script(const char* name,
  782. Script_sections::Section_type section_type);
  783. // Make a segment. This is used by the linker script code.
  784. Output_segment*
  785. make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
  786. // Return the number of segments.
  787. size_t
  788. segment_count() const
  789. { return this->segment_list_.size(); }
  790. // Map from section flags to segment flags.
  791. static elfcpp::Elf_Word
  792. section_flags_to_segment(elfcpp::Elf_Xword flags);
  793. // Attach sections to segments.
  794. void
  795. attach_sections_to_segments(const Target*);
  796. // For relaxation clean up, we need to know output section data created
  797. // from a linker script.
  798. void
  799. new_output_section_data_from_script(Output_section_data* posd)
  800. {
  801. if (this->record_output_section_data_from_script_)
  802. this->script_output_section_data_list_.push_back(posd);
  803. }
  804. // Return section list.
  805. const Section_list&
  806. section_list() const
  807. { return this->section_list_; }
  808. // Returns TRUE iff NAME (an input section from RELOBJ) will
  809. // be mapped to an output section that should be KEPT.
  810. bool
  811. keep_input_section(const Relobj*, const char*);
  812. // Add a special output object that will be recreated afresh
  813. // if there is another relaxation iteration.
  814. void
  815. add_relax_output(Output_data* data)
  816. { this->relax_output_list_.push_back(data); }
  817. // Clear out (and free) everything added by add_relax_output.
  818. void
  819. reset_relax_output();
  820. private:
  821. Layout(const Layout&);
  822. Layout& operator=(const Layout&);
  823. // Mapping from input section names to output section names.
  824. struct Section_name_mapping
  825. {
  826. const char* from;
  827. int fromlen;
  828. const char* to;
  829. int tolen;
  830. };
  831. static const Section_name_mapping section_name_mapping[];
  832. static const int section_name_mapping_count;
  833. // During a relocatable link, a list of group sections and
  834. // signatures.
  835. struct Group_signature
  836. {
  837. // The group section.
  838. Output_section* section;
  839. // The signature.
  840. const char* signature;
  841. Group_signature()
  842. : section(NULL), signature(NULL)
  843. { }
  844. Group_signature(Output_section* sectiona, const char* signaturea)
  845. : section(sectiona), signature(signaturea)
  846. { }
  847. };
  848. typedef std::vector<Group_signature> Group_signatures;
  849. // Create a note section, filling in the header.
  850. Output_section*
  851. create_note(const char* name, int note_type, const char* section_name,
  852. size_t descsz, bool allocate, size_t* trailing_padding);
  853. // Create a note section for gold version.
  854. void
  855. create_gold_note();
  856. // Record whether the stack must be executable.
  857. void
  858. create_executable_stack_info();
  859. // Create a build ID note if needed.
  860. void
  861. create_build_id();
  862. // Link .stab and .stabstr sections.
  863. void
  864. link_stabs_sections();
  865. // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
  866. // for the next run of incremental linking to check what has changed.
  867. void
  868. create_incremental_info_sections(Symbol_table*);
  869. // Find the first read-only PT_LOAD segment, creating one if
  870. // necessary.
  871. Output_segment*
  872. find_first_load_seg(const Target*);
  873. // Count the local symbols in the regular symbol table and the dynamic
  874. // symbol table, and build the respective string pools.
  875. void
  876. count_local_symbols(const Task*, const Input_objects*);
  877. // Create the output sections for the symbol table.
  878. void
  879. create_symtab_sections(const Input_objects*, Symbol_table*,
  880. unsigned int, off_t*);
  881. // Create the .shstrtab section.
  882. Output_section*
  883. create_shstrtab();
  884. // Create the section header table.
  885. void
  886. create_shdrs(const Output_section* shstrtab_section, off_t*);
  887. // Create the dynamic symbol table.
  888. void
  889. create_dynamic_symtab(const Input_objects*, Symbol_table*,
  890. Output_section** pdynstr,
  891. unsigned int* plocal_dynamic_count,
  892. std::vector<Symbol*>* pdynamic_symbols,
  893. Versions* versions);
  894. // Assign offsets to each local portion of the dynamic symbol table.
  895. void
  896. assign_local_dynsym_offsets(const Input_objects*);
  897. // Finish the .dynamic section and PT_DYNAMIC segment.
  898. void
  899. finish_dynamic_section(const Input_objects*, const Symbol_table*);
  900. // Set the size of the _DYNAMIC symbol.
  901. void
  902. set_dynamic_symbol_size(const Symbol_table*);
  903. // Create the .interp section and PT_INTERP segment.
  904. void
  905. create_interp(const Target* target);
  906. // Create the version sections.
  907. void
  908. create_version_sections(const Versions*,
  909. const Symbol_table*,
  910. unsigned int local_symcount,
  911. const std::vector<Symbol*>& dynamic_symbols,
  912. const Output_section* dynstr);
  913. template<int size, bool big_endian>
  914. void
  915. sized_create_version_sections(const Versions* versions,
  916. const Symbol_table*,
  917. unsigned int local_symcount,
  918. const std::vector<Symbol*>& dynamic_symbols,
  919. const Output_section* dynstr);
  920. // Return whether to include this section in the link.
  921. template<int size, bool big_endian>
  922. bool
  923. include_section(Sized_relobj_file<size, big_endian>* object, const char* name,
  924. const elfcpp::Shdr<size, big_endian>&);
  925. // Return the output section name to use given an input section
  926. // name. Set *PLEN to the length of the name. *PLEN must be
  927. // initialized to the length of NAME.
  928. static const char*
  929. output_section_name(const Relobj*, const char* name, size_t* plen);
  930. // Return the number of allocated output sections.
  931. size_t
  932. allocated_output_section_count() const;
  933. // Return the output section for NAME, TYPE and FLAGS.
  934. Output_section*
  935. get_output_section(const char* name, Stringpool::Key name_key,
  936. elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
  937. Output_section_order order, bool is_relro);
  938. // Clear the input section flags that should not be copied to the
  939. // output section.
  940. elfcpp::Elf_Xword
  941. get_output_section_flags (elfcpp::Elf_Xword input_section_flags);
  942. // Choose the output section for NAME in RELOBJ.
  943. Output_section*
  944. choose_output_section(const Relobj* relobj, const char* name,
  945. elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
  946. bool is_input_section, Output_section_order order,
  947. bool is_relro);
  948. // Create a new Output_section.
  949. Output_section*
  950. make_output_section(const char* name, elfcpp::Elf_Word type,
  951. elfcpp::Elf_Xword flags, Output_section_order order,
  952. bool is_relro);
  953. // Attach a section to a segment.
  954. void
  955. attach_section_to_segment(const Target*, Output_section*);
  956. // Get section order.
  957. Output_section_order
  958. default_section_order(Output_section*, bool is_relro_local);
  959. // Attach an allocated section to a segment.
  960. void
  961. attach_allocated_section_to_segment(const Target*, Output_section*);
  962. // Make the .eh_frame section.
  963. Output_section*
  964. make_eh_frame_section(const Relobj*);
  965. // Set the final file offsets of all the segments.
  966. off_t
  967. set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
  968. // Set the file offsets of the sections when doing a relocatable
  969. // link.
  970. off_t
  971. set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
  972. // Set the final file offsets of all the sections not associated
  973. // with a segment. We set section offsets in three passes: the
  974. // first handles all allocated sections, the second sections that
  975. // require postprocessing, and the last the late-bound STRTAB
  976. // sections (probably only shstrtab, which is the one we care about
  977. // because it holds section names).
  978. enum Section_offset_pass
  979. {
  980. BEFORE_INPUT_SECTIONS_PASS,
  981. POSTPROCESSING_SECTIONS_PASS,
  982. STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
  983. };
  984. off_t
  985. set_section_offsets(off_t, Section_offset_pass pass);
  986. // Set the final section indexes of all the sections not associated
  987. // with a segment. Returns the next unused index.
  988. unsigned int
  989. set_section_indexes(unsigned int pshndx);
  990. // Set the section addresses when using a script.
  991. Output_segment*
  992. set_section_addresses_from_script(Symbol_table*);
  993. // Find appropriate places or orphan sections in a script.
  994. void
  995. place_orphan_sections_in_script();
  996. // Return whether SEG1 comes before SEG2 in the output file.
  997. bool
  998. segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
  999. // Use to save and restore segments during relaxation.
  1000. typedef Unordered_map<const Output_segment*, const Output_segment*>
  1001. Segment_states;
  1002. // Save states of current output segments.
  1003. void
  1004. save_segments(Segment_states*);
  1005. // Restore output segment states.
  1006. void
  1007. restore_segments(const Segment_states*);
  1008. // Clean up after relaxation so that it is possible to lay out the
  1009. // sections and segments again.
  1010. void
  1011. clean_up_after_relaxation();
  1012. // Doing preparation work for relaxation. This is factored out to make
  1013. // Layout::finalized a bit smaller and easier to read.
  1014. void
  1015. prepare_for_relaxation();
  1016. // Main body of the relaxation loop, which lays out the section.
  1017. off_t
  1018. relaxation_loop_body(int, Target*, Symbol_table*, Output_segment**,
  1019. Output_segment*, Output_segment_headers*,
  1020. Output_file_header*, unsigned int*);
  1021. // A mapping used for kept comdats/.gnu.linkonce group signatures.
  1022. typedef Unordered_map<std::string, Kept_section> Signatures;
  1023. // Mapping from input section name/type/flags to output section. We
  1024. // use canonicalized strings here.
  1025. typedef std::pair<Stringpool::Key,
  1026. std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
  1027. struct Hash_key
  1028. {
  1029. size_t
  1030. operator()(const Key& k) const;
  1031. };
  1032. typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
  1033. // A comparison class for segments.
  1034. class Compare_segments
  1035. {
  1036. public:
  1037. Compare_segments(Layout* layout)
  1038. : layout_(layout)
  1039. { }
  1040. bool
  1041. operator()(const Output_segment* seg1, const Output_segment* seg2)
  1042. { return this->layout_->segment_precedes(seg1, seg2); }
  1043. private:
  1044. Layout* layout_;
  1045. };
  1046. typedef std::vector<Output_section_data*> Output_section_data_list;
  1047. // Debug checker class.
  1048. class Relaxation_debug_check
  1049. {
  1050. public:
  1051. Relaxation_debug_check()
  1052. : section_infos_()
  1053. { }
  1054. // Check that sections and special data are in reset states.
  1055. void
  1056. check_output_data_for_reset_values(const Layout::Section_list&,
  1057. const Layout::Data_list& special_outputs,
  1058. const Layout::Data_list& relax_outputs);
  1059. // Record information of a section list.
  1060. void
  1061. read_sections(const Layout::Section_list&);
  1062. // Verify a section list with recorded information.
  1063. void
  1064. verify_sections(const Layout::Section_list&);
  1065. private:
  1066. // Information we care about a section.
  1067. struct Section_info
  1068. {
  1069. // Output section described by this.
  1070. Output_section* output_section;
  1071. // Load address.
  1072. uint64_t address;
  1073. // Data size.
  1074. off_t data_size;
  1075. // File offset.
  1076. off_t offset;
  1077. };
  1078. // Section information.
  1079. std::vector<Section_info> section_infos_;
  1080. };
  1081. // The number of input files, for sizing tables.
  1082. int number_of_input_files_;
  1083. // Information set by scripts or by command line options.
  1084. Script_options* script_options_;
  1085. // The output section names.
  1086. Stringpool namepool_;
  1087. // The output symbol names.
  1088. Stringpool sympool_;
  1089. // The dynamic strings, if needed.
  1090. Stringpool dynpool_;
  1091. // The list of group sections and linkonce sections which we have seen.
  1092. Signatures signatures_;
  1093. // The mapping from input section name/type/flags to output sections.
  1094. Section_name_map section_name_map_;
  1095. // The list of output segments.
  1096. Segment_list segment_list_;
  1097. // The list of output sections.
  1098. Section_list section_list_;
  1099. // The list of output sections which are not attached to any output
  1100. // segment.
  1101. Section_list unattached_section_list_;
  1102. // The list of unattached Output_data objects which require special
  1103. // handling because they are not Output_sections.
  1104. Data_list special_output_list_;
  1105. // Like special_output_list_, but cleared and recreated on each
  1106. // iteration of relaxation.
  1107. Data_list relax_output_list_;
  1108. // The section headers.
  1109. Output_section_headers* section_headers_;
  1110. // A pointer to the PT_TLS segment if there is one.
  1111. Output_segment* tls_segment_;
  1112. // A pointer to the PT_GNU_RELRO segment if there is one.
  1113. Output_segment* relro_segment_;
  1114. // A pointer to the PT_INTERP segment if there is one.
  1115. Output_segment* interp_segment_;
  1116. // A backend may increase the size of the PT_GNU_RELRO segment if
  1117. // there is one. This is the amount to increase it by.
  1118. unsigned int increase_relro_;
  1119. // The SHT_SYMTAB output section.
  1120. Output_section* symtab_section_;
  1121. // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
  1122. Output_symtab_xindex* symtab_xindex_;
  1123. // The SHT_DYNSYM output section if there is one.
  1124. Output_section* dynsym_section_;
  1125. // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
  1126. Output_symtab_xindex* dynsym_xindex_;
  1127. // The SHT_DYNAMIC output section if there is one.
  1128. Output_section* dynamic_section_;
  1129. // The _DYNAMIC symbol if there is one.
  1130. Symbol* dynamic_symbol_;
  1131. // The dynamic data which goes into dynamic_section_.
  1132. Output_data_dynamic* dynamic_data_;
  1133. // The exception frame output section if there is one.
  1134. Output_section* eh_frame_section_;
  1135. // The exception frame data for eh_frame_section_.
  1136. Eh_frame* eh_frame_data_;
  1137. // Whether we have added eh_frame_data_ to the .eh_frame section.
  1138. bool added_eh_frame_data_;
  1139. // The exception frame header output section if there is one.
  1140. Output_section* eh_frame_hdr_section_;
  1141. // The data for the .gdb_index section.
  1142. Gdb_index* gdb_index_data_;
  1143. // The space for the build ID checksum if there is one.
  1144. Output_section_data* build_id_note_;
  1145. // The output section containing dwarf abbreviations
  1146. Output_reduced_debug_abbrev_section* debug_abbrev_;
  1147. // The output section containing the dwarf debug info tree
  1148. Output_reduced_debug_info_section* debug_info_;
  1149. // A list of group sections and their signatures.
  1150. Group_signatures group_signatures_;
  1151. // The size of the output file.
  1152. off_t output_file_size_;
  1153. // Whether we have added an input section to an output section.
  1154. bool have_added_input_section_;
  1155. // Whether we have attached the sections to the segments.
  1156. bool sections_are_attached_;
  1157. // Whether we have seen an object file marked to require an
  1158. // executable stack.
  1159. bool input_requires_executable_stack_;
  1160. // Whether we have seen at least one object file with an executable
  1161. // stack marker.
  1162. bool input_with_gnu_stack_note_;
  1163. // Whether we have seen at least one object file without an
  1164. // executable stack marker.
  1165. bool input_without_gnu_stack_note_;
  1166. // Whether we have seen an object file that uses the static TLS model.
  1167. bool has_static_tls_;
  1168. // Whether any sections require postprocessing.
  1169. bool any_postprocessing_sections_;
  1170. // Whether we have resized the signatures_ hash table.
  1171. bool resized_signatures_;
  1172. // Whether we have created a .stab*str output section.
  1173. bool have_stabstr_section_;
  1174. // True if the input sections in the output sections should be sorted
  1175. // as specified in a section ordering file.
  1176. bool section_ordering_specified_;
  1177. // True if some input sections need to be mapped to a unique segment,
  1178. // after being mapped to a unique Output_section.
  1179. bool unique_segment_for_sections_specified_;
  1180. // In incremental build, holds information check the inputs and build the
  1181. // .gnu_incremental_inputs section.
  1182. Incremental_inputs* incremental_inputs_;
  1183. // Whether we record output section data created in script
  1184. bool record_output_section_data_from_script_;
  1185. // List of output data that needs to be removed at relaxation clean up.
  1186. Output_section_data_list script_output_section_data_list_;
  1187. // Structure to save segment states before entering the relaxation loop.
  1188. Segment_states* segment_states_;
  1189. // A relaxation debug checker. We only create one when in debugging mode.
  1190. Relaxation_debug_check* relaxation_debug_check_;
  1191. // Plugins specify section_ordering using this map. This is set in
  1192. // update_section_order in plugin.cc
  1193. std::map<Section_id, unsigned int> section_order_map_;
  1194. // This maps an input section to a unique segment. This is done by first
  1195. // placing such input sections in unique output sections and then mapping
  1196. // the output section to a unique segment. Unique_segment_info stores
  1197. // any additional flags and alignment of the new segment.
  1198. Section_segment_map section_segment_map_;
  1199. // Hash a pattern to its position in the section ordering file.
  1200. Unordered_map<std::string, unsigned int> input_section_position_;
  1201. // Vector of glob only patterns in the section_ordering file.
  1202. std::vector<std::string> input_section_glob_;
  1203. // For incremental links, the base file to be modified.
  1204. Incremental_binary* incremental_base_;
  1205. // For incremental links, a list of free space within the file.
  1206. Free_list free_list_;
  1207. };
  1208. // This task handles writing out data in output sections which is not
  1209. // part of an input section, or which requires special handling. When
  1210. // this is done, it unblocks both output_sections_blocker and
  1211. // final_blocker.
  1212. class Write_sections_task : public Task
  1213. {
  1214. public:
  1215. Write_sections_task(const Layout* layout, Output_file* of,
  1216. Task_token* output_sections_blocker,
  1217. Task_token* input_sections_blocker,
  1218. Task_token* final_blocker)
  1219. : layout_(layout), of_(of),
  1220. output_sections_blocker_(output_sections_blocker),
  1221. input_sections_blocker_(input_sections_blocker),
  1222. final_blocker_(final_blocker)
  1223. { }
  1224. // The standard Task methods.
  1225. Task_token*
  1226. is_runnable();
  1227. void
  1228. locks(Task_locker*);
  1229. void
  1230. run(Workqueue*);
  1231. std::string
  1232. get_name() const
  1233. { return "Write_sections_task"; }
  1234. private:
  1235. class Write_sections_locker;
  1236. const Layout* layout_;
  1237. Output_file* of_;
  1238. Task_token* output_sections_blocker_;
  1239. Task_token* input_sections_blocker_;
  1240. Task_token* final_blocker_;
  1241. };
  1242. // This task handles writing out data which is not part of a section
  1243. // or segment.
  1244. class Write_data_task : public Task
  1245. {
  1246. public:
  1247. Write_data_task(const Layout* layout, const Symbol_table* symtab,
  1248. Output_file* of, Task_token* final_blocker)
  1249. : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
  1250. { }
  1251. // The standard Task methods.
  1252. Task_token*
  1253. is_runnable();
  1254. void
  1255. locks(Task_locker*);
  1256. void
  1257. run(Workqueue*);
  1258. std::string
  1259. get_name() const
  1260. { return "Write_data_task"; }
  1261. private:
  1262. const Layout* layout_;
  1263. const Symbol_table* symtab_;
  1264. Output_file* of_;
  1265. Task_token* final_blocker_;
  1266. };
  1267. // This task handles writing out the global symbols.
  1268. class Write_symbols_task : public Task
  1269. {
  1270. public:
  1271. Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
  1272. const Input_objects* /*input_objects*/,
  1273. const Stringpool* sympool, const Stringpool* dynpool,
  1274. Output_file* of, Task_token* final_blocker)
  1275. : layout_(layout), symtab_(symtab),
  1276. sympool_(sympool), dynpool_(dynpool), of_(of),
  1277. final_blocker_(final_blocker)
  1278. { }
  1279. // The standard Task methods.
  1280. Task_token*
  1281. is_runnable();
  1282. void
  1283. locks(Task_locker*);
  1284. void
  1285. run(Workqueue*);
  1286. std::string
  1287. get_name() const
  1288. { return "Write_symbols_task"; }
  1289. private:
  1290. const Layout* layout_;
  1291. const Symbol_table* symtab_;
  1292. const Stringpool* sympool_;
  1293. const Stringpool* dynpool_;
  1294. Output_file* of_;
  1295. Task_token* final_blocker_;
  1296. };
  1297. // This task handles writing out data in output sections which can't
  1298. // be written out until all the input sections have been handled.
  1299. // This is for sections whose contents is based on the contents of
  1300. // other output sections.
  1301. class Write_after_input_sections_task : public Task
  1302. {
  1303. public:
  1304. Write_after_input_sections_task(Layout* layout, Output_file* of,
  1305. Task_token* input_sections_blocker,
  1306. Task_token* final_blocker)
  1307. : layout_(layout), of_(of),
  1308. input_sections_blocker_(input_sections_blocker),
  1309. final_blocker_(final_blocker)
  1310. { }
  1311. // The standard Task methods.
  1312. Task_token*
  1313. is_runnable();
  1314. void
  1315. locks(Task_locker*);
  1316. void
  1317. run(Workqueue*);
  1318. std::string
  1319. get_name() const
  1320. { return "Write_after_input_sections_task"; }
  1321. private:
  1322. Layout* layout_;
  1323. Output_file* of_;
  1324. Task_token* input_sections_blocker_;
  1325. Task_token* final_blocker_;
  1326. };
  1327. // This task function handles computation of the build id.
  1328. // When using --build-id=tree, it schedules the tasks that
  1329. // compute the hashes for each chunk of the file. This task
  1330. // cannot run until we have finalized the size of the output
  1331. // file, after the completion of Write_after_input_sections_task.
  1332. class Build_id_task_runner : public Task_function_runner
  1333. {
  1334. public:
  1335. Build_id_task_runner(const General_options* options, const Layout* layout,
  1336. Output_file* of)
  1337. : options_(options), layout_(layout), of_(of)
  1338. { }
  1339. // Run the operation.
  1340. void
  1341. run(Workqueue*, const Task*);
  1342. private:
  1343. const General_options* options_;
  1344. const Layout* layout_;
  1345. Output_file* of_;
  1346. };
  1347. // This task function handles closing the file.
  1348. class Close_task_runner : public Task_function_runner
  1349. {
  1350. public:
  1351. Close_task_runner(const General_options* options, const Layout* layout,
  1352. Output_file* of, unsigned char* array_of_hashes,
  1353. size_t size_of_hashes)
  1354. : options_(options), layout_(layout), of_(of),
  1355. array_of_hashes_(array_of_hashes), size_of_hashes_(size_of_hashes)
  1356. { }
  1357. // Run the operation.
  1358. void
  1359. run(Workqueue*, const Task*);
  1360. private:
  1361. const General_options* options_;
  1362. const Layout* layout_;
  1363. Output_file* of_;
  1364. unsigned char* const array_of_hashes_;
  1365. const size_t size_of_hashes_;
  1366. };
  1367. // A small helper function to align an address.
  1368. inline uint64_t
  1369. align_address(uint64_t address, uint64_t addralign)
  1370. {
  1371. if (addralign != 0)
  1372. address = (address + addralign - 1) &~ (addralign - 1);
  1373. return address;
  1374. }
  1375. } // End namespace gold.
  1376. #endif // !defined(GOLD_LAYOUT_H)