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