PR 10400
[external/binutils.git] / gold / layout.h
1 // layout.h -- lay out output file sections for gold  -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009 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 Input_objects;
45 class Mapfile;
46 class Symbol_table;
47 class Output_section_data;
48 class Output_section;
49 class Output_section_headers;
50 class Output_segment;
51 class Output_data;
52 class Output_data_dynamic;
53 class Output_symtab_xindex;
54 class Output_reduced_debug_abbrev_section;
55 class Output_reduced_debug_info_section;
56 class Eh_frame;
57 class Target;
58
59 // This task function handles mapping the input sections to output
60 // sections and laying them out in memory.
61
62 class Layout_task_runner : public Task_function_runner
63 {
64  public:
65   // OPTIONS is the command line options, INPUT_OBJECTS is the list of
66   // input objects, SYMTAB is the symbol table, LAYOUT is the layout
67   // object.
68   Layout_task_runner(const General_options& options,
69                      const Input_objects* input_objects,
70                      Symbol_table* symtab,
71                      Target* target,
72                      Layout* layout,
73                      Mapfile* mapfile)
74     : options_(options), input_objects_(input_objects), symtab_(symtab),
75       target_(target), layout_(layout), mapfile_(mapfile)
76   { }
77
78   // Run the operation.
79   void
80   run(Workqueue*, const Task*);
81
82  private:
83   Layout_task_runner(const Layout_task_runner&);
84   Layout_task_runner& operator=(const Layout_task_runner&);
85
86   const General_options& options_;
87   const Input_objects* input_objects_;
88   Symbol_table* symtab_;
89   Target* target_;
90   Layout* layout_;
91   Mapfile* mapfile_;
92 };
93
94 // This class holds information about the comdat group or
95 // .gnu.linkonce section that will be kept for a given signature.
96
97 class Kept_section
98 {
99  private:
100   // For a comdat group, we build a mapping from the name of each
101   // section in the group to the section index and the size in object.
102   // When we discard a group in some other object file, we use this
103   // map to figure out which kept section the discarded section is
104   // associated with.  We then use that mapping when processing relocs
105   // against discarded sections.
106   struct Comdat_section_info
107   {
108     // The section index.
109     unsigned int shndx;
110     // The section size.
111     uint64_t size;
112
113     Comdat_section_info(unsigned int a_shndx, uint64_t a_size)
114       : shndx(a_shndx), size(a_size)
115     { }
116   };
117
118   // Most comdat groups have only one or two sections, so we use a
119   // std::map rather than an Unordered_map to optimize for that case
120   // without paying too heavily for groups with more sections.
121   typedef std::map<std::string, Comdat_section_info> Comdat_group;
122
123  public:
124   Kept_section()
125     : object_(NULL), shndx_(0), is_comdat_(false), is_group_name_(false)
126   { this->u_.linkonce_size = 0; }
127
128   // We need to support copies for the signature map in the Layout
129   // object, but we should never copy an object after it has been
130   // marked as a comdat section.
131   Kept_section(const Kept_section& k)
132     : object_(k.object_), shndx_(k.shndx_), is_comdat_(false),
133       is_group_name_(k.is_group_name_)
134   {
135     gold_assert(!k.is_comdat_);
136     this->u_.linkonce_size = 0;
137   }
138
139   ~Kept_section()
140   {
141     if (this->is_comdat_)
142       delete this->u_.group_sections;
143   }
144
145   // The object where this section lives.
146   Relobj*
147   object() const
148   { return this->object_; }
149
150   // Set the object.
151   void
152   set_object(Relobj* object)
153   {
154     gold_assert(this->object_ == NULL);
155     this->object_ = object;
156   }
157
158   // The section index.
159   unsigned int
160   shndx() const
161   { return this->shndx_; }
162
163   // Set the section index.
164   void
165   set_shndx(unsigned int shndx)
166   {
167     gold_assert(this->shndx_ == 0);
168     this->shndx_ = shndx;
169   }
170
171   // Whether this is a comdat group.
172   bool
173   is_comdat() const
174   { return this->is_comdat_; }
175
176   // Set that this is a comdat group.
177   void
178   set_is_comdat()
179   {
180     gold_assert(!this->is_comdat_);
181     this->is_comdat_ = true;
182     this->u_.group_sections = new Comdat_group();
183   }
184
185   // Whether this is associated with the name of a group or section
186   // rather than the symbol name derived from a linkonce section.
187   bool
188   is_group_name() const
189   { return this->is_group_name_; }
190
191   // Note that this represents a comdat group rather than a single
192   // linkonce section.
193   void
194   set_is_group_name()
195   { this->is_group_name_ = true; }
196
197   // Add a section to the group list.
198   void
199   add_comdat_section(const std::string& name, unsigned int shndx,
200                      uint64_t size)
201   {
202     gold_assert(this->is_comdat_);
203     Comdat_section_info sinfo(shndx, size);
204     this->u_.group_sections->insert(std::make_pair(name, sinfo));
205   }
206
207   // Look for a section name in the group list, and return whether it
208   // was found.  If found, returns the section index and size.
209   bool
210   find_comdat_section(const std::string& name, unsigned int *pshndx,
211                       uint64_t *psize) const
212   {
213     gold_assert(this->is_comdat_);
214     Comdat_group::const_iterator p = this->u_.group_sections->find(name);
215     if (p == this->u_.group_sections->end())
216       return false;
217     *pshndx = p->second.shndx;
218     *psize = p->second.size;
219     return true;
220   }
221
222   // If there is only one section in the group list, return true, and
223   // return the section index and size.
224   bool
225   find_single_comdat_section(unsigned int *pshndx, uint64_t *psize) const
226   {
227     gold_assert(this->is_comdat_);
228     if (this->u_.group_sections->size() != 1)
229       return false;
230     Comdat_group::const_iterator p = this->u_.group_sections->begin();
231     *pshndx = p->second.shndx;
232     *psize = p->second.size;
233     return true;
234   }
235
236   // Return the size of a linkonce section.
237   uint64_t
238   linkonce_size() const
239   {
240     gold_assert(!this->is_comdat_);
241     return this->u_.linkonce_size;
242   }
243
244   // Set the size of a linkonce section.
245   void
246   set_linkonce_size(uint64_t size)
247   {
248     gold_assert(!this->is_comdat_);
249     this->u_.linkonce_size = size;
250   }
251
252  private:
253   // No assignment.
254   Kept_section& operator=(const Kept_section&);
255
256   // The object containing the comdat group or .gnu.linkonce section.
257   Relobj* object_;
258   // Index of the group section for comdats and the section itself for
259   // .gnu.linkonce.
260   unsigned int shndx_;
261   // True if this is for a comdat group rather than a .gnu.linkonce
262   // section.
263   bool is_comdat_;
264   // The Kept_sections are values of a mapping, that maps names to
265   // them.  This field is true if this struct is associated with the
266   // name of a comdat or .gnu.linkonce, false if it is associated with
267   // the name of a symbol obtained from the .gnu.linkonce.* name
268   // through some heuristics.
269   bool is_group_name_;
270   union
271   {
272     // If the is_comdat_ field is true, this holds a map from names of
273     // the sections in the group to section indexes in object_ and to
274     // section sizes.
275     Comdat_group* group_sections;
276     // If the is_comdat_ field is false, this holds the size of the
277     // single section.
278     uint64_t linkonce_size;
279   } u_;
280 };
281
282 // This class handles the details of laying out input sections.
283
284 class Layout
285 {
286  public:
287   Layout(int number_of_input_files, Script_options*);
288
289   // Given an input section SHNDX, named NAME, with data in SHDR, from
290   // the object file OBJECT, return the output section where this
291   // input section should go.  RELOC_SHNDX is the index of a
292   // relocation section which applies to this section, or 0 if none,
293   // or -1U if more than one.  RELOC_TYPE is the type of the
294   // relocation section if there is one.  Set *OFFSET to the offset
295   // within the output section.
296   template<int size, bool big_endian>
297   Output_section*
298   layout(Sized_relobj<size, big_endian> *object, unsigned int shndx,
299          const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
300          unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
301
302   // Layout an input reloc section when doing a relocatable link.  The
303   // section is RELOC_SHNDX in OBJECT, with data in SHDR.
304   // DATA_SECTION is the reloc section to which it refers.  RR is the
305   // relocatable information.
306   template<int size, bool big_endian>
307   Output_section*
308   layout_reloc(Sized_relobj<size, big_endian>* object,
309                unsigned int reloc_shndx,
310                const elfcpp::Shdr<size, big_endian>& shdr,
311                Output_section* data_section,
312                Relocatable_relocs* rr);
313
314   // Layout a group section when doing a relocatable link.
315   template<int size, bool big_endian>
316   void
317   layout_group(Symbol_table* symtab,
318                Sized_relobj<size, big_endian>* object,
319                unsigned int group_shndx,
320                const char* group_section_name,
321                const char* signature,
322                const elfcpp::Shdr<size, big_endian>& shdr,
323                elfcpp::Elf_Word flags,
324                std::vector<unsigned int>* shndxes);
325
326   // Like layout, only for exception frame sections.  OBJECT is an
327   // object file.  SYMBOLS is the contents of the symbol table
328   // section, with size SYMBOLS_SIZE.  SYMBOL_NAMES is the contents of
329   // the symbol name section, with size SYMBOL_NAMES_SIZE.  SHNDX is a
330   // .eh_frame section in OBJECT.  SHDR is the section header.
331   // RELOC_SHNDX is the index of a relocation section which applies to
332   // this section, or 0 if none, or -1U if more than one.  RELOC_TYPE
333   // is the type of the relocation section if there is one.  This
334   // returns the output section, and sets *OFFSET to the offset.
335   template<int size, bool big_endian>
336   Output_section*
337   layout_eh_frame(Sized_relobj<size, big_endian>* object,
338                   const unsigned char* symbols,
339                   off_t symbols_size,
340                   const unsigned char* symbol_names,
341                   off_t symbol_names_size,
342                   unsigned int shndx,
343                   const elfcpp::Shdr<size, big_endian>& shdr,
344                   unsigned int reloc_shndx, unsigned int reloc_type,
345                   off_t* offset);
346
347   // Handle a GNU stack note.  This is called once per input object
348   // file.  SEEN_GNU_STACK is true if the object file has a
349   // .note.GNU-stack section.  GNU_STACK_FLAGS is the section flags
350   // from that section if there was one.
351   void
352   layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
353
354   // Add an Output_section_data to the layout.  This is used for
355   // special sections like the GOT section.
356   Output_section*
357   add_output_section_data(const char* name, elfcpp::Elf_Word type,
358                           elfcpp::Elf_Xword flags,
359                           Output_section_data*);
360
361   // Create dynamic sections if necessary.
362   void
363   create_initial_dynamic_sections(Symbol_table*);
364
365   // Define __start and __stop symbols for output sections.
366   void
367   define_section_symbols(Symbol_table*);
368
369   // Create automatic note sections.
370   void
371   create_notes();
372
373   // Create sections for linker scripts.
374   void
375   create_script_sections()
376   { this->script_options_->create_script_sections(this); }
377
378   // Define symbols from any linker script.
379   void
380   define_script_symbols(Symbol_table* symtab)
381   { this->script_options_->add_symbols_to_table(symtab); }
382
383   // Define symbols for group signatures.
384   void
385   define_group_signatures(Symbol_table*);
386
387   // Return the Stringpool used for symbol names.
388   const Stringpool*
389   sympool() const
390   { return &this->sympool_; }
391
392   // Return the Stringpool used for dynamic symbol names and dynamic
393   // tags.
394   const Stringpool*
395   dynpool() const
396   { return &this->dynpool_; }
397
398   // Return the symtab_xindex section used to hold large section
399   // indexes for the normal symbol table.
400   Output_symtab_xindex*
401   symtab_xindex() const
402   { return this->symtab_xindex_; }
403
404   // Return the dynsym_xindex section used to hold large section
405   // indexes for the dynamic symbol table.
406   Output_symtab_xindex*
407   dynsym_xindex() const
408   { return this->dynsym_xindex_; }
409
410   // Return whether a section is a .gnu.linkonce section, given the
411   // section name.
412   static inline bool
413   is_linkonce(const char* name)
414   { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
415
416   // Return true if a section is a debugging section.
417   static inline bool
418   is_debug_info_section(const char* name)
419   {
420     // Debugging sections can only be recognized by name.
421     return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
422             || strncmp(name, ".gnu.linkonce.wi.",
423                        sizeof(".gnu.linkonce.wi.") - 1) == 0
424             || strncmp(name, ".line", sizeof(".line") - 1) == 0
425             || strncmp(name, ".stab", sizeof(".stab") - 1) == 0);
426   }
427
428   // Check if a comdat group or .gnu.linkonce section with the given
429   // NAME is selected for the link.  If there is already a section,
430   // *KEPT_SECTION is set to point to the signature and the function
431   // returns false.  Otherwise, OBJECT, SHNDX,IS_COMDAT, and
432   // IS_GROUP_NAME are recorded for this NAME in the layout object,
433   // *KEPT_SECTION is set to the internal copy and the function return
434   // false.
435   bool
436   find_or_add_kept_section(const std::string& name, Relobj* object, 
437                            unsigned int shndx, bool is_comdat,
438                            bool is_group_name, Kept_section** kept_section);
439
440   // Finalize the layout after all the input sections have been added.
441   off_t
442   finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
443
444   // Return whether any sections require postprocessing.
445   bool
446   any_postprocessing_sections() const
447   { return this->any_postprocessing_sections_; }
448
449   // Return the size of the output file.
450   off_t
451   output_file_size() const
452   { return this->output_file_size_; }
453
454   // Return the TLS segment.  This will return NULL if there isn't
455   // one.
456   Output_segment*
457   tls_segment() const
458   { return this->tls_segment_; }
459
460   // Return the normal symbol table.
461   Output_section*
462   symtab_section() const
463   {
464     gold_assert(this->symtab_section_ != NULL);
465     return this->symtab_section_;
466   }
467
468   // Return the dynamic symbol table.
469   Output_section*
470   dynsym_section() const
471   {
472     gold_assert(this->dynsym_section_ != NULL);
473     return this->dynsym_section_;
474   }
475
476   // Return the dynamic tags.
477   Output_data_dynamic*
478   dynamic_data() const
479   { return this->dynamic_data_; }
480
481   // Write out the output sections.
482   void
483   write_output_sections(Output_file* of) const;
484
485   // Write out data not associated with an input file or the symbol
486   // table.
487   void
488   write_data(const Symbol_table*, Output_file*) const;
489
490   // Write out output sections which can not be written until all the
491   // input sections are complete.
492   void
493   write_sections_after_input_sections(Output_file* of);
494
495   // Return an output section named NAME, or NULL if there is none.
496   Output_section*
497   find_output_section(const char* name) const;
498
499   // Return an output segment of type TYPE, with segment flags SET set
500   // and segment flags CLEAR clear.  Return NULL if there is none.
501   Output_segment*
502   find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
503                       elfcpp::Elf_Word clear) const;
504
505   // Return the number of segments we expect to produce.
506   size_t
507   expected_segment_count() const;
508
509   // Set a flag to indicate that an object file uses the static TLS model.
510   void
511   set_has_static_tls()
512   { this->has_static_tls_ = true; }
513
514   // Return true if any object file uses the static TLS model.
515   bool
516   has_static_tls() const
517   { return this->has_static_tls_; }
518
519   // Return the options which may be set by a linker script.
520   Script_options*
521   script_options()
522   { return this->script_options_; }
523
524   const Script_options*
525   script_options() const
526   { return this->script_options_; }
527
528   // Return the object managing inputs in incremental build. NULL in
529   // non-incremental builds.
530   Incremental_inputs*
531   incremental_inputs()
532   { return this->incremental_inputs_; }
533
534   // Compute and write out the build ID if needed.
535   void
536   write_build_id(Output_file*) const;
537
538   // Rewrite output file in binary format.
539   void
540   write_binary(Output_file* in) const;
541
542   // Print output sections to the map file.
543   void
544   print_to_mapfile(Mapfile*) const;
545
546   // Dump statistical information to stderr.
547   void
548   print_stats() const;
549
550   // A list of segments.
551
552   typedef std::vector<Output_segment*> Segment_list;
553
554   // A list of sections.
555
556   typedef std::vector<Output_section*> Section_list;
557
558   // The list of information to write out which is not attached to
559   // either a section or a segment.
560   typedef std::vector<Output_data*> Data_list;
561
562   // Store the allocated sections into the section list.  This is used
563   // by the linker script code.
564   void
565   get_allocated_sections(Section_list*) const;
566
567   // Make a section for a linker script to hold data.
568   Output_section*
569   make_output_section_for_script(const char* name);
570
571   // Make a segment.  This is used by the linker script code.
572   Output_segment*
573   make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
574
575   // Return the number of segments.
576   size_t
577   segment_count() const
578   { return this->segment_list_.size(); }
579
580   // Map from section flags to segment flags.
581   static elfcpp::Elf_Word
582   section_flags_to_segment(elfcpp::Elf_Xword flags);
583
584   // Attach sections to segments.
585   void
586   attach_sections_to_segments();
587
588  private:
589   Layout(const Layout&);
590   Layout& operator=(const Layout&);
591
592   // Mapping from input section names to output section names.
593   struct Section_name_mapping
594   {
595     const char* from;
596     int fromlen;
597     const char* to;
598     int tolen;
599   };
600   static const Section_name_mapping section_name_mapping[];
601   static const int section_name_mapping_count;
602
603   // During a relocatable link, a list of group sections and
604   // signatures.
605   struct Group_signature
606   {
607     // The group section.
608     Output_section* section;
609     // The signature.
610     const char* signature;
611
612     Group_signature()
613       : section(NULL), signature(NULL)
614     { }
615
616     Group_signature(Output_section* sectiona, const char* signaturea)
617       : section(sectiona), signature(signaturea)
618     { }
619   };
620   typedef std::vector<Group_signature> Group_signatures;
621
622   // Create a note section, filling in the header.
623   Output_section*
624   create_note(const char* name, int note_type, const char *section_name,
625               size_t descsz, bool allocate, size_t* trailing_padding);
626
627   // Create a note section for gold version.
628   void
629   create_gold_note();
630
631   // Record whether the stack must be executable.
632   void
633   create_executable_stack_info();
634
635   // Create a build ID note if needed.
636   void
637   create_build_id();
638
639   // Link .stab and .stabstr sections.
640   void
641   link_stabs_sections();
642
643   // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
644   // for the next run of incremental linking to check what has changed.
645   void
646   create_incremental_info_sections();
647
648   // Find the first read-only PT_LOAD segment, creating one if
649   // necessary.
650   Output_segment*
651   find_first_load_seg();
652
653   // Count the local symbols in the regular symbol table and the dynamic
654   // symbol table, and build the respective string pools.
655   void
656   count_local_symbols(const Task*, const Input_objects*);
657
658   // Create the output sections for the symbol table.
659   void
660   create_symtab_sections(const Input_objects*, Symbol_table*,
661                          unsigned int, off_t*);
662
663   // Create the .shstrtab section.
664   Output_section*
665   create_shstrtab();
666
667   // Create the section header table.
668   void
669   create_shdrs(const Output_section* shstrtab_section, off_t*);
670
671   // Create the dynamic symbol table.
672   void
673   create_dynamic_symtab(const Input_objects*, Symbol_table*,
674                         Output_section** pdynstr,
675                         unsigned int* plocal_dynamic_count,
676                         std::vector<Symbol*>* pdynamic_symbols,
677                         Versions* versions);
678
679   // Assign offsets to each local portion of the dynamic symbol table.
680   void
681   assign_local_dynsym_offsets(const Input_objects*);
682
683   // Finish the .dynamic section and PT_DYNAMIC segment.
684   void
685   finish_dynamic_section(const Input_objects*, const Symbol_table*);
686
687   // Create the .interp section and PT_INTERP segment.
688   void
689   create_interp(const Target* target);
690
691   // Create the version sections.
692   void
693   create_version_sections(const Versions*,
694                           const Symbol_table*,
695                           unsigned int local_symcount,
696                           const std::vector<Symbol*>& dynamic_symbols,
697                           const Output_section* dynstr);
698
699   template<int size, bool big_endian>
700   void
701   sized_create_version_sections(const Versions* versions,
702                                 const Symbol_table*,
703                                 unsigned int local_symcount,
704                                 const std::vector<Symbol*>& dynamic_symbols,
705                                 const Output_section* dynstr);
706
707   // Return whether to include this section in the link.
708   template<int size, bool big_endian>
709   bool
710   include_section(Sized_relobj<size, big_endian>* object, const char* name,
711                   const elfcpp::Shdr<size, big_endian>&);
712
713   // Return the output section name to use given an input section
714   // name.  Set *PLEN to the length of the name.  *PLEN must be
715   // initialized to the length of NAME.
716   static const char*
717   output_section_name(const char* name, size_t* plen);
718
719   // Return the number of allocated output sections.
720   size_t
721   allocated_output_section_count() const;
722
723   // Return the output section for NAME, TYPE and FLAGS.
724   Output_section*
725   get_output_section(const char* name, Stringpool::Key name_key,
726                      elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
727
728   // Choose the output section for NAME in RELOBJ.
729   Output_section*
730   choose_output_section(const Relobj* relobj, const char* name,
731                         elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
732                         bool is_input_section);
733
734   // Create a new Output_section.
735   Output_section*
736   make_output_section(const char* name, elfcpp::Elf_Word type,
737                       elfcpp::Elf_Xword flags);
738
739   // Attach a section to a segment.
740   void
741   attach_section_to_segment(Output_section*);
742
743   // Attach an allocated section to a segment.
744   void
745   attach_allocated_section_to_segment(Output_section*);
746
747   // Set the final file offsets of all the segments.
748   off_t
749   set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
750
751   // Set the file offsets of the sections when doing a relocatable
752   // link.
753   off_t
754   set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
755
756   // Set the final file offsets of all the sections not associated
757   // with a segment.  We set section offsets in three passes: the
758   // first handles all allocated sections, the second sections that
759   // require postprocessing, and the last the late-bound STRTAB
760   // sections (probably only shstrtab, which is the one we care about
761   // because it holds section names).
762   enum Section_offset_pass
763   {
764     BEFORE_INPUT_SECTIONS_PASS,
765     POSTPROCESSING_SECTIONS_PASS,
766     STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
767   };
768   off_t
769   set_section_offsets(off_t, Section_offset_pass pass);
770
771   // Set the final section indexes of all the sections not associated
772   // with a segment.  Returns the next unused index.
773   unsigned int
774   set_section_indexes(unsigned int pshndx);
775
776   // Set the section addresses when using a script.
777   Output_segment*
778   set_section_addresses_from_script(Symbol_table*);
779
780   // Return whether SEG1 comes before SEG2 in the output file.
781   static bool
782   segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
783
784   // A mapping used for kept comdats/.gnu.linkonce group signatures.
785   typedef Unordered_map<std::string, Kept_section> Signatures;
786
787   // Mapping from input section name/type/flags to output section.  We
788   // use canonicalized strings here.
789
790   typedef std::pair<Stringpool::Key,
791                     std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
792
793   struct Hash_key
794   {
795     size_t
796     operator()(const Key& k) const;
797   };
798
799   typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
800
801   // A comparison class for segments.
802
803   struct Compare_segments
804   {
805     bool
806     operator()(const Output_segment* seg1, const Output_segment* seg2)
807     { return Layout::segment_precedes(seg1, seg2); }
808   };
809
810   // The number of input files, for sizing tables.
811   int number_of_input_files_;
812   // Information set by scripts or by command line options.
813   Script_options* script_options_;
814   // The output section names.
815   Stringpool namepool_;
816   // The output symbol names.
817   Stringpool sympool_;
818   // The dynamic strings, if needed.
819   Stringpool dynpool_;
820   // The list of group sections and linkonce sections which we have seen.
821   Signatures signatures_;
822   // The mapping from input section name/type/flags to output sections.
823   Section_name_map section_name_map_;
824   // The list of output segments.
825   Segment_list segment_list_;
826   // The list of output sections.
827   Section_list section_list_;
828   // The list of output sections which are not attached to any output
829   // segment.
830   Section_list unattached_section_list_;
831   // The list of unattached Output_data objects which require special
832   // handling because they are not Output_sections.
833   Data_list special_output_list_;
834   // The section headers.
835   Output_section_headers* section_headers_;
836   // A pointer to the PT_TLS segment if there is one.
837   Output_segment* tls_segment_;
838   // A pointer to the PT_GNU_RELRO segment if there is one.
839   Output_segment* relro_segment_;
840   // The SHT_SYMTAB output section.
841   Output_section* symtab_section_;
842   // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
843   Output_symtab_xindex* symtab_xindex_;
844   // The SHT_DYNSYM output section if there is one.
845   Output_section* dynsym_section_;
846   // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
847   Output_symtab_xindex* dynsym_xindex_;
848   // The SHT_DYNAMIC output section if there is one.
849   Output_section* dynamic_section_;
850   // The dynamic data which goes into dynamic_section_.
851   Output_data_dynamic* dynamic_data_;
852   // The exception frame output section if there is one.
853   Output_section* eh_frame_section_;
854   // The exception frame data for eh_frame_section_.
855   Eh_frame* eh_frame_data_;
856   // Whether we have added eh_frame_data_ to the .eh_frame section.
857   bool added_eh_frame_data_;
858   // The exception frame header output section if there is one.
859   Output_section* eh_frame_hdr_section_;
860   // The space for the build ID checksum if there is one.
861   Output_section_data* build_id_note_;
862   // The output section containing dwarf abbreviations
863   Output_reduced_debug_abbrev_section* debug_abbrev_;
864   // The output section containing the dwarf debug info tree
865   Output_reduced_debug_info_section* debug_info_;
866   // A list of group sections and their signatures.
867   Group_signatures group_signatures_;
868   // The size of the output file.
869   off_t output_file_size_;
870   // Whether we have attached the sections to the segments.
871   bool sections_are_attached_;
872   // Whether we have seen an object file marked to require an
873   // executable stack.
874   bool input_requires_executable_stack_;
875   // Whether we have seen at least one object file with an executable
876   // stack marker.
877   bool input_with_gnu_stack_note_;
878   // Whether we have seen at least one object file without an
879   // executable stack marker.
880   bool input_without_gnu_stack_note_;
881   // Whether we have seen an object file that uses the static TLS model.
882   bool has_static_tls_;
883   // Whether any sections require postprocessing.
884   bool any_postprocessing_sections_;
885   // Whether we have resized the signatures_ hash table.
886   bool resized_signatures_;
887   // Whether we have created a .stab*str output section.
888   bool have_stabstr_section_;
889   // In incremental build, holds information check the inputs and build the
890   // .gnu_incremental_inputs section.
891   Incremental_inputs* incremental_inputs_;
892 };
893
894 // This task handles writing out data in output sections which is not
895 // part of an input section, or which requires special handling.  When
896 // this is done, it unblocks both output_sections_blocker and
897 // final_blocker.
898
899 class Write_sections_task : public Task
900 {
901  public:
902   Write_sections_task(const Layout* layout, Output_file* of,
903                       Task_token* output_sections_blocker,
904                       Task_token* final_blocker)
905     : layout_(layout), of_(of),
906       output_sections_blocker_(output_sections_blocker),
907       final_blocker_(final_blocker)
908   { }
909
910   // The standard Task methods.
911
912   Task_token*
913   is_runnable();
914
915   void
916   locks(Task_locker*);
917
918   void
919   run(Workqueue*);
920
921   std::string
922   get_name() const
923   { return "Write_sections_task"; }
924
925  private:
926   class Write_sections_locker;
927
928   const Layout* layout_;
929   Output_file* of_;
930   Task_token* output_sections_blocker_;
931   Task_token* final_blocker_;
932 };
933
934 // This task handles writing out data which is not part of a section
935 // or segment.
936
937 class Write_data_task : public Task
938 {
939  public:
940   Write_data_task(const Layout* layout, const Symbol_table* symtab,
941                   Output_file* of, Task_token* final_blocker)
942     : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
943   { }
944
945   // The standard Task methods.
946
947   Task_token*
948   is_runnable();
949
950   void
951   locks(Task_locker*);
952
953   void
954   run(Workqueue*);
955
956   std::string
957   get_name() const
958   { return "Write_data_task"; }
959
960  private:
961   const Layout* layout_;
962   const Symbol_table* symtab_;
963   Output_file* of_;
964   Task_token* final_blocker_;
965 };
966
967 // This task handles writing out the global symbols.
968
969 class Write_symbols_task : public Task
970 {
971  public:
972   Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
973                      const Input_objects* input_objects,
974                      const Stringpool* sympool, const Stringpool* dynpool,
975                      Output_file* of, Task_token* final_blocker)
976     : layout_(layout), symtab_(symtab), input_objects_(input_objects),
977       sympool_(sympool), dynpool_(dynpool), of_(of),
978       final_blocker_(final_blocker)
979   { }
980
981   // The standard Task methods.
982
983   Task_token*
984   is_runnable();
985
986   void
987   locks(Task_locker*);
988
989   void
990   run(Workqueue*);
991
992   std::string
993   get_name() const
994   { return "Write_symbols_task"; }
995
996  private:
997   const Layout* layout_;
998   const Symbol_table* symtab_;
999   const Input_objects* input_objects_;
1000   const Stringpool* sympool_;
1001   const Stringpool* dynpool_;
1002   Output_file* of_;
1003   Task_token* final_blocker_;
1004 };
1005
1006 // This task handles writing out data in output sections which can't
1007 // be written out until all the input sections have been handled.
1008 // This is for sections whose contents is based on the contents of
1009 // other output sections.
1010
1011 class Write_after_input_sections_task : public Task
1012 {
1013  public:
1014   Write_after_input_sections_task(Layout* layout, Output_file* of,
1015                                   Task_token* input_sections_blocker,
1016                                   Task_token* final_blocker)
1017     : layout_(layout), of_(of),
1018       input_sections_blocker_(input_sections_blocker),
1019       final_blocker_(final_blocker)
1020   { }
1021
1022   // The standard Task methods.
1023
1024   Task_token*
1025   is_runnable();
1026
1027   void
1028   locks(Task_locker*);
1029
1030   void
1031   run(Workqueue*);
1032
1033   std::string
1034   get_name() const
1035   { return "Write_after_input_sections_task"; }
1036
1037  private:
1038   Layout* layout_;
1039   Output_file* of_;
1040   Task_token* input_sections_blocker_;
1041   Task_token* final_blocker_;
1042 };
1043
1044 // This task function handles closing the file.
1045
1046 class Close_task_runner : public Task_function_runner
1047 {
1048  public:
1049   Close_task_runner(const General_options* options, const Layout* layout,
1050                     Output_file* of)
1051     : options_(options), layout_(layout), of_(of)
1052   { }
1053
1054   // Run the operation.
1055   void
1056   run(Workqueue*, const Task*);
1057
1058  private:
1059   const General_options* options_;
1060   const Layout* layout_;
1061   Output_file* of_;
1062 };
1063
1064 // A small helper function to align an address.
1065
1066 inline uint64_t
1067 align_address(uint64_t address, uint64_t addralign)
1068 {
1069   if (addralign != 0)
1070     address = (address + addralign - 1) &~ (addralign - 1);
1071   return address;
1072 }
1073
1074 } // End namespace gold.
1075
1076 #endif // !defined(GOLD_LAYOUT_H)