418f016605be1928644661b67d79204b4d2cebca
[external/binutils.git] / gold / output.h
1 // output.h -- manage the output file for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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_OUTPUT_H
24 #define GOLD_OUTPUT_H
25
26 #include <list>
27 #include <vector>
28
29 #include "elfcpp.h"
30 #include "mapfile.h"
31 #include "layout.h"
32 #include "reloc-types.h"
33
34 namespace gold
35 {
36
37 class General_options;
38 class Object;
39 class Symbol;
40 class Output_file;
41 class Output_merge_base;
42 class Output_section;
43 class Relocatable_relocs;
44 class Target;
45 template<int size, bool big_endian>
46 class Sized_target;
47 template<int size, bool big_endian>
48 class Sized_relobj;
49
50 // An abtract class for data which has to go into the output file.
51
52 class Output_data
53 {
54  public:
55   explicit Output_data()
56     : address_(0), data_size_(0), offset_(-1),
57       is_address_valid_(false), is_data_size_valid_(false),
58       is_offset_valid_(false), is_data_size_fixed_(false),
59       has_dynamic_reloc_(false)
60   { }
61
62   virtual
63   ~Output_data();
64
65   // Return the address.  For allocated sections, this is only valid
66   // after Layout::finalize is finished.
67   uint64_t
68   address() const
69   {
70     gold_assert(this->is_address_valid_);
71     return this->address_;
72   }
73
74   // Return the size of the data.  For allocated sections, this must
75   // be valid after Layout::finalize calls set_address, but need not
76   // be valid before then.
77   off_t
78   data_size() const
79   {
80     gold_assert(this->is_data_size_valid_);
81     return this->data_size_;
82   }
83
84   // Get the current data size.
85   off_t
86   current_data_size() const
87   { return this->current_data_size_for_child(); }
88
89   // Return true if data size is fixed.
90   bool
91   is_data_size_fixed() const
92   { return this->is_data_size_fixed_; }
93   
94   // Return the file offset.  This is only valid after
95   // Layout::finalize is finished.  For some non-allocated sections,
96   // it may not be valid until near the end of the link.
97   off_t
98   offset() const
99   {
100     gold_assert(this->is_offset_valid_);
101     return this->offset_;
102   }
103
104   // Reset the address and file offset.  This essentially disables the
105   // sanity testing about duplicate and unknown settings.
106   void
107   reset_address_and_file_offset()
108   {
109     this->is_address_valid_ = false;
110     this->is_offset_valid_ = false;
111     if (!this->is_data_size_fixed_)
112       this->is_data_size_valid_ = false;
113     this->do_reset_address_and_file_offset();
114   }
115
116   // Return true if address and file offset already have reset values. In
117   // other words, calling reset_address_and_file_offset will not change them.
118   bool
119   address_and_file_offset_have_reset_values() const
120   { return this->do_address_and_file_offset_have_reset_values(); }
121
122   // Return the required alignment.
123   uint64_t
124   addralign() const
125   { return this->do_addralign(); }
126
127   // Return whether this has a load address.
128   bool
129   has_load_address() const
130   { return this->do_has_load_address(); }
131
132   // Return the load address.
133   uint64_t
134   load_address() const
135   { return this->do_load_address(); }
136
137   // Return whether this is an Output_section.
138   bool
139   is_section() const
140   { return this->do_is_section(); }
141
142   // Return whether this is an Output_section of the specified type.
143   bool
144   is_section_type(elfcpp::Elf_Word stt) const
145   { return this->do_is_section_type(stt); }
146
147   // Return whether this is an Output_section with the specified flag
148   // set.
149   bool
150   is_section_flag_set(elfcpp::Elf_Xword shf) const
151   { return this->do_is_section_flag_set(shf); }
152
153   // Return the output section that this goes in, if there is one.
154   Output_section*
155   output_section()
156   { return this->do_output_section(); }
157
158   const Output_section*
159   output_section() const
160   { return this->do_output_section(); }
161
162   // Return the output section index, if there is an output section.
163   unsigned int
164   out_shndx() const
165   { return this->do_out_shndx(); }
166
167   // Set the output section index, if this is an output section.
168   void
169   set_out_shndx(unsigned int shndx)
170   { this->do_set_out_shndx(shndx); }
171
172   // Set the address and file offset of this data, and finalize the
173   // size of the data.  This is called during Layout::finalize for
174   // allocated sections.
175   void
176   set_address_and_file_offset(uint64_t addr, off_t off)
177   {
178     this->set_address(addr);
179     this->set_file_offset(off);
180     this->finalize_data_size();
181   }
182
183   // Set the address.
184   void
185   set_address(uint64_t addr)
186   {
187     gold_assert(!this->is_address_valid_);
188     this->address_ = addr;
189     this->is_address_valid_ = true;
190   }
191
192   // Set the file offset.
193   void
194   set_file_offset(off_t off)
195   {
196     gold_assert(!this->is_offset_valid_);
197     this->offset_ = off;
198     this->is_offset_valid_ = true;
199   }
200
201   // Update the data size without finalizing it.
202   void
203   pre_finalize_data_size()
204   {
205     if (!this->is_data_size_valid_)
206       {
207         // Tell the child class to update the data size.
208         this->update_data_size();
209       }
210   }
211
212   // Finalize the data size.
213   void
214   finalize_data_size()
215   {
216     if (!this->is_data_size_valid_)
217       {
218         // Tell the child class to set the data size.
219         this->set_final_data_size();
220         gold_assert(this->is_data_size_valid_);
221       }
222   }
223
224   // Set the TLS offset.  Called only for SHT_TLS sections.
225   void
226   set_tls_offset(uint64_t tls_base)
227   { this->do_set_tls_offset(tls_base); }
228
229   // Return the TLS offset, relative to the base of the TLS segment.
230   // Valid only for SHT_TLS sections.
231   uint64_t
232   tls_offset() const
233   { return this->do_tls_offset(); }
234
235   // Write the data to the output file.  This is called after
236   // Layout::finalize is complete.
237   void
238   write(Output_file* file)
239   { this->do_write(file); }
240
241   // This is called by Layout::finalize to note that the sizes of
242   // allocated sections must now be fixed.
243   static void
244   layout_complete()
245   { Output_data::allocated_sizes_are_fixed = true; }
246
247   // Used to check that layout has been done.
248   static bool
249   is_layout_complete()
250   { return Output_data::allocated_sizes_are_fixed; }
251
252   // Note that a dynamic reloc has been applied to this data.
253   void
254   add_dynamic_reloc()
255   { this->has_dynamic_reloc_ = true; }
256
257   // Return whether a dynamic reloc has been applied.
258   bool
259   has_dynamic_reloc() const
260   { return this->has_dynamic_reloc_; }
261
262   // Whether the address is valid.
263   bool
264   is_address_valid() const
265   { return this->is_address_valid_; }
266
267   // Whether the file offset is valid.
268   bool
269   is_offset_valid() const
270   { return this->is_offset_valid_; }
271
272   // Whether the data size is valid.
273   bool
274   is_data_size_valid() const
275   { return this->is_data_size_valid_; }
276
277   // Print information to the map file.
278   void
279   print_to_mapfile(Mapfile* mapfile) const
280   { return this->do_print_to_mapfile(mapfile); }
281
282  protected:
283   // Functions that child classes may or in some cases must implement.
284
285   // Write the data to the output file.
286   virtual void
287   do_write(Output_file*) = 0;
288
289   // Return the required alignment.
290   virtual uint64_t
291   do_addralign() const = 0;
292
293   // Return whether this has a load address.
294   virtual bool
295   do_has_load_address() const
296   { return false; }
297
298   // Return the load address.
299   virtual uint64_t
300   do_load_address() const
301   { gold_unreachable(); }
302
303   // Return whether this is an Output_section.
304   virtual bool
305   do_is_section() const
306   { return false; }
307
308   // Return whether this is an Output_section of the specified type.
309   // This only needs to be implement by Output_section.
310   virtual bool
311   do_is_section_type(elfcpp::Elf_Word) const
312   { return false; }
313
314   // Return whether this is an Output_section with the specific flag
315   // set.  This only needs to be implemented by Output_section.
316   virtual bool
317   do_is_section_flag_set(elfcpp::Elf_Xword) const
318   { return false; }
319
320   // Return the output section, if there is one.
321   virtual Output_section*
322   do_output_section()
323   { return NULL; }
324
325   virtual const Output_section*
326   do_output_section() const
327   { return NULL; }
328
329   // Return the output section index, if there is an output section.
330   virtual unsigned int
331   do_out_shndx() const
332   { gold_unreachable(); }
333
334   // Set the output section index, if this is an output section.
335   virtual void
336   do_set_out_shndx(unsigned int)
337   { gold_unreachable(); }
338
339   // This is a hook for derived classes to set the preliminary data size.
340   // This is called by pre_finalize_data_size, normally called during
341   // Layout::finalize, before the section address is set, and is used
342   // during an incremental update, when we need to know the size of a
343   // section before allocating space in the output file.  For classes
344   // where the current data size is up to date, this default version of
345   // the method can be inherited.
346   virtual void
347   update_data_size()
348   { }
349
350   // This is a hook for derived classes to set the data size.  This is
351   // called by finalize_data_size, normally called during
352   // Layout::finalize, when the section address is set.
353   virtual void
354   set_final_data_size()
355   { gold_unreachable(); }
356
357   // A hook for resetting the address and file offset.
358   virtual void
359   do_reset_address_and_file_offset()
360   { }
361
362   // Return true if address and file offset already have reset values. In
363   // other words, calling reset_address_and_file_offset will not change them.
364   // A child class overriding do_reset_address_and_file_offset may need to
365   // also override this.
366   virtual bool
367   do_address_and_file_offset_have_reset_values() const
368   { return !this->is_address_valid_ && !this->is_offset_valid_; }
369
370   // Set the TLS offset.  Called only for SHT_TLS sections.
371   virtual void
372   do_set_tls_offset(uint64_t)
373   { gold_unreachable(); }
374
375   // Return the TLS offset, relative to the base of the TLS segment.
376   // Valid only for SHT_TLS sections.
377   virtual uint64_t
378   do_tls_offset() const
379   { gold_unreachable(); }
380
381   // Print to the map file.  This only needs to be implemented by
382   // classes which may appear in a PT_LOAD segment.
383   virtual void
384   do_print_to_mapfile(Mapfile*) const
385   { gold_unreachable(); }
386
387   // Functions that child classes may call.
388
389   // Reset the address.  The Output_section class needs this when an
390   // SHF_ALLOC input section is added to an output section which was
391   // formerly not SHF_ALLOC.
392   void
393   mark_address_invalid()
394   { this->is_address_valid_ = false; }
395
396   // Set the size of the data.
397   void
398   set_data_size(off_t data_size)
399   {
400     gold_assert(!this->is_data_size_valid_
401                 && !this->is_data_size_fixed_);
402     this->data_size_ = data_size;
403     this->is_data_size_valid_ = true;
404   }
405
406   // Fix the data size.  Once it is fixed, it cannot be changed
407   // and the data size remains always valid. 
408   void
409   fix_data_size()
410   {
411     gold_assert(this->is_data_size_valid_);
412     this->is_data_size_fixed_ = true;
413   }
414
415   // Get the current data size--this is for the convenience of
416   // sections which build up their size over time.
417   off_t
418   current_data_size_for_child() const
419   { return this->data_size_; }
420
421   // Set the current data size--this is for the convenience of
422   // sections which build up their size over time.
423   void
424   set_current_data_size_for_child(off_t data_size)
425   {
426     gold_assert(!this->is_data_size_valid_);
427     this->data_size_ = data_size;
428   }
429
430   // Return default alignment for the target size.
431   static uint64_t
432   default_alignment();
433
434   // Return default alignment for a specified size--32 or 64.
435   static uint64_t
436   default_alignment_for_size(int size);
437
438  private:
439   Output_data(const Output_data&);
440   Output_data& operator=(const Output_data&);
441
442   // This is used for verification, to make sure that we don't try to
443   // change any sizes of allocated sections after we set the section
444   // addresses.
445   static bool allocated_sizes_are_fixed;
446
447   // Memory address in output file.
448   uint64_t address_;
449   // Size of data in output file.
450   off_t data_size_;
451   // File offset of contents in output file.
452   off_t offset_;
453   // Whether address_ is valid.
454   bool is_address_valid_ : 1;
455   // Whether data_size_ is valid.
456   bool is_data_size_valid_ : 1;
457   // Whether offset_ is valid.
458   bool is_offset_valid_ : 1;
459   // Whether data size is fixed.
460   bool is_data_size_fixed_ : 1;
461   // Whether any dynamic relocs have been applied to this section.
462   bool has_dynamic_reloc_ : 1;
463 };
464
465 // Output the section headers.
466
467 class Output_section_headers : public Output_data
468 {
469  public:
470   Output_section_headers(const Layout*,
471                          const Layout::Segment_list*,
472                          const Layout::Section_list*,
473                          const Layout::Section_list*,
474                          const Stringpool*,
475                          const Output_section*);
476
477  protected:
478   // Write the data to the file.
479   void
480   do_write(Output_file*);
481
482   // Return the required alignment.
483   uint64_t
484   do_addralign() const
485   { return Output_data::default_alignment(); }
486
487   // Write to a map file.
488   void
489   do_print_to_mapfile(Mapfile* mapfile) const
490   { mapfile->print_output_data(this, _("** section headers")); }
491
492   // Update the data size.
493   void
494   update_data_size()
495   { this->set_data_size(this->do_size()); }
496
497   // Set final data size.
498   void
499   set_final_data_size()
500   { this->set_data_size(this->do_size()); }
501
502  private:
503   // Write the data to the file with the right size and endianness.
504   template<int size, bool big_endian>
505   void
506   do_sized_write(Output_file*);
507
508   // Compute data size.
509   off_t
510   do_size() const;
511
512   const Layout* layout_;
513   const Layout::Segment_list* segment_list_;
514   const Layout::Section_list* section_list_;
515   const Layout::Section_list* unattached_section_list_;
516   const Stringpool* secnamepool_;
517   const Output_section* shstrtab_section_;
518 };
519
520 // Output the segment headers.
521
522 class Output_segment_headers : public Output_data
523 {
524  public:
525   Output_segment_headers(const Layout::Segment_list& segment_list);
526
527  protected:
528   // Write the data to the file.
529   void
530   do_write(Output_file*);
531
532   // Return the required alignment.
533   uint64_t
534   do_addralign() const
535   { return Output_data::default_alignment(); }
536
537   // Write to a map file.
538   void
539   do_print_to_mapfile(Mapfile* mapfile) const
540   { mapfile->print_output_data(this, _("** segment headers")); }
541
542   // Set final data size.
543   void
544   set_final_data_size()
545   { this->set_data_size(this->do_size()); }
546
547  private:
548   // Write the data to the file with the right size and endianness.
549   template<int size, bool big_endian>
550   void
551   do_sized_write(Output_file*);
552
553   // Compute the current size.
554   off_t
555   do_size() const;
556
557   const Layout::Segment_list& segment_list_;
558 };
559
560 // Output the ELF file header.
561
562 class Output_file_header : public Output_data
563 {
564  public:
565   Output_file_header(const Target*,
566                      const Symbol_table*,
567                      const Output_segment_headers*,
568                      const char* entry);
569
570   // Add information about the section headers.  We lay out the ELF
571   // file header before we create the section headers.
572   void set_section_info(const Output_section_headers*,
573                         const Output_section* shstrtab);
574
575  protected:
576   // Write the data to the file.
577   void
578   do_write(Output_file*);
579
580   // Return the required alignment.
581   uint64_t
582   do_addralign() const
583   { return Output_data::default_alignment(); }
584
585   // Write to a map file.
586   void
587   do_print_to_mapfile(Mapfile* mapfile) const
588   { mapfile->print_output_data(this, _("** file header")); }
589
590   // Set final data size.
591   void
592   set_final_data_size(void)
593   { this->set_data_size(this->do_size()); }
594
595  private:
596   // Write the data to the file with the right size and endianness.
597   template<int size, bool big_endian>
598   void
599   do_sized_write(Output_file*);
600
601   // Return the value to use for the entry address.
602   template<int size>
603   typename elfcpp::Elf_types<size>::Elf_Addr
604   entry();
605
606   // Compute the current data size.
607   off_t
608   do_size() const;
609
610   const Target* target_;
611   const Symbol_table* symtab_;
612   const Output_segment_headers* segment_header_;
613   const Output_section_headers* section_header_;
614   const Output_section* shstrtab_;
615   const char* entry_;
616 };
617
618 // Output sections are mainly comprised of input sections.  However,
619 // there are cases where we have data to write out which is not in an
620 // input section.  Output_section_data is used in such cases.  This is
621 // an abstract base class.
622
623 class Output_section_data : public Output_data
624 {
625  public:
626   Output_section_data(off_t data_size, uint64_t addralign,
627                       bool is_data_size_fixed)
628     : Output_data(), output_section_(NULL), addralign_(addralign)
629   {
630     this->set_data_size(data_size);
631     if (is_data_size_fixed)
632       this->fix_data_size();
633   }
634
635   Output_section_data(uint64_t addralign)
636     : Output_data(), output_section_(NULL), addralign_(addralign)
637   { }
638
639   // Return the output section.
640   Output_section*
641   output_section()
642   { return this->output_section_; }
643
644   const Output_section*
645   output_section() const
646   { return this->output_section_; }
647
648   // Record the output section.
649   void
650   set_output_section(Output_section* os);
651
652   // Add an input section, for SHF_MERGE sections.  This returns true
653   // if the section was handled.
654   bool
655   add_input_section(Relobj* object, unsigned int shndx)
656   { return this->do_add_input_section(object, shndx); }
657
658   // Given an input OBJECT, an input section index SHNDX within that
659   // object, and an OFFSET relative to the start of that input
660   // section, return whether or not the corresponding offset within
661   // the output section is known.  If this function returns true, it
662   // sets *POUTPUT to the output offset.  The value -1 indicates that
663   // this input offset is being discarded.
664   bool
665   output_offset(const Relobj* object, unsigned int shndx,
666                 section_offset_type offset,
667                 section_offset_type* poutput) const
668   { return this->do_output_offset(object, shndx, offset, poutput); }
669
670   // Return whether this is the merge section for the input section
671   // SHNDX in OBJECT.  This should return true when output_offset
672   // would return true for some values of OFFSET.
673   bool
674   is_merge_section_for(const Relobj* object, unsigned int shndx) const
675   { return this->do_is_merge_section_for(object, shndx); }
676
677   // Write the contents to a buffer.  This is used for sections which
678   // require postprocessing, such as compression.
679   void
680   write_to_buffer(unsigned char* buffer)
681   { this->do_write_to_buffer(buffer); }
682
683   // Print merge stats to stderr.  This should only be called for
684   // SHF_MERGE sections.
685   void
686   print_merge_stats(const char* section_name)
687   { this->do_print_merge_stats(section_name); }
688
689  protected:
690   // The child class must implement do_write.
691
692   // The child class may implement specific adjustments to the output
693   // section.
694   virtual void
695   do_adjust_output_section(Output_section*)
696   { }
697
698   // May be implemented by child class.  Return true if the section
699   // was handled.
700   virtual bool
701   do_add_input_section(Relobj*, unsigned int)
702   { gold_unreachable(); }
703
704   // The child class may implement output_offset.
705   virtual bool
706   do_output_offset(const Relobj*, unsigned int, section_offset_type,
707                    section_offset_type*) const
708   { return false; }
709
710   // The child class may implement is_merge_section_for.
711   virtual bool
712   do_is_merge_section_for(const Relobj*, unsigned int) const
713   { return false; }
714
715   // The child class may implement write_to_buffer.  Most child
716   // classes can not appear in a compressed section, and they do not
717   // implement this.
718   virtual void
719   do_write_to_buffer(unsigned char*)
720   { gold_unreachable(); }
721
722   // Print merge statistics.
723   virtual void
724   do_print_merge_stats(const char*)
725   { gold_unreachable(); }
726
727   // Return the required alignment.
728   uint64_t
729   do_addralign() const
730   { return this->addralign_; }
731
732   // Return the output section.
733   Output_section*
734   do_output_section()
735   { return this->output_section_; }
736
737   const Output_section*
738   do_output_section() const
739   { return this->output_section_; }
740
741   // Return the section index of the output section.
742   unsigned int
743   do_out_shndx() const;
744
745   // Set the alignment.
746   void
747   set_addralign(uint64_t addralign);
748
749  private:
750   // The output section for this section.
751   Output_section* output_section_;
752   // The required alignment.
753   uint64_t addralign_;
754 };
755
756 // Some Output_section_data classes build up their data step by step,
757 // rather than all at once.  This class provides an interface for
758 // them.
759
760 class Output_section_data_build : public Output_section_data
761 {
762  public:
763   Output_section_data_build(uint64_t addralign)
764     : Output_section_data(addralign)
765   { }
766
767   Output_section_data_build(off_t data_size, uint64_t addralign)
768     : Output_section_data(data_size, addralign, false)
769   { }
770
771   // Set the current data size.
772   void
773   set_current_data_size(off_t data_size)
774   { this->set_current_data_size_for_child(data_size); }
775
776  protected:
777   // Set the final data size.
778   virtual void
779   set_final_data_size()
780   { this->set_data_size(this->current_data_size_for_child()); }
781 };
782
783 // A simple case of Output_data in which we have constant data to
784 // output.
785
786 class Output_data_const : public Output_section_data
787 {
788  public:
789   Output_data_const(const std::string& data, uint64_t addralign)
790     : Output_section_data(data.size(), addralign, true), data_(data)
791   { }
792
793   Output_data_const(const char* p, off_t len, uint64_t addralign)
794     : Output_section_data(len, addralign, true), data_(p, len)
795   { }
796
797   Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
798     : Output_section_data(len, addralign, true),
799       data_(reinterpret_cast<const char*>(p), len)
800   { }
801
802  protected:
803   // Write the data to the output file.
804   void
805   do_write(Output_file*);
806
807   // Write the data to a buffer.
808   void
809   do_write_to_buffer(unsigned char* buffer)
810   { memcpy(buffer, this->data_.data(), this->data_.size()); }
811
812   // Write to a map file.
813   void
814   do_print_to_mapfile(Mapfile* mapfile) const
815   { mapfile->print_output_data(this, _("** fill")); }
816
817  private:
818   std::string data_;
819 };
820
821 // Another version of Output_data with constant data, in which the
822 // buffer is allocated by the caller.
823
824 class Output_data_const_buffer : public Output_section_data
825 {
826  public:
827   Output_data_const_buffer(const unsigned char* p, off_t len,
828                            uint64_t addralign, const char* map_name)
829     : Output_section_data(len, addralign, true),
830       p_(p), map_name_(map_name)
831   { }
832
833  protected:
834   // Write the data the output file.
835   void
836   do_write(Output_file*);
837
838   // Write the data to a buffer.
839   void
840   do_write_to_buffer(unsigned char* buffer)
841   { memcpy(buffer, this->p_, this->data_size()); }
842
843   // Write to a map file.
844   void
845   do_print_to_mapfile(Mapfile* mapfile) const
846   { mapfile->print_output_data(this, _(this->map_name_)); }
847
848  private:
849   // The data to output.
850   const unsigned char* p_;
851   // Name to use in a map file.  Maps are a rarely used feature, but
852   // the space usage is minor as aren't very many of these objects.
853   const char* map_name_;
854 };
855
856 // A place holder for a fixed amount of data written out via some
857 // other mechanism.
858
859 class Output_data_fixed_space : public Output_section_data
860 {
861  public:
862   Output_data_fixed_space(off_t data_size, uint64_t addralign,
863                           const char* map_name)
864     : Output_section_data(data_size, addralign, true),
865       map_name_(map_name)
866   { }
867
868  protected:
869   // Write out the data--the actual data must be written out
870   // elsewhere.
871   void
872   do_write(Output_file*)
873   { }
874
875   // Write to a map file.
876   void
877   do_print_to_mapfile(Mapfile* mapfile) const
878   { mapfile->print_output_data(this, _(this->map_name_)); }
879
880  private:
881   // Name to use in a map file.  Maps are a rarely used feature, but
882   // the space usage is minor as aren't very many of these objects.
883   const char* map_name_;
884 };
885
886 // A place holder for variable sized data written out via some other
887 // mechanism.
888
889 class Output_data_space : public Output_section_data_build
890 {
891  public:
892   explicit Output_data_space(uint64_t addralign, const char* map_name)
893     : Output_section_data_build(addralign),
894       map_name_(map_name)
895   { }
896
897   explicit Output_data_space(off_t data_size, uint64_t addralign,
898                              const char* map_name)
899     : Output_section_data_build(data_size, addralign),
900       map_name_(map_name)
901   { }
902
903   // Set the alignment.
904   void
905   set_space_alignment(uint64_t align)
906   { this->set_addralign(align); }
907
908  protected:
909   // Write out the data--the actual data must be written out
910   // elsewhere.
911   void
912   do_write(Output_file*)
913   { }
914
915   // Write to a map file.
916   void
917   do_print_to_mapfile(Mapfile* mapfile) const
918   { mapfile->print_output_data(this, _(this->map_name_)); }
919
920  private:
921   // Name to use in a map file.  Maps are a rarely used feature, but
922   // the space usage is minor as aren't very many of these objects.
923   const char* map_name_;
924 };
925
926 // Fill fixed space with zeroes.  This is just like
927 // Output_data_fixed_space, except that the map name is known.
928
929 class Output_data_zero_fill : public Output_section_data
930 {
931  public:
932   Output_data_zero_fill(off_t data_size, uint64_t addralign)
933     : Output_section_data(data_size, addralign, true)
934   { }
935
936  protected:
937   // There is no data to write out.
938   void
939   do_write(Output_file*)
940   { }
941
942   // Write to a map file.
943   void
944   do_print_to_mapfile(Mapfile* mapfile) const
945   { mapfile->print_output_data(this, "** zero fill"); }
946 };
947
948 // A string table which goes into an output section.
949
950 class Output_data_strtab : public Output_section_data
951 {
952  public:
953   Output_data_strtab(Stringpool* strtab)
954     : Output_section_data(1), strtab_(strtab)
955   { }
956
957  protected:
958   // This is called to update the section size prior to assigning
959   // the address and file offset.
960   void
961   update_data_size()
962   { this->set_final_data_size(); }
963
964   // This is called to set the address and file offset.  Here we make
965   // sure that the Stringpool is finalized.
966   void
967   set_final_data_size();
968
969   // Write out the data.
970   void
971   do_write(Output_file*);
972
973   // Write the data to a buffer.
974   void
975   do_write_to_buffer(unsigned char* buffer)
976   { this->strtab_->write_to_buffer(buffer, this->data_size()); }
977
978   // Write to a map file.
979   void
980   do_print_to_mapfile(Mapfile* mapfile) const
981   { mapfile->print_output_data(this, _("** string table")); }
982
983  private:
984   Stringpool* strtab_;
985 };
986
987 // This POD class is used to represent a single reloc in the output
988 // file.  This could be a private class within Output_data_reloc, but
989 // the templatization is complex enough that I broke it out into a
990 // separate class.  The class is templatized on either elfcpp::SHT_REL
991 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
992 // relocation or an ordinary relocation.
993
994 // A relocation can be against a global symbol, a local symbol, a
995 // local section symbol, an output section, or the undefined symbol at
996 // index 0.  We represent the latter by using a NULL global symbol.
997
998 template<int sh_type, bool dynamic, int size, bool big_endian>
999 class Output_reloc;
1000
1001 template<bool dynamic, int size, bool big_endian>
1002 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1003 {
1004  public:
1005   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1006   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1007
1008   static const Address invalid_address = static_cast<Address>(0) - 1;
1009
1010   // An uninitialized entry.  We need this because we want to put
1011   // instances of this class into an STL container.
1012   Output_reloc()
1013     : local_sym_index_(INVALID_CODE)
1014   { }
1015
1016   // We have a bunch of different constructors.  They come in pairs
1017   // depending on how the address of the relocation is specified.  It
1018   // can either be an offset in an Output_data or an offset in an
1019   // input section.
1020
1021   // A reloc against a global symbol.
1022
1023   Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1024                Address address, bool is_relative, bool is_symbolless);
1025
1026   Output_reloc(Symbol* gsym, unsigned int type,
1027                Sized_relobj<size, big_endian>* relobj,
1028                unsigned int shndx, Address address, bool is_relative,
1029                bool is_symbolless);
1030
1031   // A reloc against a local symbol or local section symbol.
1032
1033   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1034                unsigned int local_sym_index, unsigned int type,
1035                Output_data* od, Address address, bool is_relative,
1036                bool is_symbolless, bool is_section_symbol);
1037
1038   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1039                unsigned int local_sym_index, unsigned int type,
1040                unsigned int shndx, Address address, bool is_relative,
1041                bool is_symbolless, bool is_section_symbol);
1042
1043   // A reloc against the STT_SECTION symbol of an output section.
1044
1045   Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1046                Address address);
1047
1048   Output_reloc(Output_section* os, unsigned int type,
1049                Sized_relobj<size, big_endian>* relobj,
1050                unsigned int shndx, Address address);
1051
1052   // An absolute relocation with no symbol.
1053
1054   Output_reloc(unsigned int type, Output_data* od, Address address);
1055
1056   Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1057                unsigned int shndx, Address address);
1058
1059   // A target specific relocation.  The target will be called to get
1060   // the symbol index, passing ARG.  The type and offset will be set
1061   // as for other relocation types.
1062
1063   Output_reloc(unsigned int type, void* arg, Output_data* od,
1064                Address address);
1065
1066   Output_reloc(unsigned int type, void* arg,
1067                Sized_relobj<size, big_endian>* relobj,
1068                unsigned int shndx, Address address);
1069
1070   // Return the reloc type.
1071   unsigned int
1072   type() const
1073   { return this->type_; }
1074
1075   // Return whether this is a RELATIVE relocation.
1076   bool
1077   is_relative() const
1078   { return this->is_relative_; }
1079
1080   // Return whether this is a relocation which should not use
1081   // a symbol, but which obtains its addend from a symbol.
1082   bool
1083   is_symbolless() const
1084   { return this->is_symbolless_; }
1085
1086   // Return whether this is against a local section symbol.
1087   bool
1088   is_local_section_symbol() const
1089   {
1090     return (this->local_sym_index_ != GSYM_CODE
1091             && this->local_sym_index_ != SECTION_CODE
1092             && this->local_sym_index_ != INVALID_CODE
1093             && this->local_sym_index_ != TARGET_CODE
1094             && this->is_section_symbol_);
1095   }
1096
1097   // Return whether this is a target specific relocation.
1098   bool
1099   is_target_specific() const
1100   { return this->local_sym_index_ == TARGET_CODE; }
1101
1102   // Return the argument to pass to the target for a target specific
1103   // relocation.
1104   void*
1105   target_arg() const
1106   {
1107     gold_assert(this->local_sym_index_ == TARGET_CODE);
1108     return this->u1_.arg;
1109   }
1110
1111   // For a local section symbol, return the offset of the input
1112   // section within the output section.  ADDEND is the addend being
1113   // applied to the input section.
1114   Address
1115   local_section_offset(Addend addend) const;
1116
1117   // Get the value of the symbol referred to by a Rel relocation when
1118   // we are adding the given ADDEND.
1119   Address
1120   symbol_value(Addend addend) const;
1121
1122   // Write the reloc entry to an output view.
1123   void
1124   write(unsigned char* pov) const;
1125
1126   // Write the offset and info fields to Write_rel.
1127   template<typename Write_rel>
1128   void write_rel(Write_rel*) const;
1129
1130   // This is used when sorting dynamic relocs.  Return -1 to sort this
1131   // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1132   int
1133   compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1134     const;
1135
1136   // Return whether this reloc should be sorted before the argument
1137   // when sorting dynamic relocs.
1138   bool
1139   sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1140               r2) const
1141   { return this->compare(r2) < 0; }
1142
1143  private:
1144   // Record that we need a dynamic symbol index.
1145   void
1146   set_needs_dynsym_index();
1147
1148   // Return the symbol index.
1149   unsigned int
1150   get_symbol_index() const;
1151
1152   // Return the output address.
1153   Address
1154   get_address() const;
1155
1156   // Codes for local_sym_index_.
1157   enum
1158   {
1159     // Global symbol.
1160     GSYM_CODE = -1U,
1161     // Output section.
1162     SECTION_CODE = -2U,
1163     // Target specific.
1164     TARGET_CODE = -3U,
1165     // Invalid uninitialized entry.
1166     INVALID_CODE = -4U
1167   };
1168
1169   union
1170   {
1171     // For a local symbol or local section symbol
1172     // (this->local_sym_index_ >= 0), the object.  We will never
1173     // generate a relocation against a local symbol in a dynamic
1174     // object; that doesn't make sense.  And our callers will always
1175     // be templatized, so we use Sized_relobj here.
1176     Sized_relobj<size, big_endian>* relobj;
1177     // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1178     // symbol.  If this is NULL, it indicates a relocation against the
1179     // undefined 0 symbol.
1180     Symbol* gsym;
1181     // For a relocation against an output section
1182     // (this->local_sym_index_ == SECTION_CODE), the output section.
1183     Output_section* os;
1184     // For a target specific relocation, an argument to pass to the
1185     // target.
1186     void* arg;
1187   } u1_;
1188   union
1189   {
1190     // If this->shndx_ is not INVALID CODE, the object which holds the
1191     // input section being used to specify the reloc address.
1192     Sized_relobj<size, big_endian>* relobj;
1193     // If this->shndx_ is INVALID_CODE, the output data being used to
1194     // specify the reloc address.  This may be NULL if the reloc
1195     // address is absolute.
1196     Output_data* od;
1197   } u2_;
1198   // The address offset within the input section or the Output_data.
1199   Address address_;
1200   // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1201   // relocation against an output section, or TARGET_CODE for a target
1202   // specific relocation, or INVALID_CODE for an uninitialized value.
1203   // Otherwise, for a local symbol (this->is_section_symbol_ is
1204   // false), the local symbol index.  For a local section symbol
1205   // (this->is_section_symbol_ is true), the section index in the
1206   // input file.
1207   unsigned int local_sym_index_;
1208   // The reloc type--a processor specific code.
1209   unsigned int type_ : 29;
1210   // True if the relocation is a RELATIVE relocation.
1211   bool is_relative_ : 1;
1212   // True if the relocation is one which should not use
1213   // a symbol, but which obtains its addend from a symbol.
1214   bool is_symbolless_ : 1;
1215   // True if the relocation is against a section symbol.
1216   bool is_section_symbol_ : 1;
1217   // If the reloc address is an input section in an object, the
1218   // section index.  This is INVALID_CODE if the reloc address is
1219   // specified in some other way.
1220   unsigned int shndx_;
1221 };
1222
1223 // The SHT_RELA version of Output_reloc<>.  This is just derived from
1224 // the SHT_REL version of Output_reloc, but it adds an addend.
1225
1226 template<bool dynamic, int size, bool big_endian>
1227 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1228 {
1229  public:
1230   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1231   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1232
1233   // An uninitialized entry.
1234   Output_reloc()
1235     : rel_()
1236   { }
1237
1238   // A reloc against a global symbol.
1239
1240   Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1241                Address address, Addend addend, bool is_relative,
1242                bool is_symbolless)
1243     : rel_(gsym, type, od, address, is_relative, is_symbolless),
1244       addend_(addend)
1245   { }
1246
1247   Output_reloc(Symbol* gsym, unsigned int type,
1248                Sized_relobj<size, big_endian>* relobj,
1249                unsigned int shndx, Address address, Addend addend,
1250                bool is_relative, bool is_symbolless)
1251     : rel_(gsym, type, relobj, shndx, address, is_relative,
1252            is_symbolless), addend_(addend)
1253   { }
1254
1255   // A reloc against a local symbol.
1256
1257   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1258                unsigned int local_sym_index, unsigned int type,
1259                Output_data* od, Address address,
1260                Addend addend, bool is_relative,
1261                bool is_symbolless, bool is_section_symbol)
1262     : rel_(relobj, local_sym_index, type, od, address, is_relative,
1263            is_symbolless, is_section_symbol),
1264       addend_(addend)
1265   { }
1266
1267   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1268                unsigned int local_sym_index, unsigned int type,
1269                unsigned int shndx, Address address,
1270                Addend addend, bool is_relative,
1271                bool is_symbolless, bool is_section_symbol)
1272     : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1273            is_symbolless, is_section_symbol),
1274       addend_(addend)
1275   { }
1276
1277   // A reloc against the STT_SECTION symbol of an output section.
1278
1279   Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1280                Address address, Addend addend)
1281     : rel_(os, type, od, address), addend_(addend)
1282   { }
1283
1284   Output_reloc(Output_section* os, unsigned int type,
1285                Sized_relobj<size, big_endian>* relobj,
1286                unsigned int shndx, Address address, Addend addend)
1287     : rel_(os, type, relobj, shndx, address), addend_(addend)
1288   { }
1289
1290   // An absolute relocation with no symbol.
1291
1292   Output_reloc(unsigned int type, Output_data* od, Address address,
1293                Addend addend)
1294     : rel_(type, od, address), addend_(addend)
1295   { }
1296
1297   Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1298                unsigned int shndx, Address address, Addend addend)
1299     : rel_(type, relobj, shndx, address), addend_(addend)
1300   { }
1301
1302   // A target specific relocation.  The target will be called to get
1303   // the symbol index and the addend, passing ARG.  The type and
1304   // offset will be set as for other relocation types.
1305
1306   Output_reloc(unsigned int type, void* arg, Output_data* od,
1307                Address address, Addend addend)
1308     : rel_(type, arg, od, address), addend_(addend)
1309   { }
1310
1311   Output_reloc(unsigned int type, void* arg,
1312                Sized_relobj<size, big_endian>* relobj,
1313                unsigned int shndx, Address address, Addend addend)
1314     : rel_(type, arg, relobj, shndx, address), addend_(addend)
1315   { }
1316
1317   // Return whether this is a RELATIVE relocation.
1318   bool
1319   is_relative() const
1320   { return this->rel_.is_relative(); }
1321
1322   // Return whether this is a relocation which should not use
1323   // a symbol, but which obtains its addend from a symbol.
1324   bool
1325   is_symbolless() const
1326   { return this->rel_.is_symbolless(); }
1327
1328   // Write the reloc entry to an output view.
1329   void
1330   write(unsigned char* pov) const;
1331
1332   // Return whether this reloc should be sorted before the argument
1333   // when sorting dynamic relocs.
1334   bool
1335   sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1336               r2) const
1337   {
1338     int i = this->rel_.compare(r2.rel_);
1339     if (i < 0)
1340       return true;
1341     else if (i > 0)
1342       return false;
1343     else
1344       return this->addend_ < r2.addend_;
1345   }
1346
1347  private:
1348   // The basic reloc.
1349   Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1350   // The addend.
1351   Addend addend_;
1352 };
1353
1354 // Output_data_reloc_generic is a non-template base class for
1355 // Output_data_reloc_base.  This gives the generic code a way to hold
1356 // a pointer to a reloc section.
1357
1358 class Output_data_reloc_generic : public Output_section_data_build
1359 {
1360  public:
1361   Output_data_reloc_generic(int size, bool sort_relocs)
1362     : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1363       relative_reloc_count_(0), sort_relocs_(sort_relocs)
1364   { }
1365
1366   // Return the number of relative relocs in this section.
1367   size_t
1368   relative_reloc_count() const
1369   { return this->relative_reloc_count_; }
1370
1371   // Whether we should sort the relocs.
1372   bool
1373   sort_relocs() const
1374   { return this->sort_relocs_; }
1375
1376  protected:
1377   // Note that we've added another relative reloc.
1378   void
1379   bump_relative_reloc_count()
1380   { ++this->relative_reloc_count_; }
1381
1382  private:
1383   // The number of relative relocs added to this section.  This is to
1384   // support DT_RELCOUNT.
1385   size_t relative_reloc_count_;
1386   // Whether to sort the relocations when writing them out, to make
1387   // the dynamic linker more efficient.
1388   bool sort_relocs_;
1389 };
1390
1391 // Output_data_reloc is used to manage a section containing relocs.
1392 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA.  DYNAMIC
1393 // indicates whether this is a dynamic relocation or a normal
1394 // relocation.  Output_data_reloc_base is a base class.
1395 // Output_data_reloc is the real class, which we specialize based on
1396 // the reloc type.
1397
1398 template<int sh_type, bool dynamic, int size, bool big_endian>
1399 class Output_data_reloc_base : public Output_data_reloc_generic
1400 {
1401  public:
1402   typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1403   typedef typename Output_reloc_type::Address Address;
1404   static const int reloc_size =
1405     Reloc_types<sh_type, size, big_endian>::reloc_size;
1406
1407   // Construct the section.
1408   Output_data_reloc_base(bool sort_relocs)
1409     : Output_data_reloc_generic(size, sort_relocs)
1410   { }
1411
1412  protected:
1413   // Write out the data.
1414   void
1415   do_write(Output_file*);
1416
1417   // Set the entry size and the link.
1418   void
1419   do_adjust_output_section(Output_section* os);
1420
1421   // Write to a map file.
1422   void
1423   do_print_to_mapfile(Mapfile* mapfile) const
1424   {
1425     mapfile->print_output_data(this,
1426                                (dynamic
1427                                 ? _("** dynamic relocs")
1428                                 : _("** relocs")));
1429   }
1430
1431   // Add a relocation entry.
1432   void
1433   add(Output_data* od, const Output_reloc_type& reloc)
1434   {
1435     this->relocs_.push_back(reloc);
1436     this->set_current_data_size(this->relocs_.size() * reloc_size);
1437     od->add_dynamic_reloc();
1438     if (reloc.is_relative())
1439       this->bump_relative_reloc_count();
1440   }
1441
1442  private:
1443   typedef std::vector<Output_reloc_type> Relocs;
1444
1445   // The class used to sort the relocations.
1446   struct Sort_relocs_comparison
1447   {
1448     bool
1449     operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1450     { return r1.sort_before(r2); }
1451   };
1452
1453   // The relocations in this section.
1454   Relocs relocs_;
1455 };
1456
1457 // The class which callers actually create.
1458
1459 template<int sh_type, bool dynamic, int size, bool big_endian>
1460 class Output_data_reloc;
1461
1462 // The SHT_REL version of Output_data_reloc.
1463
1464 template<bool dynamic, int size, bool big_endian>
1465 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1466   : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1467 {
1468  private:
1469   typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1470                                  big_endian> Base;
1471
1472  public:
1473   typedef typename Base::Output_reloc_type Output_reloc_type;
1474   typedef typename Output_reloc_type::Address Address;
1475
1476   Output_data_reloc(bool sr)
1477     : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1478   { }
1479
1480   // Add a reloc against a global symbol.
1481
1482   void
1483   add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1484   { this->add(od, Output_reloc_type(gsym, type, od, address, false, false)); }
1485
1486   void
1487   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1488              Sized_relobj<size, big_endian>* relobj,
1489              unsigned int shndx, Address address)
1490   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1491                                     false, false)); }
1492
1493   // These are to simplify the Copy_relocs class.
1494
1495   void
1496   add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1497              Address addend)
1498   {
1499     gold_assert(addend == 0);
1500     this->add_global(gsym, type, od, address);
1501   }
1502
1503   void
1504   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1505              Sized_relobj<size, big_endian>* relobj,
1506              unsigned int shndx, Address address, Address addend)
1507   {
1508     gold_assert(addend == 0);
1509     this->add_global(gsym, type, od, relobj, shndx, address);
1510   }
1511
1512   // Add a RELATIVE reloc against a global symbol.  The final relocation
1513   // will not reference the symbol.
1514
1515   void
1516   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1517                       Address address)
1518   { this->add(od, Output_reloc_type(gsym, type, od, address, true, true)); }
1519
1520   void
1521   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1522                       Sized_relobj<size, big_endian>* relobj,
1523                       unsigned int shndx, Address address)
1524   {
1525     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1526                                     true, true));
1527   }
1528
1529   // Add a global relocation which does not use a symbol for the relocation,
1530   // but which gets its addend from a symbol.
1531
1532   void
1533   add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1534                                Output_data* od, Address address)
1535   { this->add(od, Output_reloc_type(gsym, type, od, address, false, true)); }
1536
1537   void
1538   add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1539                                Output_data* od,
1540                                Sized_relobj<size, big_endian>* relobj,
1541                                unsigned int shndx, Address address)
1542   {
1543     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1544                                     false, true));
1545   }
1546
1547   // Add a reloc against a local symbol.
1548
1549   void
1550   add_local(Sized_relobj<size, big_endian>* relobj,
1551             unsigned int local_sym_index, unsigned int type,
1552             Output_data* od, Address address)
1553   {
1554     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1555                                     address, false, false, false));
1556   }
1557
1558   void
1559   add_local(Sized_relobj<size, big_endian>* relobj,
1560             unsigned int local_sym_index, unsigned int type,
1561             Output_data* od, unsigned int shndx, Address address)
1562   {
1563     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1564                                     address, false, false, false));
1565   }
1566
1567   // Add a RELATIVE reloc against a local symbol.
1568
1569   void
1570   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1571                      unsigned int local_sym_index, unsigned int type,
1572                      Output_data* od, Address address)
1573   {
1574     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1575                                     address, true, true, false));
1576   }
1577
1578   void
1579   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1580                      unsigned int local_sym_index, unsigned int type,
1581                      Output_data* od, unsigned int shndx, Address address)
1582   {
1583     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1584                                     address, true, true, false));
1585   }
1586
1587   // Add a local relocation which does not use a symbol for the relocation,
1588   // but which gets its addend from a symbol.
1589
1590   void
1591   add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1592                               unsigned int local_sym_index, unsigned int type,
1593                               Output_data* od, Address address)
1594   {
1595     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1596                                     address, false, true, false));
1597   }
1598
1599   void
1600   add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1601                               unsigned int local_sym_index, unsigned int type,
1602                               Output_data* od, unsigned int shndx,
1603                               Address address)
1604   {
1605     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1606                                     address, false, true, false));
1607   }
1608
1609   // Add a reloc against a local section symbol.  This will be
1610   // converted into a reloc against the STT_SECTION symbol of the
1611   // output section.
1612
1613   void
1614   add_local_section(Sized_relobj<size, big_endian>* relobj,
1615                     unsigned int input_shndx, unsigned int type,
1616                     Output_data* od, Address address)
1617   {
1618     this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1619                                     address, false, false, true));
1620   }
1621
1622   void
1623   add_local_section(Sized_relobj<size, big_endian>* relobj,
1624                     unsigned int input_shndx, unsigned int type,
1625                     Output_data* od, unsigned int shndx, Address address)
1626   {
1627     this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1628                                     address, false, false, true));
1629   }
1630
1631   // A reloc against the STT_SECTION symbol of an output section.
1632   // OS is the Output_section that the relocation refers to; OD is
1633   // the Output_data object being relocated.
1634
1635   void
1636   add_output_section(Output_section* os, unsigned int type,
1637                      Output_data* od, Address address)
1638   { this->add(od, Output_reloc_type(os, type, od, address)); }
1639
1640   void
1641   add_output_section(Output_section* os, unsigned int type, Output_data* od,
1642                      Sized_relobj<size, big_endian>* relobj,
1643                      unsigned int shndx, Address address)
1644   { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1645
1646   // Add an absolute relocation.
1647
1648   void
1649   add_absolute(unsigned int type, Output_data* od, Address address)
1650   { this->add(od, Output_reloc_type(type, od, address)); }
1651
1652   void
1653   add_absolute(unsigned int type, Output_data* od,
1654                Sized_relobj<size, big_endian>* relobj,
1655                unsigned int shndx, Address address)
1656   { this->add(od, Output_reloc_type(type, relobj, shndx, address)); }
1657
1658   // Add a target specific relocation.  A target which calls this must
1659   // define the reloc_symbol_index and reloc_addend virtual functions.
1660
1661   void
1662   add_target_specific(unsigned int type, void* arg, Output_data* od,
1663                       Address address)
1664   { this->add(od, Output_reloc_type(type, arg, od, address)); }
1665
1666   void
1667   add_target_specific(unsigned int type, void* arg, Output_data* od,
1668                       Sized_relobj<size, big_endian>* relobj,
1669                       unsigned int shndx, Address address)
1670   { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1671 };
1672
1673 // The SHT_RELA version of Output_data_reloc.
1674
1675 template<bool dynamic, int size, bool big_endian>
1676 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1677   : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1678 {
1679  private:
1680   typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1681                                  big_endian> Base;
1682
1683  public:
1684   typedef typename Base::Output_reloc_type Output_reloc_type;
1685   typedef typename Output_reloc_type::Address Address;
1686   typedef typename Output_reloc_type::Addend Addend;
1687
1688   Output_data_reloc(bool sr)
1689     : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1690   { }
1691
1692   // Add a reloc against a global symbol.
1693
1694   void
1695   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1696              Address address, Addend addend)
1697   { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1698                                     false, false)); }
1699
1700   void
1701   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1702              Sized_relobj<size, big_endian>* relobj,
1703              unsigned int shndx, Address address,
1704              Addend addend)
1705   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1706                                     addend, false, false)); }
1707
1708   // Add a RELATIVE reloc against a global symbol.  The final output
1709   // relocation will not reference the symbol, but we must keep the symbol
1710   // information long enough to set the addend of the relocation correctly
1711   // when it is written.
1712
1713   void
1714   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1715                       Address address, Addend addend)
1716   { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1717                                     true)); }
1718
1719   void
1720   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1721                       Sized_relobj<size, big_endian>* relobj,
1722                       unsigned int shndx, Address address, Addend addend)
1723   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1724                                     addend, true, true)); }
1725
1726   // Add a global relocation which does not use a symbol for the relocation,
1727   // but which gets its addend from a symbol.
1728
1729   void
1730   add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1731                                Address address, Addend addend)
1732   { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1733                                     false, true)); }
1734
1735   void
1736   add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1737                                Output_data* od,
1738                                Sized_relobj<size, big_endian>* relobj,
1739                                unsigned int shndx, Address address, Addend addend)
1740   { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1741                                     addend, false, true)); }
1742
1743   // Add a reloc against a local symbol.
1744
1745   void
1746   add_local(Sized_relobj<size, big_endian>* relobj,
1747             unsigned int local_sym_index, unsigned int type,
1748             Output_data* od, Address address, Addend addend)
1749   {
1750     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1751                                     addend, false, false, false));
1752   }
1753
1754   void
1755   add_local(Sized_relobj<size, big_endian>* relobj,
1756             unsigned int local_sym_index, unsigned int type,
1757             Output_data* od, unsigned int shndx, Address address,
1758             Addend addend)
1759   {
1760     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1761                                     address, addend, false, false, false));
1762   }
1763
1764   // Add a RELATIVE reloc against a local symbol.
1765
1766   void
1767   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1768                      unsigned int local_sym_index, unsigned int type,
1769                      Output_data* od, Address address, Addend addend)
1770   {
1771     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1772                                     addend, true, true, false));
1773   }
1774
1775   void
1776   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1777                      unsigned int local_sym_index, unsigned int type,
1778                      Output_data* od, unsigned int shndx, Address address,
1779                      Addend addend)
1780   {
1781     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1782                                     address, addend, true, true, false));
1783   }
1784
1785   // Add a local relocation which does not use a symbol for the relocation,
1786   // but which gets it's addend from a symbol.
1787
1788   void
1789   add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1790                               unsigned int local_sym_index, unsigned int type,
1791                               Output_data* od, Address address, Addend addend)
1792   {
1793     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1794                                     addend, false, true, false));
1795   }
1796
1797   void
1798   add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1799                               unsigned int local_sym_index, unsigned int type,
1800                               Output_data* od, unsigned int shndx,
1801                               Address address, Addend addend)
1802   {
1803     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1804                                     address, addend, false, true, false));
1805   }
1806
1807   // Add a reloc against a local section symbol.  This will be
1808   // converted into a reloc against the STT_SECTION symbol of the
1809   // output section.
1810
1811   void
1812   add_local_section(Sized_relobj<size, big_endian>* relobj,
1813                     unsigned int input_shndx, unsigned int type,
1814                     Output_data* od, Address address, Addend addend)
1815   {
1816     this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1817                                     addend, false, false, true));
1818   }
1819
1820   void
1821   add_local_section(Sized_relobj<size, big_endian>* relobj,
1822                      unsigned int input_shndx, unsigned int type,
1823                      Output_data* od, unsigned int shndx, Address address,
1824                      Addend addend)
1825   {
1826     this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1827                                     address, addend, false, false, true));
1828   }
1829
1830   // A reloc against the STT_SECTION symbol of an output section.
1831
1832   void
1833   add_output_section(Output_section* os, unsigned int type, Output_data* od,
1834                      Address address, Addend addend)
1835   { this->add(od, Output_reloc_type(os, type, od, address, addend)); }
1836
1837   void
1838   add_output_section(Output_section* os, unsigned int type, Output_data* od,
1839                      Sized_relobj<size, big_endian>* relobj,
1840                      unsigned int shndx, Address address, Addend addend)
1841   { this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
1842                                     addend)); }
1843
1844   // Add an absolute relocation.
1845
1846   void
1847   add_absolute(unsigned int type, Output_data* od, Address address,
1848                Addend addend)
1849   { this->add(od, Output_reloc_type(type, od, address, addend)); }
1850
1851   void
1852   add_absolute(unsigned int type, Output_data* od,
1853                Sized_relobj<size, big_endian>* relobj,
1854                unsigned int shndx, Address address, Addend addend)
1855   { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); }
1856
1857   // Add a target specific relocation.  A target which calls this must
1858   // define the reloc_symbol_index and reloc_addend virtual functions.
1859
1860   void
1861   add_target_specific(unsigned int type, void* arg, Output_data* od,
1862                       Address address, Addend addend)
1863   { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
1864
1865   void
1866   add_target_specific(unsigned int type, void* arg, Output_data* od,
1867                       Sized_relobj<size, big_endian>* relobj,
1868                       unsigned int shndx, Address address, Addend addend)
1869   {
1870     this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
1871                                     addend));
1872   }
1873 };
1874
1875 // Output_relocatable_relocs represents a relocation section in a
1876 // relocatable link.  The actual data is written out in the target
1877 // hook relocate_for_relocatable.  This just saves space for it.
1878
1879 template<int sh_type, int size, bool big_endian>
1880 class Output_relocatable_relocs : public Output_section_data
1881 {
1882  public:
1883   Output_relocatable_relocs(Relocatable_relocs* rr)
1884     : Output_section_data(Output_data::default_alignment_for_size(size)),
1885       rr_(rr)
1886   { }
1887
1888   void
1889   set_final_data_size();
1890
1891   // Write out the data.  There is nothing to do here.
1892   void
1893   do_write(Output_file*)
1894   { }
1895
1896   // Write to a map file.
1897   void
1898   do_print_to_mapfile(Mapfile* mapfile) const
1899   { mapfile->print_output_data(this, _("** relocs")); }
1900
1901  private:
1902   // The relocs associated with this input section.
1903   Relocatable_relocs* rr_;
1904 };
1905
1906 // Handle a GROUP section.
1907
1908 template<int size, bool big_endian>
1909 class Output_data_group : public Output_section_data
1910 {
1911  public:
1912   // The constructor clears *INPUT_SHNDXES.
1913   Output_data_group(Sized_relobj<size, big_endian>* relobj,
1914                     section_size_type entry_count,
1915                     elfcpp::Elf_Word flags,
1916                     std::vector<unsigned int>* input_shndxes);
1917
1918   void
1919   do_write(Output_file*);
1920
1921   // Write to a map file.
1922   void
1923   do_print_to_mapfile(Mapfile* mapfile) const
1924   { mapfile->print_output_data(this, _("** group")); }
1925
1926   // Set final data size.
1927   void
1928   set_final_data_size()
1929   { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1930
1931  private:
1932   // The input object.
1933   Sized_relobj<size, big_endian>* relobj_;
1934   // The group flag word.
1935   elfcpp::Elf_Word flags_;
1936   // The section indexes of the input sections in this group.
1937   std::vector<unsigned int> input_shndxes_;
1938 };
1939
1940 // Output_data_got is used to manage a GOT.  Each entry in the GOT is
1941 // for one symbol--either a global symbol or a local symbol in an
1942 // object.  The target specific code adds entries to the GOT as
1943 // needed.
1944
1945 template<int size, bool big_endian>
1946 class Output_data_got : public Output_section_data_build
1947 {
1948  public:
1949   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1950   typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1951   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1952
1953   Output_data_got()
1954     : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1955       entries_(), free_list_()
1956   { }
1957
1958   Output_data_got(off_t data_size)
1959     : Output_section_data_build(data_size,
1960                                 Output_data::default_alignment_for_size(size)),
1961       entries_(), free_list_()
1962   {
1963     // For an incremental update, we have an existing GOT section.
1964     // Initialize the list of entries and the free list.
1965     this->entries_.resize(data_size / (size / 8));
1966     this->free_list_.init(data_size, false);
1967   }
1968
1969   // Add an entry for a global symbol to the GOT.  Return true if this
1970   // is a new GOT entry, false if the symbol was already in the GOT.
1971   bool
1972   add_global(Symbol* gsym, unsigned int got_type);
1973
1974   // Like add_global, but use the PLT offset of the global symbol if
1975   // it has one.
1976   bool
1977   add_global_plt(Symbol* gsym, unsigned int got_type);
1978
1979   // Add an entry for a global symbol to the GOT, and add a dynamic
1980   // relocation of type R_TYPE for the GOT entry.
1981   void
1982   add_global_with_rel(Symbol* gsym, unsigned int got_type,
1983                       Rel_dyn* rel_dyn, unsigned int r_type);
1984
1985   void
1986   add_global_with_rela(Symbol* gsym, unsigned int got_type,
1987                        Rela_dyn* rela_dyn, unsigned int r_type);
1988
1989   // Add a pair of entries for a global symbol to the GOT, and add
1990   // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1991   void
1992   add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1993                            Rel_dyn* rel_dyn, unsigned int r_type_1,
1994                            unsigned int r_type_2);
1995
1996   void
1997   add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1998                             Rela_dyn* rela_dyn, unsigned int r_type_1,
1999                             unsigned int r_type_2);
2000
2001   // Add an entry for a local symbol to the GOT.  This returns true if
2002   // this is a new GOT entry, false if the symbol already has a GOT
2003   // entry.
2004   bool
2005   add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
2006             unsigned int got_type);
2007
2008   // Like add_local, but use the PLT offset of the local symbol if it
2009   // has one.
2010   bool
2011   add_local_plt(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
2012                 unsigned int got_type);
2013
2014   // Add an entry for a local symbol to the GOT, and add a dynamic
2015   // relocation of type R_TYPE for the GOT entry.
2016   void
2017   add_local_with_rel(Sized_relobj<size, big_endian>* object,
2018                      unsigned int sym_index, unsigned int got_type,
2019                      Rel_dyn* rel_dyn, unsigned int r_type);
2020
2021   void
2022   add_local_with_rela(Sized_relobj<size, big_endian>* object,
2023                       unsigned int sym_index, unsigned int got_type,
2024                       Rela_dyn* rela_dyn, unsigned int r_type);
2025
2026   // Add a pair of entries for a local symbol to the GOT, and add
2027   // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2028   void
2029   add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
2030                           unsigned int sym_index, unsigned int shndx,
2031                           unsigned int got_type, Rel_dyn* rel_dyn,
2032                           unsigned int r_type_1, unsigned int r_type_2);
2033
2034   void
2035   add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
2036                           unsigned int sym_index, unsigned int shndx,
2037                           unsigned int got_type, Rela_dyn* rela_dyn,
2038                           unsigned int r_type_1, unsigned int r_type_2);
2039
2040   // Add a constant to the GOT.  This returns the offset of the new
2041   // entry from the start of the GOT.
2042   unsigned int
2043   add_constant(Valtype constant)
2044   {
2045     unsigned int got_offset = this->add_got_entry(Got_entry(constant));
2046     return got_offset;
2047   }
2048
2049   // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
2050   void
2051   reserve_slot(unsigned int i);
2052
2053   // Reserve a slot in the GOT for a global symbol.
2054   void
2055   reserve_slot_for_global(unsigned int i, Symbol* gsym, unsigned int got_type);
2056
2057  protected:
2058   // Write out the GOT table.
2059   void
2060   do_write(Output_file*);
2061
2062   // Write to a map file.
2063   void
2064   do_print_to_mapfile(Mapfile* mapfile) const
2065   { mapfile->print_output_data(this, _("** GOT")); }
2066
2067  private:
2068   // This POD class holds a single GOT entry.
2069   class Got_entry
2070   {
2071    public:
2072     // Create a zero entry.
2073     Got_entry()
2074       : local_sym_index_(RESERVED_CODE), use_plt_offset_(false)
2075     { this->u_.constant = 0; }
2076
2077     // Create a global symbol entry.
2078     Got_entry(Symbol* gsym, bool use_plt_offset)
2079       : local_sym_index_(GSYM_CODE), use_plt_offset_(use_plt_offset)
2080     { this->u_.gsym = gsym; }
2081
2082     // Create a local symbol entry.
2083     Got_entry(Sized_relobj<size, big_endian>* object,
2084               unsigned int local_sym_index, bool use_plt_offset)
2085       : local_sym_index_(local_sym_index), use_plt_offset_(use_plt_offset)
2086     {
2087       gold_assert(local_sym_index != GSYM_CODE
2088                   && local_sym_index != CONSTANT_CODE
2089                   && local_sym_index != RESERVED_CODE
2090                   && local_sym_index == this->local_sym_index_);
2091       this->u_.object = object;
2092     }
2093
2094     // Create a constant entry.  The constant is a host value--it will
2095     // be swapped, if necessary, when it is written out.
2096     explicit Got_entry(Valtype constant)
2097       : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
2098     { this->u_.constant = constant; }
2099
2100     // Write the GOT entry to an output view.
2101     void
2102     write(unsigned char* pov) const;
2103
2104    private:
2105     enum
2106     {
2107       GSYM_CODE = 0x7fffffff,
2108       CONSTANT_CODE = 0x7ffffffe,
2109       RESERVED_CODE = 0x7ffffffd
2110     };
2111
2112     union
2113     {
2114       // For a local symbol, the object.
2115       Sized_relobj<size, big_endian>* object;
2116       // For a global symbol, the symbol.
2117       Symbol* gsym;
2118       // For a constant, the constant.
2119       Valtype constant;
2120     } u_;
2121     // For a local symbol, the local symbol index.  This is GSYM_CODE
2122     // for a global symbol, or CONSTANT_CODE for a constant.
2123     unsigned int local_sym_index_ : 31;
2124     // Whether to use the PLT offset of the symbol if it has one.
2125     bool use_plt_offset_ : 1;
2126   };
2127
2128   typedef std::vector<Got_entry> Got_entries;
2129
2130   // Create a new GOT entry and return its offset.
2131   unsigned int
2132   add_got_entry(Got_entry got_entry);
2133
2134   // Create a pair of new GOT entries and return the offset of the first.
2135   unsigned int
2136   add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2137
2138   // Return the offset into the GOT of GOT entry I.
2139   unsigned int
2140   got_offset(unsigned int i) const
2141   { return i * (size / 8); }
2142
2143   // Return the offset into the GOT of the last entry added.
2144   unsigned int
2145   last_got_offset() const
2146   { return this->got_offset(this->entries_.size() - 1); }
2147
2148   // Set the size of the section.
2149   void
2150   set_got_size()
2151   { this->set_current_data_size(this->got_offset(this->entries_.size())); }
2152
2153   // The list of GOT entries.
2154   Got_entries entries_;
2155
2156   // List of available regions within the section, for incremental
2157   // update links.
2158   Free_list free_list_;
2159 };
2160
2161 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2162 // section.
2163
2164 class Output_data_dynamic : public Output_section_data
2165 {
2166  public:
2167   Output_data_dynamic(Stringpool* pool)
2168     : Output_section_data(Output_data::default_alignment()),
2169       entries_(), pool_(pool)
2170   { }
2171
2172   // Add a new dynamic entry with a fixed numeric value.
2173   void
2174   add_constant(elfcpp::DT tag, unsigned int val)
2175   { this->add_entry(Dynamic_entry(tag, val)); }
2176
2177   // Add a new dynamic entry with the address of output data.
2178   void
2179   add_section_address(elfcpp::DT tag, const Output_data* od)
2180   { this->add_entry(Dynamic_entry(tag, od, false)); }
2181
2182   // Add a new dynamic entry with the address of output data
2183   // plus a constant offset.
2184   void
2185   add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2186                           unsigned int offset)
2187   { this->add_entry(Dynamic_entry(tag, od, offset)); }
2188
2189   // Add a new dynamic entry with the size of output data.
2190   void
2191   add_section_size(elfcpp::DT tag, const Output_data* od)
2192   { this->add_entry(Dynamic_entry(tag, od, true)); }
2193
2194   // Add a new dynamic entry with the total size of two output datas.
2195   void
2196   add_section_size(elfcpp::DT tag, const Output_data* od,
2197                    const Output_data* od2)
2198   { this->add_entry(Dynamic_entry(tag, od, od2)); }
2199
2200   // Add a new dynamic entry with the address of a symbol.
2201   void
2202   add_symbol(elfcpp::DT tag, const Symbol* sym)
2203   { this->add_entry(Dynamic_entry(tag, sym)); }
2204
2205   // Add a new dynamic entry with a string.
2206   void
2207   add_string(elfcpp::DT tag, const char* str)
2208   { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2209
2210   void
2211   add_string(elfcpp::DT tag, const std::string& str)
2212   { this->add_string(tag, str.c_str()); }
2213
2214  protected:
2215   // Adjust the output section to set the entry size.
2216   void
2217   do_adjust_output_section(Output_section*);
2218
2219   // Set the final data size.
2220   void
2221   set_final_data_size();
2222
2223   // Write out the dynamic entries.
2224   void
2225   do_write(Output_file*);
2226
2227   // Write to a map file.
2228   void
2229   do_print_to_mapfile(Mapfile* mapfile) const
2230   { mapfile->print_output_data(this, _("** dynamic")); }
2231
2232  private:
2233   // This POD class holds a single dynamic entry.
2234   class Dynamic_entry
2235   {
2236    public:
2237     // Create an entry with a fixed numeric value.
2238     Dynamic_entry(elfcpp::DT tag, unsigned int val)
2239       : tag_(tag), offset_(DYNAMIC_NUMBER)
2240     { this->u_.val = val; }
2241
2242     // Create an entry with the size or address of a section.
2243     Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2244       : tag_(tag),
2245         offset_(section_size
2246                 ? DYNAMIC_SECTION_SIZE
2247                 : DYNAMIC_SECTION_ADDRESS)
2248     {
2249       this->u_.od = od;
2250       this->od2 = NULL;
2251     }
2252
2253     // Create an entry with the size of two sections.
2254     Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2255       : tag_(tag),
2256         offset_(DYNAMIC_SECTION_SIZE)
2257     {
2258       this->u_.od = od;
2259       this->od2 = od2;
2260     }
2261
2262     // Create an entry with the address of a section plus a constant offset.
2263     Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2264       : tag_(tag),
2265         offset_(offset)
2266     { this->u_.od = od; }
2267
2268     // Create an entry with the address of a symbol.
2269     Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2270       : tag_(tag), offset_(DYNAMIC_SYMBOL)
2271     { this->u_.sym = sym; }
2272
2273     // Create an entry with a string.
2274     Dynamic_entry(elfcpp::DT tag, const char* str)
2275       : tag_(tag), offset_(DYNAMIC_STRING)
2276     { this->u_.str = str; }
2277
2278     // Return the tag of this entry.
2279     elfcpp::DT
2280     tag() const
2281     { return this->tag_; }
2282
2283     // Write the dynamic entry to an output view.
2284     template<int size, bool big_endian>
2285     void
2286     write(unsigned char* pov, const Stringpool*) const;
2287
2288    private:
2289     // Classification is encoded in the OFFSET field.
2290     enum Classification
2291     {
2292       // Section address.
2293       DYNAMIC_SECTION_ADDRESS = 0,
2294       // Number.
2295       DYNAMIC_NUMBER = -1U,
2296       // Section size.
2297       DYNAMIC_SECTION_SIZE = -2U,
2298       // Symbol adress.
2299       DYNAMIC_SYMBOL = -3U,
2300       // String.
2301       DYNAMIC_STRING = -4U
2302       // Any other value indicates a section address plus OFFSET.
2303     };
2304
2305     union
2306     {
2307       // For DYNAMIC_NUMBER.
2308       unsigned int val;
2309       // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2310       const Output_data* od;
2311       // For DYNAMIC_SYMBOL.
2312       const Symbol* sym;
2313       // For DYNAMIC_STRING.
2314       const char* str;
2315     } u_;
2316     // For DYNAMIC_SYMBOL with two sections.
2317     const Output_data* od2;
2318     // The dynamic tag.
2319     elfcpp::DT tag_;
2320     // The type of entry (Classification) or offset within a section.
2321     unsigned int offset_;
2322   };
2323
2324   // Add an entry to the list.
2325   void
2326   add_entry(const Dynamic_entry& entry)
2327   { this->entries_.push_back(entry); }
2328
2329   // Sized version of write function.
2330   template<int size, bool big_endian>
2331   void
2332   sized_write(Output_file* of);
2333
2334   // The type of the list of entries.
2335   typedef std::vector<Dynamic_entry> Dynamic_entries;
2336
2337   // The entries.
2338   Dynamic_entries entries_;
2339   // The pool used for strings.
2340   Stringpool* pool_;
2341 };
2342
2343 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2344 // which may be required if the object file has more than
2345 // SHN_LORESERVE sections.
2346
2347 class Output_symtab_xindex : public Output_section_data
2348 {
2349  public:
2350   Output_symtab_xindex(size_t symcount)
2351     : Output_section_data(symcount * 4, 4, true),
2352       entries_()
2353   { }
2354
2355   // Add an entry: symbol number SYMNDX has section SHNDX.
2356   void
2357   add(unsigned int symndx, unsigned int shndx)
2358   { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2359
2360  protected:
2361   void
2362   do_write(Output_file*);
2363
2364   // Write to a map file.
2365   void
2366   do_print_to_mapfile(Mapfile* mapfile) const
2367   { mapfile->print_output_data(this, _("** symtab xindex")); }
2368
2369  private:
2370   template<bool big_endian>
2371   void
2372   endian_do_write(unsigned char*);
2373
2374   // It is likely that most symbols will not require entries.  Rather
2375   // than keep a vector for all symbols, we keep pairs of symbol index
2376   // and section index.
2377   typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2378
2379   // The entries we need.
2380   Xindex_entries entries_;
2381 };
2382
2383 // A relaxed input section.
2384 class Output_relaxed_input_section : public Output_section_data_build
2385 {
2386  public:
2387   // We would like to call relobj->section_addralign(shndx) to get the
2388   // alignment but we do not want the constructor to fail.  So callers
2389   // are repsonsible for ensuring that.
2390   Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2391                                uint64_t addralign)
2392     : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2393   { }
2394  
2395   // Return the Relobj of this relaxed input section.
2396   Relobj*
2397   relobj() const
2398   { return this->relobj_; }
2399  
2400   // Return the section index of this relaxed input section.
2401   unsigned int
2402   shndx() const
2403   { return this->shndx_; }
2404
2405  private:
2406   Relobj* relobj_;
2407   unsigned int shndx_;
2408 };
2409
2410 // This class describes properties of merge data sections.  It is used
2411 // as a key type for maps.
2412 class Merge_section_properties
2413 {
2414  public:
2415   Merge_section_properties(bool is_string, uint64_t entsize,
2416                              uint64_t addralign)
2417     : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2418   { }
2419
2420   // Whether this equals to another Merge_section_properties MSP.
2421   bool
2422   eq(const Merge_section_properties& msp) const
2423   {
2424     return ((this->is_string_ == msp.is_string_)
2425             && (this->entsize_ == msp.entsize_)
2426             && (this->addralign_ == msp.addralign_));
2427   }
2428
2429   // Compute a hash value for this using 64-bit FNV-1a hash.
2430   size_t
2431   hash_value() const
2432   {
2433     uint64_t h = 14695981039346656037ULL;       // FNV offset basis.
2434     uint64_t prime = 1099511628211ULL;
2435     h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2436     h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2437     h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2438     return h;
2439   }
2440     
2441   // Functors for associative containers.
2442   struct equal_to
2443   {
2444     bool
2445     operator()(const Merge_section_properties& msp1,
2446                const Merge_section_properties& msp2) const
2447     { return msp1.eq(msp2); }
2448   };
2449
2450   struct hash
2451   {
2452     size_t
2453     operator()(const Merge_section_properties& msp) const
2454     { return msp.hash_value(); }
2455   };
2456
2457  private:
2458   // Whether this merge data section is for strings.
2459   bool is_string_;
2460   // Entsize of this merge data section.
2461   uint64_t entsize_;
2462   // Address alignment.
2463   uint64_t addralign_;
2464 };
2465
2466 // This class is used to speed up look up of special input sections in an
2467 // Output_section.
2468
2469 class Output_section_lookup_maps
2470 {
2471  public:
2472   Output_section_lookup_maps()
2473     : is_valid_(true), merge_sections_by_properties_(),
2474       merge_sections_by_id_(), relaxed_input_sections_by_id_()
2475   { }
2476
2477   // Whether the maps are valid.
2478   bool
2479   is_valid() const
2480   { return this->is_valid_; }
2481
2482   // Invalidate the maps.
2483   void
2484   invalidate()
2485   { this->is_valid_ = false; }
2486
2487   // Clear the maps.
2488   void
2489   clear()
2490   {
2491     this->merge_sections_by_properties_.clear();
2492     this->merge_sections_by_id_.clear();
2493     this->relaxed_input_sections_by_id_.clear();
2494     // A cleared map is valid.
2495     this->is_valid_ = true;
2496   }
2497   
2498   // Find a merge section by merge section properties.  Return NULL if none
2499   // is found.
2500   Output_merge_base*
2501   find_merge_section(const Merge_section_properties& msp) const
2502   {
2503     gold_assert(this->is_valid_);
2504     Merge_sections_by_properties::const_iterator p =
2505       this->merge_sections_by_properties_.find(msp);
2506     return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2507   }
2508
2509   // Find a merge section by section ID of a merge input section.  Return NULL
2510   // if none is found.
2511   Output_merge_base*
2512   find_merge_section(const Object* object, unsigned int shndx) const
2513   {
2514     gold_assert(this->is_valid_);
2515     Merge_sections_by_id::const_iterator p =
2516       this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2517     return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2518   }
2519
2520   // Add a merge section pointed by POMB with properties MSP.
2521   void
2522   add_merge_section(const Merge_section_properties& msp,
2523                     Output_merge_base* pomb)
2524   {
2525     std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2526     std::pair<Merge_sections_by_properties::iterator, bool> result =
2527       this->merge_sections_by_properties_.insert(value);
2528     gold_assert(result.second);
2529   }
2530   
2531   // Add a mapping from a merged input section in OBJECT with index SHNDX
2532   // to a merge output section pointed by POMB.
2533   void
2534   add_merge_input_section(const Object* object, unsigned int shndx,
2535                           Output_merge_base* pomb)
2536   {
2537     Const_section_id csid(object, shndx);
2538     std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2539     std::pair<Merge_sections_by_id::iterator, bool> result =
2540       this->merge_sections_by_id_.insert(value);
2541     gold_assert(result.second);
2542   }
2543
2544   // Find a relaxed input section of OBJECT with index SHNDX.
2545   Output_relaxed_input_section*
2546   find_relaxed_input_section(const Object* object, unsigned int shndx) const
2547   {
2548     gold_assert(this->is_valid_);
2549     Relaxed_input_sections_by_id::const_iterator p =
2550       this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2551     return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2552   }
2553
2554   // Add a relaxed input section pointed by POMB and whose original input
2555   // section is in OBJECT with index SHNDX.
2556   void
2557   add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2558                             Output_relaxed_input_section* poris)
2559   {
2560     Const_section_id csid(relobj, shndx);
2561     std::pair<Const_section_id, Output_relaxed_input_section*>
2562       value(csid, poris);
2563     std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2564       this->relaxed_input_sections_by_id_.insert(value);
2565     gold_assert(result.second);
2566   }
2567
2568  private:
2569   typedef Unordered_map<Const_section_id, Output_merge_base*,
2570                         Const_section_id_hash>
2571     Merge_sections_by_id;
2572
2573   typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2574                         Merge_section_properties::hash,
2575                         Merge_section_properties::equal_to>
2576     Merge_sections_by_properties;
2577
2578   typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2579                         Const_section_id_hash>
2580     Relaxed_input_sections_by_id;
2581
2582   // Whether this is valid
2583   bool is_valid_;
2584   // Merge sections by merge section properties.
2585   Merge_sections_by_properties merge_sections_by_properties_;
2586   // Merge sections by section IDs.
2587   Merge_sections_by_id merge_sections_by_id_;
2588   // Relaxed sections by section IDs.
2589   Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2590 };
2591
2592 // An output section.  We don't expect to have too many output
2593 // sections, so we don't bother to do a template on the size.
2594
2595 class Output_section : public Output_data
2596 {
2597  public:
2598   // Create an output section, giving the name, type, and flags.
2599   Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2600   virtual ~Output_section();
2601
2602   // Add a new input section SHNDX, named NAME, with header SHDR, from
2603   // object OBJECT.  RELOC_SHNDX is the index of a relocation section
2604   // which applies to this section, or 0 if none, or -1 if more than
2605   // one.  HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2606   // in a linker script; in that case we need to keep track of input
2607   // sections associated with an output section.  Return the offset
2608   // within the output section.
2609   template<int size, bool big_endian>
2610   off_t
2611   add_input_section(Layout* layout, Sized_relobj<size, big_endian>* object,
2612                     unsigned int shndx, const char* name,
2613                     const elfcpp::Shdr<size, big_endian>& shdr,
2614                     unsigned int reloc_shndx, bool have_sections_script);
2615
2616   // Add generated data POSD to this output section.
2617   void
2618   add_output_section_data(Output_section_data* posd);
2619
2620   // Add a relaxed input section PORIS called NAME to this output section
2621   // with LAYOUT.
2622   void
2623   add_relaxed_input_section(Layout* layout,
2624                             Output_relaxed_input_section* poris,
2625                             const std::string& name);
2626
2627   // Return the section name.
2628   const char*
2629   name() const
2630   { return this->name_; }
2631
2632   // Return the section type.
2633   elfcpp::Elf_Word
2634   type() const
2635   { return this->type_; }
2636
2637   // Return the section flags.
2638   elfcpp::Elf_Xword
2639   flags() const
2640   { return this->flags_; }
2641
2642   // Update the output section flags based on input section flags.
2643   void
2644   update_flags_for_input_section(elfcpp::Elf_Xword flags);
2645
2646   // Return the entsize field.
2647   uint64_t
2648   entsize() const
2649   { return this->entsize_; }
2650
2651   // Set the entsize field.
2652   void
2653   set_entsize(uint64_t v);
2654
2655   // Set the load address.
2656   void
2657   set_load_address(uint64_t load_address)
2658   {
2659     this->load_address_ = load_address;
2660     this->has_load_address_ = true;
2661   }
2662
2663   // Set the link field to the output section index of a section.
2664   void
2665   set_link_section(const Output_data* od)
2666   {
2667     gold_assert(this->link_ == 0
2668                 && !this->should_link_to_symtab_
2669                 && !this->should_link_to_dynsym_);
2670     this->link_section_ = od;
2671   }
2672
2673   // Set the link field to a constant.
2674   void
2675   set_link(unsigned int v)
2676   {
2677     gold_assert(this->link_section_ == NULL
2678                 && !this->should_link_to_symtab_
2679                 && !this->should_link_to_dynsym_);
2680     this->link_ = v;
2681   }
2682
2683   // Record that this section should link to the normal symbol table.
2684   void
2685   set_should_link_to_symtab()
2686   {
2687     gold_assert(this->link_section_ == NULL
2688                 && this->link_ == 0
2689                 && !this->should_link_to_dynsym_);
2690     this->should_link_to_symtab_ = true;
2691   }
2692
2693   // Record that this section should link to the dynamic symbol table.
2694   void
2695   set_should_link_to_dynsym()
2696   {
2697     gold_assert(this->link_section_ == NULL
2698                 && this->link_ == 0
2699                 && !this->should_link_to_symtab_);
2700     this->should_link_to_dynsym_ = true;
2701   }
2702
2703   // Return the info field.
2704   unsigned int
2705   info() const
2706   {
2707     gold_assert(this->info_section_ == NULL
2708                 && this->info_symndx_ == NULL);
2709     return this->info_;
2710   }
2711
2712   // Set the info field to the output section index of a section.
2713   void
2714   set_info_section(const Output_section* os)
2715   {
2716     gold_assert((this->info_section_ == NULL
2717                  || (this->info_section_ == os
2718                      && this->info_uses_section_index_))
2719                 && this->info_symndx_ == NULL
2720                 && this->info_ == 0);
2721     this->info_section_ = os;
2722     this->info_uses_section_index_= true;
2723   }
2724
2725   // Set the info field to the symbol table index of a symbol.
2726   void
2727   set_info_symndx(const Symbol* sym)
2728   {
2729     gold_assert(this->info_section_ == NULL
2730                 && (this->info_symndx_ == NULL
2731                     || this->info_symndx_ == sym)
2732                 && this->info_ == 0);
2733     this->info_symndx_ = sym;
2734   }
2735
2736   // Set the info field to the symbol table index of a section symbol.
2737   void
2738   set_info_section_symndx(const Output_section* os)
2739   {
2740     gold_assert((this->info_section_ == NULL
2741                  || (this->info_section_ == os
2742                      && !this->info_uses_section_index_))
2743                 && this->info_symndx_ == NULL
2744                 && this->info_ == 0);
2745     this->info_section_ = os;
2746     this->info_uses_section_index_ = false;
2747   }
2748
2749   // Set the info field to a constant.
2750   void
2751   set_info(unsigned int v)
2752   {
2753     gold_assert(this->info_section_ == NULL
2754                 && this->info_symndx_ == NULL
2755                 && (this->info_ == 0
2756                     || this->info_ == v));
2757     this->info_ = v;
2758   }
2759
2760   // Set the addralign field.
2761   void
2762   set_addralign(uint64_t v)
2763   { this->addralign_ = v; }
2764
2765   // Whether the output section index has been set.
2766   bool
2767   has_out_shndx() const
2768   { return this->out_shndx_ != -1U; }
2769
2770   // Indicate that we need a symtab index.
2771   void
2772   set_needs_symtab_index()
2773   { this->needs_symtab_index_ = true; }
2774
2775   // Return whether we need a symtab index.
2776   bool
2777   needs_symtab_index() const
2778   { return this->needs_symtab_index_; }
2779
2780   // Get the symtab index.
2781   unsigned int
2782   symtab_index() const
2783   {
2784     gold_assert(this->symtab_index_ != 0);
2785     return this->symtab_index_;
2786   }
2787
2788   // Set the symtab index.
2789   void
2790   set_symtab_index(unsigned int index)
2791   {
2792     gold_assert(index != 0);
2793     this->symtab_index_ = index;
2794   }
2795
2796   // Indicate that we need a dynsym index.
2797   void
2798   set_needs_dynsym_index()
2799   { this->needs_dynsym_index_ = true; }
2800
2801   // Return whether we need a dynsym index.
2802   bool
2803   needs_dynsym_index() const
2804   { return this->needs_dynsym_index_; }
2805
2806   // Get the dynsym index.
2807   unsigned int
2808   dynsym_index() const
2809   {
2810     gold_assert(this->dynsym_index_ != 0);
2811     return this->dynsym_index_;
2812   }
2813
2814   // Set the dynsym index.
2815   void
2816   set_dynsym_index(unsigned int index)
2817   {
2818     gold_assert(index != 0);
2819     this->dynsym_index_ = index;
2820   }
2821
2822   // Return whether the input sections sections attachd to this output
2823   // section may require sorting.  This is used to handle constructor
2824   // priorities compatibly with GNU ld.
2825   bool
2826   may_sort_attached_input_sections() const
2827   { return this->may_sort_attached_input_sections_; }
2828
2829   // Record that the input sections attached to this output section
2830   // may require sorting.
2831   void
2832   set_may_sort_attached_input_sections()
2833   { this->may_sort_attached_input_sections_ = true; }
2834
2835    // Returns true if input sections must be sorted according to the
2836   // order in which their name appear in the --section-ordering-file.
2837   bool
2838   input_section_order_specified()
2839   { return this->input_section_order_specified_; }
2840
2841   // Record that input sections must be sorted as some of their names
2842   // match the patterns specified through --section-ordering-file.
2843   void
2844   set_input_section_order_specified()
2845   { this->input_section_order_specified_ = true; }
2846
2847   // Return whether the input sections attached to this output section
2848   // require sorting.  This is used to handle constructor priorities
2849   // compatibly with GNU ld.
2850   bool
2851   must_sort_attached_input_sections() const
2852   { return this->must_sort_attached_input_sections_; }
2853
2854   // Record that the input sections attached to this output section
2855   // require sorting.
2856   void
2857   set_must_sort_attached_input_sections()
2858   { this->must_sort_attached_input_sections_ = true; }
2859
2860   // Get the order in which this section appears in the PT_LOAD output
2861   // segment.
2862   Output_section_order
2863   order() const
2864   { return this->order_; }
2865
2866   // Set the order for this section.
2867   void
2868   set_order(Output_section_order order)
2869   { this->order_ = order; }
2870
2871   // Return whether this section holds relro data--data which has
2872   // dynamic relocations but which may be marked read-only after the
2873   // dynamic relocations have been completed.
2874   bool
2875   is_relro() const
2876   { return this->is_relro_; }
2877
2878   // Record that this section holds relro data.
2879   void
2880   set_is_relro()
2881   { this->is_relro_ = true; }
2882
2883   // Record that this section does not hold relro data.
2884   void
2885   clear_is_relro()
2886   { this->is_relro_ = false; }
2887
2888   // True if this is a small section: a section which holds small
2889   // variables.
2890   bool
2891   is_small_section() const
2892   { return this->is_small_section_; }
2893
2894   // Record that this is a small section.
2895   void
2896   set_is_small_section()
2897   { this->is_small_section_ = true; }
2898
2899   // True if this is a large section: a section which holds large
2900   // variables.
2901   bool
2902   is_large_section() const
2903   { return this->is_large_section_; }
2904
2905   // Record that this is a large section.
2906   void
2907   set_is_large_section()
2908   { this->is_large_section_ = true; }
2909
2910   // True if this is a large data (not BSS) section.
2911   bool
2912   is_large_data_section()
2913   { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2914
2915   // Return whether this section should be written after all the input
2916   // sections are complete.
2917   bool
2918   after_input_sections() const
2919   { return this->after_input_sections_; }
2920
2921   // Record that this section should be written after all the input
2922   // sections are complete.
2923   void
2924   set_after_input_sections()
2925   { this->after_input_sections_ = true; }
2926
2927   // Return whether this section requires postprocessing after all
2928   // relocations have been applied.
2929   bool
2930   requires_postprocessing() const
2931   { return this->requires_postprocessing_; }
2932
2933   // If a section requires postprocessing, return the buffer to use.
2934   unsigned char*
2935   postprocessing_buffer() const
2936   {
2937     gold_assert(this->postprocessing_buffer_ != NULL);
2938     return this->postprocessing_buffer_;
2939   }
2940
2941   // If a section requires postprocessing, create the buffer to use.
2942   void
2943   create_postprocessing_buffer();
2944
2945   // If a section requires postprocessing, this is the size of the
2946   // buffer to which relocations should be applied.
2947   off_t
2948   postprocessing_buffer_size() const
2949   { return this->current_data_size_for_child(); }
2950
2951   // Modify the section name.  This is only permitted for an
2952   // unallocated section, and only before the size has been finalized.
2953   // Otherwise the name will not get into Layout::namepool_.
2954   void
2955   set_name(const char* newname)
2956   {
2957     gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2958     gold_assert(!this->is_data_size_valid());
2959     this->name_ = newname;
2960   }
2961
2962   // Return whether the offset OFFSET in the input section SHNDX in
2963   // object OBJECT is being included in the link.
2964   bool
2965   is_input_address_mapped(const Relobj* object, unsigned int shndx,
2966                           off_t offset) const;
2967
2968   // Return the offset within the output section of OFFSET relative to
2969   // the start of input section SHNDX in object OBJECT.
2970   section_offset_type
2971   output_offset(const Relobj* object, unsigned int shndx,
2972                 section_offset_type offset) const;
2973
2974   // Return the output virtual address of OFFSET relative to the start
2975   // of input section SHNDX in object OBJECT.
2976   uint64_t
2977   output_address(const Relobj* object, unsigned int shndx,
2978                  off_t offset) const;
2979
2980   // Look for the merged section for input section SHNDX in object
2981   // OBJECT.  If found, return true, and set *ADDR to the address of
2982   // the start of the merged section.  This is not necessary the
2983   // output offset corresponding to input offset 0 in the section,
2984   // since the section may be mapped arbitrarily.
2985   bool
2986   find_starting_output_address(const Relobj* object, unsigned int shndx,
2987                                uint64_t* addr) const;
2988
2989   // Record that this output section was found in the SECTIONS clause
2990   // of a linker script.
2991   void
2992   set_found_in_sections_clause()
2993   { this->found_in_sections_clause_ = true; }
2994
2995   // Return whether this output section was found in the SECTIONS
2996   // clause of a linker script.
2997   bool
2998   found_in_sections_clause() const
2999   { return this->found_in_sections_clause_; }
3000
3001   // Write the section header into *OPHDR.
3002   template<int size, bool big_endian>
3003   void
3004   write_header(const Layout*, const Stringpool*,
3005                elfcpp::Shdr_write<size, big_endian>*) const;
3006
3007   // The next few calls are for linker script support.
3008
3009   // In some cases we need to keep a list of the input sections
3010   // associated with this output section.  We only need the list if we
3011   // might have to change the offsets of the input section within the
3012   // output section after we add the input section.  The ordinary
3013   // input sections will be written out when we process the object
3014   // file, and as such we don't need to track them here.  We do need
3015   // to track Output_section_data objects here.  We store instances of
3016   // this structure in a std::vector, so it must be a POD.  There can
3017   // be many instances of this structure, so we use a union to save
3018   // some space.
3019   class Input_section
3020   {
3021    public:
3022     Input_section()
3023       : shndx_(0), p2align_(0)
3024     {
3025       this->u1_.data_size = 0;
3026       this->u2_.object = NULL;
3027     }
3028
3029     // For an ordinary input section.
3030     Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3031                   uint64_t addralign)
3032       : shndx_(shndx),
3033         p2align_(ffsll(static_cast<long long>(addralign))),
3034         section_order_index_(0)
3035     {
3036       gold_assert(shndx != OUTPUT_SECTION_CODE
3037                   && shndx != MERGE_DATA_SECTION_CODE
3038                   && shndx != MERGE_STRING_SECTION_CODE
3039                   && shndx != RELAXED_INPUT_SECTION_CODE);
3040       this->u1_.data_size = data_size;
3041       this->u2_.object = object;
3042     }
3043
3044     // For a non-merge output section.
3045     Input_section(Output_section_data* posd)
3046       : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3047         section_order_index_(0)
3048     {
3049       this->u1_.data_size = 0;
3050       this->u2_.posd = posd;
3051     }
3052
3053     // For a merge section.
3054     Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3055       : shndx_(is_string
3056                ? MERGE_STRING_SECTION_CODE
3057                : MERGE_DATA_SECTION_CODE),
3058         p2align_(0),
3059         section_order_index_(0)
3060     {
3061       this->u1_.entsize = entsize;
3062       this->u2_.posd = posd;
3063     }
3064
3065     // For a relaxed input section.
3066     Input_section(Output_relaxed_input_section* psection)
3067       : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3068         section_order_index_(0)
3069     {
3070       this->u1_.data_size = 0;
3071       this->u2_.poris = psection;
3072     }
3073
3074     unsigned int
3075     section_order_index() const
3076     {
3077       return this->section_order_index_;
3078     }
3079
3080     void
3081     set_section_order_index(unsigned int number)
3082     {
3083       this->section_order_index_ = number;
3084     }
3085
3086     // The required alignment.
3087     uint64_t
3088     addralign() const
3089     {
3090       if (this->p2align_ != 0)
3091         return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3092       else if (!this->is_input_section())
3093         return this->u2_.posd->addralign();
3094       else
3095         return 0;
3096     }
3097
3098     // Set the required alignment, which must be either 0 or a power of 2.
3099     // For input sections that are sub-classes of Output_section_data, a
3100     // alignment of zero means asking the underlying object for alignment.
3101     void
3102     set_addralign(uint64_t addralign)
3103     {
3104       if (addralign == 0)
3105         this->p2align_ = 0;
3106       else
3107         {
3108           gold_assert((addralign & (addralign - 1)) == 0);
3109           this->p2align_ = ffsll(static_cast<long long>(addralign));
3110         }
3111     }
3112  
3113     // Return the current required size, without finalization.
3114     off_t
3115     current_data_size() const;
3116
3117     // Return the required size.
3118     off_t
3119     data_size() const;
3120
3121     // Whether this is an input section.
3122     bool
3123     is_input_section() const
3124     {
3125       return (this->shndx_ != OUTPUT_SECTION_CODE
3126               && this->shndx_ != MERGE_DATA_SECTION_CODE
3127               && this->shndx_ != MERGE_STRING_SECTION_CODE
3128               && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3129     }
3130
3131     // Return whether this is a merge section which matches the
3132     // parameters.
3133     bool
3134     is_merge_section(bool is_string, uint64_t entsize,
3135                      uint64_t addralign) const
3136     {
3137       return (this->shndx_ == (is_string
3138                                ? MERGE_STRING_SECTION_CODE
3139                                : MERGE_DATA_SECTION_CODE)
3140               && this->u1_.entsize == entsize
3141               && this->addralign() == addralign);
3142     }
3143
3144     // Return whether this is a merge section for some input section.
3145     bool
3146     is_merge_section() const
3147     {
3148       return (this->shndx_ == MERGE_DATA_SECTION_CODE
3149               || this->shndx_ == MERGE_STRING_SECTION_CODE);
3150     }
3151
3152     // Return whether this is a relaxed input section.
3153     bool
3154     is_relaxed_input_section() const
3155     { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3156
3157     // Return whether this is a generic Output_section_data.
3158     bool
3159     is_output_section_data() const
3160     {
3161       return this->shndx_ == OUTPUT_SECTION_CODE;
3162     }
3163
3164     // Return the object for an input section.
3165     Relobj*
3166     relobj() const;
3167
3168     // Return the input section index for an input section.
3169     unsigned int
3170     shndx() const;
3171
3172     // For non-input-sections, return the associated Output_section_data
3173     // object.
3174     Output_section_data*
3175     output_section_data() const
3176     {
3177       gold_assert(!this->is_input_section());
3178       return this->u2_.posd;
3179     }
3180  
3181     // For a merge section, return the Output_merge_base pointer.
3182     Output_merge_base*
3183     output_merge_base() const
3184     {
3185       gold_assert(this->is_merge_section());
3186       return this->u2_.pomb;
3187     }
3188
3189     // Return the Output_relaxed_input_section object.
3190     Output_relaxed_input_section*
3191     relaxed_input_section() const
3192     {
3193       gold_assert(this->is_relaxed_input_section());
3194       return this->u2_.poris;
3195     }
3196
3197     // Set the output section.
3198     void
3199     set_output_section(Output_section* os)
3200     {
3201       gold_assert(!this->is_input_section());
3202       Output_section_data* posd = 
3203         this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3204       posd->set_output_section(os);
3205     }
3206
3207     // Set the address and file offset.  This is called during
3208     // Layout::finalize.  SECTION_FILE_OFFSET is the file offset of
3209     // the enclosing section.
3210     void
3211     set_address_and_file_offset(uint64_t address, off_t file_offset,
3212                                 off_t section_file_offset);
3213
3214     // Reset the address and file offset.
3215     void
3216     reset_address_and_file_offset();
3217
3218     // Finalize the data size.
3219     void
3220     finalize_data_size();
3221
3222     // Add an input section, for SHF_MERGE sections.
3223     bool
3224     add_input_section(Relobj* object, unsigned int shndx)
3225     {
3226       gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3227                   || this->shndx_ == MERGE_STRING_SECTION_CODE);
3228       return this->u2_.posd->add_input_section(object, shndx);
3229     }
3230
3231     // Given an input OBJECT, an input section index SHNDX within that
3232     // object, and an OFFSET relative to the start of that input
3233     // section, return whether or not the output offset is known.  If
3234     // this function returns true, it sets *POUTPUT to the offset in
3235     // the output section, relative to the start of the input section
3236     // in the output section.  *POUTPUT may be different from OFFSET
3237     // for a merged section.
3238     bool
3239     output_offset(const Relobj* object, unsigned int shndx,
3240                   section_offset_type offset,
3241                   section_offset_type* poutput) const;
3242
3243     // Return whether this is the merge section for the input section
3244     // SHNDX in OBJECT.
3245     bool
3246     is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3247
3248     // Write out the data.  This does nothing for an input section.
3249     void
3250     write(Output_file*);
3251
3252     // Write the data to a buffer.  This does nothing for an input
3253     // section.
3254     void
3255     write_to_buffer(unsigned char*);
3256
3257     // Print to a map file.
3258     void
3259     print_to_mapfile(Mapfile*) const;
3260
3261     // Print statistics about merge sections to stderr.
3262     void
3263     print_merge_stats(const char* section_name)
3264     {
3265       if (this->shndx_ == MERGE_DATA_SECTION_CODE
3266           || this->shndx_ == MERGE_STRING_SECTION_CODE)
3267         this->u2_.posd->print_merge_stats(section_name);
3268     }
3269
3270    private:
3271     // Code values which appear in shndx_.  If the value is not one of
3272     // these codes, it is the input section index in the object file.
3273     enum
3274     {
3275       // An Output_section_data.
3276       OUTPUT_SECTION_CODE = -1U,
3277       // An Output_section_data for an SHF_MERGE section with
3278       // SHF_STRINGS not set.
3279       MERGE_DATA_SECTION_CODE = -2U,
3280       // An Output_section_data for an SHF_MERGE section with
3281       // SHF_STRINGS set.
3282       MERGE_STRING_SECTION_CODE = -3U,
3283       // An Output_section_data for a relaxed input section.
3284       RELAXED_INPUT_SECTION_CODE = -4U
3285     };
3286
3287     // For an ordinary input section, this is the section index in the
3288     // input file.  For an Output_section_data, this is
3289     // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3290     // MERGE_STRING_SECTION_CODE.
3291     unsigned int shndx_;
3292     // The required alignment, stored as a power of 2.
3293     unsigned int p2align_;
3294     union
3295     {
3296       // For an ordinary input section, the section size.
3297       off_t data_size;
3298       // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3299       // used.  For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3300       // entity size.
3301       uint64_t entsize;
3302     } u1_;
3303     union
3304     {
3305       // For an ordinary input section, the object which holds the
3306       // input section.
3307       Relobj* object;
3308       // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3309       // MERGE_STRING_SECTION_CODE, the data.
3310       Output_section_data* posd;
3311       Output_merge_base* pomb;
3312       // For RELAXED_INPUT_SECTION_CODE, the data.
3313       Output_relaxed_input_section* poris;
3314     } u2_;
3315     // The line number of the pattern it matches in the --section-ordering-file
3316     // file.  It is 0 if does not match any pattern.
3317     unsigned int section_order_index_;
3318   };
3319
3320   // Store the list of input sections for this Output_section into the
3321   // list passed in.  This removes the input sections, leaving only
3322   // any Output_section_data elements.  This returns the size of those
3323   // Output_section_data elements.  ADDRESS is the address of this
3324   // output section.  FILL is the fill value to use, in case there are
3325   // any spaces between the remaining Output_section_data elements.
3326   uint64_t
3327   get_input_sections(uint64_t address, const std::string& fill,
3328                      std::list<Input_section>*);
3329
3330   // Add a script input section.  A script input section can either be
3331   // a plain input section or a sub-class of Output_section_data.
3332   void
3333   add_script_input_section(const Input_section& input_section);
3334
3335   // Set the current size of the output section.
3336   void
3337   set_current_data_size(off_t size)
3338   { this->set_current_data_size_for_child(size); }
3339
3340   // End of linker script support.
3341
3342   // Save states before doing section layout.
3343   // This is used for relaxation.
3344   void
3345   save_states();
3346
3347   // Restore states prior to section layout.
3348   void
3349   restore_states();
3350
3351   // Discard states.
3352   void
3353   discard_states();
3354
3355   // Convert existing input sections to relaxed input sections.
3356   void
3357   convert_input_sections_to_relaxed_sections(
3358       const std::vector<Output_relaxed_input_section*>& sections);
3359
3360   // Find a relaxed input section to an input section in OBJECT
3361   // with index SHNDX.  Return NULL if none is found.
3362   const Output_relaxed_input_section*
3363   find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3364   
3365   // Whether section offsets need adjustment due to relaxation.
3366   bool
3367   section_offsets_need_adjustment() const
3368   { return this->section_offsets_need_adjustment_; }
3369
3370   // Set section_offsets_need_adjustment to be true.
3371   void
3372   set_section_offsets_need_adjustment()
3373   { this->section_offsets_need_adjustment_ = true; }
3374
3375   // Adjust section offsets of input sections in this.  This is
3376   // requires if relaxation caused some input sections to change sizes.
3377   void
3378   adjust_section_offsets();
3379
3380   // Whether this is a NOLOAD section.
3381   bool
3382   is_noload() const
3383   { return this->is_noload_; }
3384
3385   // Set NOLOAD flag.
3386   void
3387   set_is_noload()
3388   { this->is_noload_ = true; }
3389
3390   // Print merge statistics to stderr.
3391   void
3392   print_merge_stats();
3393
3394   // Set a fixed layout for the section.  Used for incremental update links.
3395   void
3396   set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3397                    uint64_t sh_addralign);
3398
3399   // Return TRUE if the section has a fixed layout.
3400   bool
3401   has_fixed_layout() const
3402   { return this->has_fixed_layout_; }
3403
3404   // Reserve space within the fixed layout for the section.  Used for
3405   // incremental update links.
3406   void
3407   reserve(uint64_t sh_offset, uint64_t sh_size);
3408
3409  protected:
3410   // Return the output section--i.e., the object itself.
3411   Output_section*
3412   do_output_section()
3413   { return this; }
3414
3415   const Output_section*
3416   do_output_section() const
3417   { return this; }
3418
3419   // Return the section index in the output file.
3420   unsigned int
3421   do_out_shndx() const
3422   {
3423     gold_assert(this->out_shndx_ != -1U);
3424     return this->out_shndx_;
3425   }
3426
3427   // Set the output section index.
3428   void
3429   do_set_out_shndx(unsigned int shndx)
3430   {
3431     gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3432     this->out_shndx_ = shndx;
3433   }
3434
3435   // Update the data size of the Output_section.  For a typical
3436   // Output_section, there is nothing to do, but if there are any
3437   // Output_section_data objects we need to do a trial layout
3438   // here.
3439   virtual void
3440   update_data_size();
3441
3442   // Set the final data size of the Output_section.  For a typical
3443   // Output_section, there is nothing to do, but if there are any
3444   // Output_section_data objects we need to set their final addresses
3445   // here.
3446   virtual void
3447   set_final_data_size();
3448
3449   // Reset the address and file offset.
3450   void
3451   do_reset_address_and_file_offset();
3452
3453   // Return true if address and file offset already have reset values. In
3454   // other words, calling reset_address_and_file_offset will not change them.
3455   bool
3456   do_address_and_file_offset_have_reset_values() const;
3457
3458   // Write the data to the file.  For a typical Output_section, this
3459   // does nothing: the data is written out by calling Object::Relocate
3460   // on each input object.  But if there are any Output_section_data
3461   // objects we do need to write them out here.
3462   virtual void
3463   do_write(Output_file*);
3464
3465   // Return the address alignment--function required by parent class.
3466   uint64_t
3467   do_addralign() const
3468   { return this->addralign_; }
3469
3470   // Return whether there is a load address.
3471   bool
3472   do_has_load_address() const
3473   { return this->has_load_address_; }
3474
3475   // Return the load address.
3476   uint64_t
3477   do_load_address() const
3478   {
3479     gold_assert(this->has_load_address_);
3480     return this->load_address_;
3481   }
3482
3483   // Return whether this is an Output_section.
3484   bool
3485   do_is_section() const
3486   { return true; }
3487
3488   // Return whether this is a section of the specified type.
3489   bool
3490   do_is_section_type(elfcpp::Elf_Word type) const
3491   { return this->type_ == type; }
3492
3493   // Return whether the specified section flag is set.
3494   bool
3495   do_is_section_flag_set(elfcpp::Elf_Xword flag) const
3496   { return (this->flags_ & flag) != 0; }
3497
3498   // Set the TLS offset.  Called only for SHT_TLS sections.
3499   void
3500   do_set_tls_offset(uint64_t tls_base);
3501
3502   // Return the TLS offset, relative to the base of the TLS segment.
3503   // Valid only for SHT_TLS sections.
3504   uint64_t
3505   do_tls_offset() const
3506   { return this->tls_offset_; }
3507
3508   // This may be implemented by a child class.
3509   virtual void
3510   do_finalize_name(Layout*)
3511   { }
3512
3513   // Print to the map file.
3514   virtual void
3515   do_print_to_mapfile(Mapfile*) const;
3516
3517   // Record that this section requires postprocessing after all
3518   // relocations have been applied.  This is called by a child class.
3519   void
3520   set_requires_postprocessing()
3521   {
3522     this->requires_postprocessing_ = true;
3523     this->after_input_sections_ = true;
3524   }
3525
3526   // Write all the data of an Output_section into the postprocessing
3527   // buffer.
3528   void
3529   write_to_postprocessing_buffer();
3530
3531   typedef std::vector<Input_section> Input_section_list;
3532
3533   // Allow a child class to access the input sections.
3534   const Input_section_list&
3535   input_sections() const
3536   { return this->input_sections_; }
3537
3538   // Whether this always keeps an input section list
3539   bool
3540   always_keeps_input_sections() const
3541   { return this->always_keeps_input_sections_; }
3542
3543   // Always keep an input section list.
3544   void
3545   set_always_keeps_input_sections()
3546   {
3547     gold_assert(this->current_data_size_for_child() == 0);
3548     this->always_keeps_input_sections_ = true;
3549   }
3550
3551  private:
3552   // We only save enough information to undo the effects of section layout.
3553   class Checkpoint_output_section
3554   {
3555    public:
3556     Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3557                               const Input_section_list& input_sections,
3558                               off_t first_input_offset,
3559                               bool attached_input_sections_are_sorted)
3560       : addralign_(addralign), flags_(flags),
3561         input_sections_(input_sections),
3562         input_sections_size_(input_sections_.size()),
3563         input_sections_copy_(), first_input_offset_(first_input_offset),
3564         attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3565     { }
3566
3567     virtual
3568     ~Checkpoint_output_section()
3569     { }
3570
3571     // Return the address alignment.
3572     uint64_t
3573     addralign() const
3574     { return this->addralign_; }
3575
3576     // Return the section flags.
3577     elfcpp::Elf_Xword
3578     flags() const
3579     { return this->flags_; }
3580
3581     // Return a reference to the input section list copy.
3582     Input_section_list*
3583     input_sections()
3584     { return &this->input_sections_copy_; }
3585
3586     // Return the size of input_sections at the time when checkpoint is
3587     // taken.
3588     size_t
3589     input_sections_size() const
3590     { return this->input_sections_size_; }
3591
3592     // Whether input sections are copied.
3593     bool
3594     input_sections_saved() const
3595     { return this->input_sections_copy_.size() == this->input_sections_size_; }
3596
3597     off_t
3598     first_input_offset() const
3599     { return this->first_input_offset_; }
3600
3601     bool
3602     attached_input_sections_are_sorted() const
3603     { return this->attached_input_sections_are_sorted_; }
3604
3605     // Save input sections.
3606     void
3607     save_input_sections()
3608     {
3609       this->input_sections_copy_.reserve(this->input_sections_size_);
3610       this->input_sections_copy_.clear();
3611       Input_section_list::const_iterator p = this->input_sections_.begin();
3612       gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3613       for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3614         this->input_sections_copy_.push_back(*p);
3615     }
3616
3617    private:
3618     // The section alignment.
3619     uint64_t addralign_;
3620     // The section flags.
3621     elfcpp::Elf_Xword flags_;
3622     // Reference to the input sections to be checkpointed.
3623     const Input_section_list& input_sections_;
3624     // Size of the checkpointed portion of input_sections_;
3625     size_t input_sections_size_;
3626     // Copy of input sections.
3627     Input_section_list input_sections_copy_;
3628     // The offset of the first entry in input_sections_.
3629     off_t first_input_offset_;
3630     // True if the input sections attached to this output section have
3631     // already been sorted.
3632     bool attached_input_sections_are_sorted_;
3633   };
3634
3635   // This class is used to sort the input sections.
3636   class Input_section_sort_entry;
3637
3638   // This is the sort comparison function for ctors and dtors.
3639   struct Input_section_sort_compare
3640   {
3641     bool
3642     operator()(const Input_section_sort_entry&,
3643                const Input_section_sort_entry&) const;
3644   };
3645
3646   // This is the sort comparison function for .init_array and .fini_array.
3647   struct Input_section_sort_init_fini_compare
3648   {
3649     bool
3650     operator()(const Input_section_sort_entry&,
3651                const Input_section_sort_entry&) const;
3652   };
3653
3654   // This is the sort comparison function when a section order is specified
3655   // from an input file.
3656   struct Input_section_sort_section_order_index_compare
3657   {
3658     bool
3659     operator()(const Input_section_sort_entry&,
3660                const Input_section_sort_entry&) const;
3661   };
3662
3663   // Fill data.  This is used to fill in data between input sections.
3664   // It is also used for data statements (BYTE, WORD, etc.) in linker
3665   // scripts.  When we have to keep track of the input sections, we
3666   // can use an Output_data_const, but we don't want to have to keep
3667   // track of input sections just to implement fills.
3668   class Fill
3669   {
3670    public:
3671     Fill(off_t section_offset, off_t length)
3672       : section_offset_(section_offset),
3673         length_(convert_to_section_size_type(length))
3674     { }
3675
3676     // Return section offset.
3677     off_t
3678     section_offset() const
3679     { return this->section_offset_; }
3680
3681     // Return fill length.
3682     section_size_type
3683     length() const
3684     { return this->length_; }
3685
3686    private:
3687     // The offset within the output section.
3688     off_t section_offset_;
3689     // The length of the space to fill.
3690     section_size_type length_;
3691   };
3692
3693   typedef std::vector<Fill> Fill_list;
3694
3695   // Map used during relaxation of existing sections.  This map
3696   // a section id an input section list index.  We assume that
3697   // Input_section_list is a vector.
3698   typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
3699
3700   // Add a new output section by Input_section.
3701   void
3702   add_output_section_data(Input_section*);
3703
3704   // Add an SHF_MERGE input section.  Returns true if the section was
3705   // handled.  If KEEPS_INPUT_SECTIONS is true, the output merge section
3706   // stores information about the merged input sections.
3707   bool
3708   add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3709                           uint64_t entsize, uint64_t addralign,
3710                           bool keeps_input_sections);
3711
3712   // Add an output SHF_MERGE section POSD to this output section.
3713   // IS_STRING indicates whether it is a SHF_STRINGS section, and
3714   // ENTSIZE is the entity size.  This returns the entry added to
3715   // input_sections_.
3716   void
3717   add_output_merge_section(Output_section_data* posd, bool is_string,
3718                            uint64_t entsize);
3719
3720   // Sort the attached input sections.
3721   void
3722   sort_attached_input_sections();
3723
3724   // Find the merge section into which an input section with index SHNDX in
3725   // OBJECT has been added.  Return NULL if none found.
3726   Output_section_data*
3727   find_merge_section(const Relobj* object, unsigned int shndx) const;
3728
3729   // Build a relaxation map.
3730   void
3731   build_relaxation_map(
3732       const Input_section_list& input_sections,
3733       size_t limit,
3734       Relaxation_map* map) const;
3735
3736   // Convert input sections in an input section list into relaxed sections.
3737   void
3738   convert_input_sections_in_list_to_relaxed_sections(
3739       const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3740       const Relaxation_map& map,
3741       Input_section_list* input_sections);
3742
3743   // Build the lookup maps for merge and relaxed input sections.
3744   void
3745   build_lookup_maps() const;
3746
3747   // Most of these fields are only valid after layout.
3748
3749   // The name of the section.  This will point into a Stringpool.
3750   const char* name_;
3751   // The section address is in the parent class.
3752   // The section alignment.
3753   uint64_t addralign_;
3754   // The section entry size.
3755   uint64_t entsize_;
3756   // The load address.  This is only used when using a linker script
3757   // with a SECTIONS clause.  The has_load_address_ field indicates
3758   // whether this field is valid.
3759   uint64_t load_address_;
3760   // The file offset is in the parent class.
3761   // Set the section link field to the index of this section.
3762   const Output_data* link_section_;
3763   // If link_section_ is NULL, this is the link field.
3764   unsigned int link_;
3765   // Set the section info field to the index of this section.
3766   const Output_section* info_section_;
3767   // If info_section_ is NULL, set the info field to the symbol table
3768   // index of this symbol.
3769   const Symbol* info_symndx_;
3770   // If info_section_ and info_symndx_ are NULL, this is the section
3771   // info field.
3772   unsigned int info_;
3773   // The section type.
3774   const elfcpp::Elf_Word type_;
3775   // The section flags.
3776   elfcpp::Elf_Xword flags_;
3777   // The order of this section in the output segment.
3778   Output_section_order order_;
3779   // The section index.
3780   unsigned int out_shndx_;
3781   // If there is a STT_SECTION for this output section in the normal
3782   // symbol table, this is the symbol index.  This starts out as zero.
3783   // It is initialized in Layout::finalize() to be the index, or -1U
3784   // if there isn't one.
3785   unsigned int symtab_index_;
3786   // If there is a STT_SECTION for this output section in the dynamic
3787   // symbol table, this is the symbol index.  This starts out as zero.
3788   // It is initialized in Layout::finalize() to be the index, or -1U
3789   // if there isn't one.
3790   unsigned int dynsym_index_;
3791   // The input sections.  This will be empty in cases where we don't
3792   // need to keep track of them.
3793   Input_section_list input_sections_;
3794   // The offset of the first entry in input_sections_.
3795   off_t first_input_offset_;
3796   // The fill data.  This is separate from input_sections_ because we
3797   // often will need fill sections without needing to keep track of
3798   // input sections.
3799   Fill_list fills_;
3800   // If the section requires postprocessing, this buffer holds the
3801   // section contents during relocation.
3802   unsigned char* postprocessing_buffer_;
3803   // Whether this output section needs a STT_SECTION symbol in the
3804   // normal symbol table.  This will be true if there is a relocation
3805   // which needs it.
3806   bool needs_symtab_index_ : 1;
3807   // Whether this output section needs a STT_SECTION symbol in the
3808   // dynamic symbol table.  This will be true if there is a dynamic
3809   // relocation which needs it.
3810   bool needs_dynsym_index_ : 1;
3811   // Whether the link field of this output section should point to the
3812   // normal symbol table.
3813   bool should_link_to_symtab_ : 1;
3814   // Whether the link field of this output section should point to the
3815   // dynamic symbol table.
3816   bool should_link_to_dynsym_ : 1;
3817   // Whether this section should be written after all the input
3818   // sections are complete.
3819   bool after_input_sections_ : 1;
3820   // Whether this section requires post processing after all
3821   // relocations have been applied.
3822   bool requires_postprocessing_ : 1;
3823   // Whether an input section was mapped to this output section
3824   // because of a SECTIONS clause in a linker script.
3825   bool found_in_sections_clause_ : 1;
3826   // Whether this section has an explicitly specified load address.
3827   bool has_load_address_ : 1;
3828   // True if the info_section_ field means the section index of the
3829   // section, false if it means the symbol index of the corresponding
3830   // section symbol.
3831   bool info_uses_section_index_ : 1;
3832   // True if input sections attached to this output section have to be
3833   // sorted according to a specified order.
3834   bool input_section_order_specified_ : 1;
3835   // True if the input sections attached to this output section may
3836   // need sorting.
3837   bool may_sort_attached_input_sections_ : 1;
3838   // True if the input sections attached to this output section must
3839   // be sorted.
3840   bool must_sort_attached_input_sections_ : 1;
3841   // True if the input sections attached to this output section have
3842   // already been sorted.
3843   bool attached_input_sections_are_sorted_ : 1;
3844   // True if this section holds relro data.
3845   bool is_relro_ : 1;
3846   // True if this is a small section.
3847   bool is_small_section_ : 1;
3848   // True if this is a large section.
3849   bool is_large_section_ : 1;
3850   // Whether code-fills are generated at write.
3851   bool generate_code_fills_at_write_ : 1;
3852   // Whether the entry size field should be zero.
3853   bool is_entsize_zero_ : 1;
3854   // Whether section offsets need adjustment due to relaxation.
3855   bool section_offsets_need_adjustment_ : 1;
3856   // Whether this is a NOLOAD section.
3857   bool is_noload_ : 1;
3858   // Whether this always keeps input section.
3859   bool always_keeps_input_sections_ : 1;
3860   // Whether this section has a fixed layout, for incremental update links.
3861   bool has_fixed_layout_ : 1;
3862   // For SHT_TLS sections, the offset of this section relative to the base
3863   // of the TLS segment.
3864   uint64_t tls_offset_;
3865   // Saved checkpoint.
3866   Checkpoint_output_section* checkpoint_;
3867   // Fast lookup maps for merged and relaxed input sections.
3868   Output_section_lookup_maps* lookup_maps_;
3869   // List of available regions within the section, for incremental
3870   // update links.
3871   Free_list free_list_;
3872 };
3873
3874 // An output segment.  PT_LOAD segments are built from collections of
3875 // output sections.  Other segments typically point within PT_LOAD
3876 // segments, and are built directly as needed.
3877 //
3878 // NOTE: We want to use the copy constructor for this class.  During
3879 // relaxation, we may try built the segments multiple times.  We do
3880 // that by copying the original segment list before lay-out, doing
3881 // a trial lay-out and roll-back to the saved copied if we need to
3882 // to the lay-out again.
3883
3884 class Output_segment
3885 {
3886  public:
3887   // Create an output segment, specifying the type and flags.
3888   Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3889
3890   // Return the virtual address.
3891   uint64_t
3892   vaddr() const
3893   { return this->vaddr_; }
3894
3895   // Return the physical address.
3896   uint64_t
3897   paddr() const
3898   { return this->paddr_; }
3899
3900   // Return the segment type.
3901   elfcpp::Elf_Word
3902   type() const
3903   { return this->type_; }
3904
3905   // Return the segment flags.
3906   elfcpp::Elf_Word
3907   flags() const
3908   { return this->flags_; }
3909
3910   // Return the memory size.
3911   uint64_t
3912   memsz() const
3913   { return this->memsz_; }
3914
3915   // Return the file size.
3916   off_t
3917   filesz() const
3918   { return this->filesz_; }
3919
3920   // Return the file offset.
3921   off_t
3922   offset() const
3923   { return this->offset_; }
3924
3925   // Whether this is a segment created to hold large data sections.
3926   bool
3927   is_large_data_segment() const
3928   { return this->is_large_data_segment_; }
3929
3930   // Record that this is a segment created to hold large data
3931   // sections.
3932   void
3933   set_is_large_data_segment()
3934   { this->is_large_data_segment_ = true; }
3935
3936   // Return the maximum alignment of the Output_data.
3937   uint64_t
3938   maximum_alignment();
3939
3940   // Add the Output_section OS to this PT_LOAD segment.  SEG_FLAGS is
3941   // the segment flags to use.
3942   void
3943   add_output_section_to_load(Layout* layout, Output_section* os,
3944                              elfcpp::Elf_Word seg_flags);
3945
3946   // Add the Output_section OS to this non-PT_LOAD segment.  SEG_FLAGS
3947   // is the segment flags to use.
3948   void
3949   add_output_section_to_nonload(Output_section* os,
3950                                 elfcpp::Elf_Word seg_flags);
3951
3952   // Remove an Output_section from this segment.  It is an error if it
3953   // is not present.
3954   void
3955   remove_output_section(Output_section* os);
3956
3957   // Add an Output_data (which need not be an Output_section) to the
3958   // start of this segment.
3959   void
3960   add_initial_output_data(Output_data*);
3961
3962   // Return true if this segment has any sections which hold actual
3963   // data, rather than being a BSS section.
3964   bool
3965   has_any_data_sections() const;
3966
3967   // Whether this segment has a dynamic relocs.
3968   bool
3969   has_dynamic_reloc() const;
3970
3971   // Return the address of the first section.
3972   uint64_t
3973   first_section_load_address() const;
3974
3975   // Return whether the addresses have been set already.
3976   bool
3977   are_addresses_set() const
3978   { return this->are_addresses_set_; }
3979
3980   // Set the addresses.
3981   void
3982   set_addresses(uint64_t vaddr, uint64_t paddr)
3983   {
3984     this->vaddr_ = vaddr;
3985     this->paddr_ = paddr;
3986     this->are_addresses_set_ = true;
3987   }
3988
3989   // Update the flags for the flags of an output section added to this
3990   // segment.
3991   void
3992   update_flags_for_output_section(elfcpp::Elf_Xword flags)
3993   {
3994     // The ELF ABI specifies that a PT_TLS segment should always have
3995     // PF_R as the flags.
3996     if (this->type() != elfcpp::PT_TLS)
3997       this->flags_ |= flags;
3998   }
3999
4000   // Set the segment flags.  This is only used if we have a PHDRS
4001   // clause which explicitly specifies the flags.
4002   void
4003   set_flags(elfcpp::Elf_Word flags)
4004   { this->flags_ = flags; }
4005
4006   // Set the address of the segment to ADDR and the offset to *POFF
4007   // and set the addresses and offsets of all contained output
4008   // sections accordingly.  Set the section indexes of all contained
4009   // output sections starting with *PSHNDX.  If RESET is true, first
4010   // reset the addresses of the contained sections.  Return the
4011   // address of the immediately following segment.  Update *POFF and
4012   // *PSHNDX.  This should only be called for a PT_LOAD segment.
4013   uint64_t
4014   set_section_addresses(Layout*, bool reset, uint64_t addr,
4015                         unsigned int* increase_relro, bool* has_relro,
4016                         off_t* poff, unsigned int* pshndx);
4017
4018   // Set the minimum alignment of this segment.  This may be adjusted
4019   // upward based on the section alignments.
4020   void
4021   set_minimum_p_align(uint64_t align)
4022   {
4023     if (align > this->min_p_align_)
4024       this->min_p_align_ = align;
4025   }
4026
4027   // Set the offset of this segment based on the section.  This should
4028   // only be called for a non-PT_LOAD segment.
4029   void
4030   set_offset(unsigned int increase);
4031
4032   // Set the TLS offsets of the sections contained in the PT_TLS segment.
4033   void
4034   set_tls_offsets();
4035
4036   // Return the number of output sections.
4037   unsigned int
4038   output_section_count() const;
4039
4040   // Return the section attached to the list segment with the lowest
4041   // load address.  This is used when handling a PHDRS clause in a
4042   // linker script.
4043   Output_section*
4044   section_with_lowest_load_address() const;
4045
4046   // Write the segment header into *OPHDR.
4047   template<int size, bool big_endian>
4048   void
4049   write_header(elfcpp::Phdr_write<size, big_endian>*);
4050
4051   // Write the section headers of associated sections into V.
4052   template<int size, bool big_endian>
4053   unsigned char*
4054   write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4055                         unsigned int* pshndx) const;
4056
4057   // Print the output sections in the map file.
4058   void
4059   print_sections_to_mapfile(Mapfile*) const;
4060
4061  private:
4062   typedef std::vector<Output_data*> Output_data_list;
4063
4064   // Find the maximum alignment in an Output_data_list.
4065   static uint64_t
4066   maximum_alignment_list(const Output_data_list*);
4067
4068   // Return whether the first data section is a relro section.
4069   bool
4070   is_first_section_relro() const;
4071
4072   // Set the section addresses in an Output_data_list.
4073   uint64_t
4074   set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4075                              uint64_t addr, off_t* poff, unsigned int* pshndx,
4076                              bool* in_tls);
4077
4078   // Return the number of Output_sections in an Output_data_list.
4079   unsigned int
4080   output_section_count_list(const Output_data_list*) const;
4081
4082   // Return whether an Output_data_list has a dynamic reloc.
4083   bool
4084   has_dynamic_reloc_list(const Output_data_list*) const;
4085
4086   // Find the section with the lowest load address in an
4087   // Output_data_list.
4088   void
4089   lowest_load_address_in_list(const Output_data_list* pdl,
4090                               Output_section** found,
4091                               uint64_t* found_lma) const;
4092
4093   // Find the first and last entries by address.
4094   void
4095   find_first_and_last_list(const Output_data_list* pdl,
4096                            const Output_data** pfirst,
4097                            const Output_data** plast) const;
4098
4099   // Write the section headers in the list into V.
4100   template<int size, bool big_endian>
4101   unsigned char*
4102   write_section_headers_list(const Layout*, const Stringpool*,
4103                              const Output_data_list*, unsigned char* v,
4104                              unsigned int* pshdx) const;
4105
4106   // Print a section list to the mapfile.
4107   void
4108   print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4109
4110   // NOTE: We want to use the copy constructor.  Currently, shallow copy
4111   // works for us so we do not need to write our own copy constructor.
4112   
4113   // The list of output data attached to this segment.
4114   Output_data_list output_lists_[ORDER_MAX];
4115   // The segment virtual address.
4116   uint64_t vaddr_;
4117   // The segment physical address.
4118   uint64_t paddr_;
4119   // The size of the segment in memory.
4120   uint64_t memsz_;
4121   // The maximum section alignment.  The is_max_align_known_ field
4122   // indicates whether this has been finalized.
4123   uint64_t max_align_;
4124   // The required minimum value for the p_align field.  This is used
4125   // for PT_LOAD segments.  Note that this does not mean that
4126   // addresses should be aligned to this value; it means the p_paddr
4127   // and p_vaddr fields must be congruent modulo this value.  For
4128   // non-PT_LOAD segments, the dynamic linker works more efficiently
4129   // if the p_align field has the more conventional value, although it
4130   // can align as needed.
4131   uint64_t min_p_align_;
4132   // The offset of the segment data within the file.
4133   off_t offset_;
4134   // The size of the segment data in the file.
4135   off_t filesz_;
4136   // The segment type;
4137   elfcpp::Elf_Word type_;
4138   // The segment flags.
4139   elfcpp::Elf_Word flags_;
4140   // Whether we have finalized max_align_.
4141   bool is_max_align_known_ : 1;
4142   // Whether vaddr and paddr were set by a linker script.
4143   bool are_addresses_set_ : 1;
4144   // Whether this segment holds large data sections.
4145   bool is_large_data_segment_ : 1;
4146 };
4147
4148 // This class represents the output file.
4149
4150 class Output_file
4151 {
4152  public:
4153   Output_file(const char* name);
4154
4155   // Indicate that this is a temporary file which should not be
4156   // output.
4157   void
4158   set_is_temporary()
4159   { this->is_temporary_ = true; }
4160
4161   // Try to open an existing file. Returns false if the file doesn't
4162   // exist, has a size of 0 or can't be mmaped.  This method is
4163   // thread-unsafe.
4164   bool
4165   open_for_modification();
4166
4167   // Open the output file.  FILE_SIZE is the final size of the file.
4168   // If the file already exists, it is deleted/truncated.  This method
4169   // is thread-unsafe.
4170   void
4171   open(off_t file_size);
4172
4173   // Resize the output file.  This method is thread-unsafe.
4174   void
4175   resize(off_t file_size);
4176
4177   // Close the output file (flushing all buffered data) and make sure
4178   // there are no errors.  This method is thread-unsafe.
4179   void
4180   close();
4181
4182   // Return the size of this file.
4183   off_t
4184   filesize()
4185   { return this->file_size_; }
4186
4187   // Return the name of this file.
4188   const char*
4189   filename()
4190   { return this->name_; }
4191
4192   // We currently always use mmap which makes the view handling quite
4193   // simple.  In the future we may support other approaches.
4194
4195   // Write data to the output file.
4196   void
4197   write(off_t offset, const void* data, size_t len)
4198   { memcpy(this->base_ + offset, data, len); }
4199
4200   // Get a buffer to use to write to the file, given the offset into
4201   // the file and the size.
4202   unsigned char*
4203   get_output_view(off_t start, size_t size)
4204   {
4205     gold_assert(start >= 0
4206                 && start + static_cast<off_t>(size) <= this->file_size_);
4207     return this->base_ + start;
4208   }
4209
4210   // VIEW must have been returned by get_output_view.  Write the
4211   // buffer to the file, passing in the offset and the size.
4212   void
4213   write_output_view(off_t, size_t, unsigned char*)
4214   { }
4215
4216   // Get a read/write buffer.  This is used when we want to write part
4217   // of the file, read it in, and write it again.
4218   unsigned char*
4219   get_input_output_view(off_t start, size_t size)
4220   { return this->get_output_view(start, size); }
4221
4222   // Write a read/write buffer back to the file.
4223   void
4224   write_input_output_view(off_t, size_t, unsigned char*)
4225   { }
4226
4227   // Get a read buffer.  This is used when we just want to read part
4228   // of the file back it in.
4229   const unsigned char*
4230   get_input_view(off_t start, size_t size)
4231   { return this->get_output_view(start, size); }
4232
4233   // Release a read bfufer.
4234   void
4235   free_input_view(off_t, size_t, const unsigned char*)
4236   { }
4237
4238  private:
4239   // Map the file into memory or, if that fails, allocate anonymous
4240   // memory.
4241   void
4242   map();
4243
4244   // Allocate anonymous memory for the file.
4245   bool
4246   map_anonymous();
4247
4248   // Map the file into memory.
4249   bool
4250   map_no_anonymous();
4251
4252   // Unmap the file from memory (and flush to disk buffers).
4253   void
4254   unmap();
4255
4256   // File name.
4257   const char* name_;
4258   // File descriptor.
4259   int o_;
4260   // File size.
4261   off_t file_size_;
4262   // Base of file mapped into memory.
4263   unsigned char* base_;
4264   // True iff base_ points to a memory buffer rather than an output file.
4265   bool map_is_anonymous_;
4266   // True if base_ was allocated using new rather than mmap.
4267   bool map_is_allocated_;
4268   // True if this is a temporary file which should not be output.
4269   bool is_temporary_;
4270 };
4271
4272 } // End namespace gold.
4273
4274 #endif // !defined(GOLD_OUTPUT_H)