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