OSDN Git Service

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